Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e79b572

Browse files
[VarDumper] Add Redis caster
1 parent 731d7a6 commit e79b572

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Caster;
13+
14+
use Symfony\Component\VarDumper\Cloner\Stub;
15+
16+
/**
17+
* Casts Redis class from ext-redis to array representation.
18+
*
19+
* @author Nicolas Grekas <[email protected]>
20+
*/
21+
class RedisCaster
22+
{
23+
private static $serializer = array(
24+
\Redis::SERIALIZER_NONE => 'NONE',
25+
\Redis::SERIALIZER_PHP => 'PHP',
26+
2 => 'IGBINARY', // Optional Redis::SERIALIZER_IGBINARY
27+
);
28+
29+
public static function castRedis(\Redis $c, array $a, Stub $stub, $isNested)
30+
{
31+
$prefix = Caster::PREFIX_VIRTUAL;
32+
33+
if (defined('HHVM_VERSION_ID')) {
34+
$ser = $a[Caster::PREFIX_PROTECTED.'serializer'];
35+
$a[Caster::PREFIX_PROTECTED.'serializer'] = isset(self::$serializer[$ser]) ? new ConstStub(self::$serializer[$ser], $ser) : $ser;
36+
37+
return $a;
38+
}
39+
40+
if (!$connected = $c->isConnected()) {
41+
return $a + array(
42+
$prefix.'isConnected' => $connected,
43+
);
44+
}
45+
46+
$ser = $c->getOption(\Redis::OPT_SERIALIZER);
47+
$retry = defined('Redis::OPT_SCAN') ? $c->getOption(\Redis::OPT_SCAN) : 0;
48+
49+
$a += array(
50+
$prefix.'isConnected' => $connected,
51+
$prefix.'host' => $c->getHost(),
52+
$prefix.'port' => $c->getPort(),
53+
$prefix.'auth' => $c->getAuth(),
54+
$prefix.'dbNum' => $c->getDbNum(),
55+
$prefix.'timeout' => $c->getTimeout(),
56+
$prefix.'persistentId' => $c->getPersistentID(),
57+
$prefix.'options' => new EnumStub(array(
58+
'READ_TIMEOUT' => $c->getOption(\Redis::OPT_READ_TIMEOUT),
59+
'SERIALIZER' => isset(self::$serializer[$ser]) ? new ConstStub(self::$serializer[$ser], $ser) : $ser,
60+
'PREFIX' => $c->getOption(\Redis::OPT_PREFIX),
61+
'SCAN' => new ConstStub($retry ? 'RETRY' : 'NORETRY', $retry),
62+
)),
63+
);
64+
65+
return $a;
66+
}
67+
68+
public static function castRedisArray(\RedisArray $c, array $a, Stub $stub, $isNested)
69+
{
70+
$prefix = Caster::PREFIX_VIRTUAL;
71+
72+
$a += array(
73+
$prefix.'hosts' => $c->_hosts(),
74+
$prefix.'function' => $c->_function(),
75+
);
76+
77+
return $a;
78+
}
79+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ abstract class AbstractCloner implements ClonerInterface
100100

101101
'MongoCursorInterface' => 'Symfony\Component\VarDumper\Caster\MongoCaster::castCursor',
102102

103+
'Redis' => 'Symfony\Component\VarDumper\Caster\RedisCaster::castRedis',
104+
'RedisArray' => 'Symfony\Component\VarDumper\Caster\RedisCaster::castRedisArray',
105+
103106
':curl' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castCurl',
104107
':dba' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castDba',
105108
':dba persistent' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castDba',
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Tests\Caster;
13+
14+
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
15+
16+
/**
17+
* @author Nicolas Grekas <[email protected]>
18+
* @requires extension redis
19+
*/
20+
class RedisCasterTest extends \PHPUnit_Framework_TestCase
21+
{
22+
use VarDumperTestTrait;
23+
24+
public function testNotConnected()
25+
{
26+
$redis = new \Redis();
27+
28+
if (defined('HHVM_VERSION_ID')) {
29+
$xCast = <<<'EODUMP'
30+
Redis {
31+
#host: ""
32+
%A
33+
}
34+
EODUMP;
35+
} else {
36+
$xCast = <<<'EODUMP'
37+
Redis {
38+
isConnected: false
39+
}
40+
EODUMP;
41+
}
42+
43+
$this->assertDumpMatchesFormat($xCast, $redis);
44+
}
45+
46+
public function testConnected()
47+
{
48+
$redis = new \Redis();
49+
if (!@$redis->connect('127.0.0.1')) {
50+
$e = error_get_last();
51+
self::markTestSkipped($e['message']);
52+
}
53+
54+
if (defined('HHVM_VERSION_ID')) {
55+
$xCast = <<<'EODUMP'
56+
Redis {
57+
#host: "127.0.0.1"
58+
%A
59+
}
60+
EODUMP;
61+
} else {
62+
$xCast = <<<'EODUMP'
63+
Redis {
64+
+"socket": Redis Socket Buffer resource
65+
isConnected: true
66+
host: "127.0.0.1"
67+
port: 6379
68+
auth: null
69+
dbNum: 0
70+
timeout: 0.0
71+
persistentId: null
72+
options: {
73+
READ_TIMEOUT: 0.0
74+
SERIALIZER: NONE
75+
PREFIX: null
76+
SCAN: NORETRY
77+
}
78+
}
79+
EODUMP;
80+
}
81+
82+
$this->assertDumpMatchesFormat($xCast, $redis);
83+
}
84+
}

0 commit comments

Comments
 (0)