8000 [VarDumper] make RedisCaster handle RedisCluster and dump all options on all drivers by nicolas-grekas · Pull Request #28264 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[VarDumper] make RedisCaster handle RedisCluster and dump all options on all drivers #28264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 87 additions & 8 deletions src/Symfony/Component/VarDumper/Caster/RedisCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ class RedisCaster
2 => 'IGBINARY', // Optional Redis::SERIALIZER_IGBINARY
);

private static $mode = array(
\Redis::ATOMIC => 'ATOMIC',
\Redis::MULTI => 'MULTI',
\Redis::PIPELINE => 'PIPELINE',
);

private static $compression = array(
0 => 'NONE', // Redis::COMPRESSION_NONE
1 => 'LZF', // Redis::COMPRESSION_LZF
);

private static $failover = array(
\RedisCluster::FAILOVER_NONE => 'NONE',
\RedisCluster::FAILOVER_ERROR => 'ERROR',
\RedisCluster::FAILOVER_DISTRIBUTE => 'DISTRIBUTE',
\RedisCluster::FAILOVER_DISTRIBUTE_SLAVES => 'DISTRIBUTE_SLAVES',
);

public static function castRedis(\Redis $c, array $a, Stub $stub, $isNested)
{
$prefix = Caster::PREFIX_VIRTUAL;
Expand All @@ -36,23 +54,19 @@ public static function castRedis(\Redis $c, array $a, Stub $stub, $isNested)
);
}

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

return $a + array(
$prefix.'isConnected' => $connected,
$prefix.'host' => $c->getHost(),
$prefix.'port' => $c->getPort(),
$prefix.'auth' => $c->getAuth(),
$prefix.'mode' => isset(self::$mode[$mode]) ? new ConstStub(self::$mode[$mode], $mode) : $mode,
$prefix.'dbNum' => $c->getDbNum(),
$prefix.'timeout' => $c->getTimeout(),
$prefix.'lastError' => $c->getLastError(),
$prefix.'persistentId' => $c->getPersistentID(),
$prefix.'options' => new EnumStub(array(
'READ_TIMEOUT' => $c->getOption(\Redis::OPT_READ_TIMEOUT),
'SERIALIZER' => isset(self::$serializer[$ser]) ? new ConstStub(self::$serializer[$ser], $ser) : $ser,
'PREFIX' => $c->getOption(\Redis::OPT_PREFIX),
'SCAN' => new ConstStub($retry ? 'RETRY' : 'NORETRY', $retry),
)),
$prefix.'options' => self::getRedisOptions($c),
);
}

Expand All @@ -63,6 +77,71 @@ public static function castRedisArray(\RedisArray $c, array $a, Stub $stub, $isN
return $a + array(
$prefix.'hosts' => $c->_hosts(),
$prefix.'function' => ClassStub::wrapCallable($c->_function()),
$prefix.'lastError' => $c->getLastError(),
$prefix.'options' => self::getRedisOptions($c),
);
}

public static function castRedisCluster(\RedisCluster $c, array $a, Stub $stub, $isNested)
{
$prefix = Caster::PREFIX_VIRTUAL;
$failover = $c->getOption(\RedisCluster::OPT_SLAVE_FAILOVER);

$a += array(
$prefix.'_masters' => $c->_masters(),
$prefix.'_redir' => $c->_redir(),
$prefix.'mode' => new ConstStub($c->getMode() ? 'MULTI' : 'ATOMIC', $c->getMode()),
$prefix.'lastError' => $c->getLastError(),
$prefix.'options' => self::getRedisOptions($c, array(
'SLAVE_FAILOVER' => isset(self::$failover[$failover]) ? new ConstStub(self::$failover[$failover], $failover) : $failover,
)),
);

return $a;
}

/**
* @param \Redis|\RedisArray|\RedisCluster
*/
private static function getRedisOptions($redis, array $options = array()): EnumStub
{
if (\is_array($serializer = $redis->getOption(\Redis::OPT_SERIALIZER))) {
foreach ($serializer as &$v) {
if (isset(self::$serializer[$v])) {
$v = new ConstStub(self::$serializer[$v], $v);
}
}
} elseif (isset(self::$serializer[$serializer])) {
$serializer = new ConstStub(self::$serializer[$serializer], $serializer);
}

if (\is_array($compression = \defined('Redis::OPT_COMPRESSION') ? $redis->getOption(\Redis::OPT_COMPRESSION) : 0)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think keeping the assignment $compression = \defined('Redis::OPT_COMPRESSION') ? $redis->getOption(\Redis::OPT_COMPRESSION) : 0 outside the if (\is_array($compression)) would be more readable (and totally equivalent).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated thanks

foreach ($compression as &$v) {
if (isset(self::$compression[$v])) {
$v = new ConstStub(self::$compression[$v], $v);
}
}
} elseif (isset(self::$compression[$compression])) {
$compression = new ConstStub(self::$compression[$compression], $compression);
}

if (\is_array($retry = \defined('Redis::OPT_SCAN') ? $redis->getOption(\Redis::OPT_SCAN) : 0)) {
foreach ($retry as &$v) {
$v = new ConstStub($v ? 'RETRY' : 'NORETRY', $v);
}
} else {
$retry = new ConstStub($retry ? 'RETRY' : 'NORETRY', $retry);
}

$options += array(
'TCP_KEEPALIVE' => \defined('Redis::OPT_TCP_KEEPALIVE') ? $redis->getOption(\Redis::OPT_TCP_KEEPALIVE) : 0,
'READ_TIMEOUT' => $redis->getOption(\Redis::OPT_READ_TIMEOUT),
'COMPRESSION' => $compression,
'SERIALIZER' => $serializer,
'PREFIX' => $redis->getOption(\Redis::OPT_PREFIX),
'SCAN' => $retry,
);

return new EnumStub($options);
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ abstract class AbstractCloner implements ClonerInterface

'Redis' => array('Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedis'),
'RedisArray' => array('Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedisArray'),
'RedisCluster' => array('Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedisCluster'),

'DateTimeInterface' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castDateTime'),
'DateInterval' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castInterval'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ public function testConnected()
host: "127.0.0.1"
port: 6379
auth: null
mode: ATOMIC
dbNum: 0
timeout: 0.0
lastError: null
persistentId: null
options: {
TCP_KEEPALIVE: 0
READ_TIMEOUT: 0.0
COMPRESSION: NONE
SERIALIZER: NONE
PREFIX: null
SCAN: NORETRY
Expand Down
0