Skip to content

Commit bc45a0e

Browse files
feature symfony#28264 [VarDumper] make RedisCaster handle RedisCluster and dump all options on all drivers (nicolas-grekas)
This PR was merged into the 4.2-dev branch. Discussion ---------- [VarDumper] make RedisCaster handle RedisCluster and dump all options on all drivers | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - e.g. <img src="https://user-images.githubusercontent.com/243674/44622234-0e769700-a8b5-11e8-8235-f42dadcd2092.png" width="300"/> Commits ------- 466d1ab [VarDumper] make RedisCaster handle RedisCluster and dump all options on all drivers
2 parents c3ec061 + 466d1ab commit bc45a0e

File tree

3 files changed

+92
-8
lines changed

3 files changed

+92
-8
lines changed

src/Symfony/Component/VarDumper/Caster/RedisCaster.php

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ class RedisCaster
2626
2 => 'IGBINARY', // Optional Redis::SERIALIZER_IGBINARY
2727
);
2828

29+
private static $mode = array(
30+
\Redis::ATOMIC => 'ATOMIC',
31+
\Redis::MULTI => 'MULTI',
32+
\Redis::PIPELINE => 'PIPELINE',
33+
);
34+
35+
private static $compression = array(
36+
0 => 'NONE', // Redis::COMPRESSION_NONE
37+
1 => 'LZF', // Redis::COMPRESSION_LZF
38+
);
39+
40+
private static $failover = array(
41+
\RedisCluster::FAILOVER_NONE => 'NONE',
42+
\RedisCluster::FAILOVER_ERROR => 'ERROR',
43+
\RedisCluster::FAILOVER_DISTRIBUTE => 'DISTRIBUTE',
44+
\RedisCluster::FAILOVER_DISTRIBUTE_SLAVES => 'DISTRIBUTE_SLAVES',
45+
);
46+
2947
public static function castRedis(\Redis $c, array $a, Stub $stub, $isNested)
3048
{
3149
$prefix = Caster::PREFIX_VIRTUAL;
@@ -36,23 +54,19 @@ public static function castRedis(\Redis $c, array $a, Stub $stub, $isNested)
3654
);
3755
}
3856

39-
$ser = $c->getOption(\Redis::OPT_SERIALIZER);
40-
$retry = \defined('Redis::OPT_SCAN') ? $c->getOption(\Redis::OPT_SCAN) : 0;
57+
$mode = $c->getMode();
4158

4259
return $a + array(
4360
$prefix.'isConnected' => $connected,
4461
$prefix.'host' => $c->getHost(),
4562
$prefix.'port' => $c->getPort(),
4663
$prefix.'auth' => $c->getAuth(),
64+
$prefix.'mode' => isset(self::$mode[$mode]) ? new ConstStub(self::$mode[$mode], $mode) : $mode,
4765
$prefix.'dbNum' => $c->getDbNum(),
4866
$prefix.'timeout' => $c->getTimeout(),
67+
$prefix.'lastError' => $c->getLastError(),
4968
$prefix.'persistentId' => $c->getPersistentID(),
50-
$prefix.'options' => new EnumStub(array(
51-
'READ_TIMEOUT' => $c->getOption(\Redis::OPT_READ_TIMEOUT),
52-
'SERIALIZER' => isset(self::$serializer[$ser]) ? new ConstStub(self::$serializer[$ser], $ser) : $ser,
53-
'PREFIX' => $c->getOption(\Redis::OPT_PREFIX),
54-
'SCAN' => new ConstStub($retry ? 'RETRY' : 'NORETRY', $retry),
55-
)),
69+
$prefix.'options' => self::getRedisOptions($c),
5670
);
5771
}
5872

@@ -63,6 +77,71 @@ public static function castRedisArray(\RedisArray $c, array $a, Stub $stub, $isN
6377
return $a + array(
6478
$prefix.'hosts' => $c->_hosts(),
6579
$prefix.'function' => ClassStub::wrapCallable($c->_function()),
80+
$prefix.'lastError' => $c->getLastError(),
81+
$prefix.'options' => self::getRedisOptions($c),
82+
);
83+
}
84+
85+
public static function castRedisCluster(\RedisCluster $c, array $a, Stub $stub, $isNested)
86+
{
87+
$prefix = Caster::PREFIX_VIRTUAL;
88+
$failover = $c->getOption(\RedisCluster::OPT_SLAVE_FAILOVER);
89+
90+
$a += array(
91+
$prefix.'_masters' => $c->_masters(),
92+
$prefix.'_redir' => $c->_redir(),
93+
$prefix.'mode' => new ConstStub($c->getMode() ? 'MULTI' : 'ATOMIC', $c->getMode()),
94+
$prefix.'lastError' => $c->getLastError(),
95+
$prefix.'options' => self::getRedisOptions($c, array(
96+
'SLAVE_FAILOVER' => isset(self::$failover[$failover]) ? new ConstStub(self::$failover[$failover], $failover) : $failover,
97+
)),
6698
);
99+
100+
return $a;
101+
}
102+
103+
/**
104+
* @param \Redis|\RedisArray|\RedisCluster
105+
*/
106+
private static function getRedisOptions($redis, array $options = array()): EnumStub
107+
{
108+
if (\is_array($serializer = $redis->getOption(\Redis::OPT_SERIALIZER))) {
109+
foreach ($serializer as &$v) {
110+
if (isset(self::$serializer[$v])) {
111+
$v = new ConstStub(self::$serializer[$v], $v);
112+
}
113+
}
114+
} elseif (isset(self::$serializer[$serializer])) {
115+
$serializer = new ConstStub(self::$serializer[$serializer], $serializer);
116+
}
117+
118+
if (\is_array($compression = \defined('Redis::OPT_COMPRESSION') ? $redis->getOption(\Redis::OPT_COMPRESSION) : 0)) {
119+
foreach ($compression as &$v) {
120+
if (isset(self::$compression[$v])) {
121+
$v = new ConstStub(self::$compression[$v], $v);
122+
}
123+
}
124+
} elseif (isset(self::$compression[$compression])) {
125+
$compression = new ConstStub(self::$compression[$compression], $compression);
126+
}
127+
128+
if (\is_array($retry = \defined('Redis::OPT_SCAN') ? $redis->getOption(\Redis::OPT_SCAN) : 0)) {
129+
foreach ($retry as &$v) {
130+
$v = new ConstStub($v ? 'RETRY' : 'NORETRY', $v);
131+
}
132+
} else {
133+
$retry = new ConstStub($retry ? 'RETRY' : 'NORETRY', $retry);
134+
}
135+
136+
$options += array(
137+
'TCP_KEEPALIVE' => \defined('Redis::OPT_TCP_KEEPALIVE') ? $redis->getOption(\Redis::OPT_TCP_KEEPALIVE) : 0,
138+
'READ_TIMEOUT' => $redis->getOption(\Redis::OPT_READ_TIMEOUT),
139+
'COMPRESSION' => $compression,
140+
'SERIALIZER' => $serializer,
141+
'PREFIX' => $redis->getOption(\Redis::OPT_PREFIX),
142+
'SCAN' => $retry,
143+
);
144+
145+
return new EnumStub($options);
67146
}
68147
}

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ abstract class AbstractCloner implements ClonerInterface
108108

109109
'Redis' => array('Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedis'),
110110
'RedisArray' => array('Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedisArray'),
111+
'RedisCluster' => array('Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedisCluster'),
111112

112113
'DateTimeInterface' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castDateTime'),
113114
'DateInterval' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castInterval'),

src/Symfony/Component/VarDumper/Tests/Caster/RedisCasterTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,15 @@ public function testConnected()
4949
host: "127.0.0.1"
5050
port: 6379
5151
auth: null
52+
mode: ATOMIC
5253
dbNum: 0
5354
timeout: 0.0
55+
lastError: null
5456
persistentId: null
5557
options: {
58+
TCP_KEEPALIVE: 0
5659
READ_TIMEOUT: 0.0
60+
COMPRESSION: NONE
5761
SERIALIZER: NONE
5862
PREFIX: null
5963
SCAN: NORETRY

0 commit comments

Comments
 (0)