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

Skip to content

Commit f95d943

Browse files
jschaedlnicolas-grekas
authored andcommitted
[VarDumper] add caster for Memcached
1 parent 9db435e commit f95d943

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-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+
* @author Jan Schädlich <[email protected]>
18+
*/
19+
class MemcachedCaster
20+
{
21+
private static $optionConstants;
22+
private static $defaultOptions;
23+
24+
public static function castMemcached(\Memcached $c, array $a, Stub $stub, $isNested)
25+
{
26+
$a += array(
27+
Caster::PREFIX_VIRTUAL.'servers' => $c->getServerList(),
28+
Caster::PREFIX_VIRTUAL.'options' => new EnumStub(
29+
self::getNonDefaultOptions($c)
30+
),
31+
);
32+
33+
return $a;
34+
}
35+
36+
private static function getNonDefaultOptions(\Memcached $c)
37+
{
38+
self::$defaultOptions = self::$defaultOptions ?? self::discoverDefaultOptions();
39+
self::$optionConstants = self::$optionConstants ?? self::getOptionConstants();
40+
41+
$nonDefaultOptions = array();
42+
foreach (self::$optionConstants as $constantKey => $value) {
43+
if (self::$defaultOptions[$constantKey] !== $option = $c->getOption($value)) {
44+
$nonDefaultOptions[$constantKey] = $option;
45+
}
46+
}
47+
48+
return $nonDefaultOptions;
49+
}
50+
51+
private static function discoverDefaultOptions()
52+
{
53+
$defaultMemcached = new \Memcached();
54+
$defaultMemcached->addServer('127.0.0.1', 11211);
55+
56+
$defaultOptions = array();
57+
self::$optionConstants = self::$optionConstants ?? self::getOptionConstants();
58+
59+
foreach (self::$optionConstants as $constantKey => $value) {
60+
$defaultOptions[$constantKey] = $defaultMemcached->getOption($value);
61+
}
62+
63+
return $defaultOptions;
64+
}
65+
66+
private static function getOptionConstants()
67+
{
68+
$reflectedMemcached = new \ReflectionClass(\Memcached::class);
69+
70+
$optionConstants = array();
71+
foreach ($reflectedMemcached->getConstants() as $constantKey => $value) {
72+
if (0 === strpos($constantKey, 'OPT_')) {
73+
$optionConstants[$constantKey] = $value;
74+
}
75+
}
76+
77+
return $optionConstants;
78+
}
79+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ abstract class AbstractCloner implements ClonerInterface
123123
'IntlCalendar' => array('Symfony\Component\VarDumper\Caster\IntlCaster', 'castIntlCalendar'),
124124
'IntlDateFormatter' => array('Symfony\Component\VarDumper\Caster\IntlCaster', 'castIntlDateFormatter'),
125125

126+
'Memcached' => array('Symfony\Component\VarDumper\Caster\MemcachedCaster', 'castMemcached'),
127+
126128
':curl' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'),
127129
':dba' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),
128130
':dba persistent' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 PHPUnit\Framework\TestCase;
15+
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
16+
17+
/**
18+
* @author Jan Schädlich <[email protected]>
19+
*/
20+
class MemcachedCasterTest extends TestCase
21+
{
22+
use VarDumperTestTrait;
23+
24+
public function testCastMemcachedWithDefaultOptions()
25+
{
26+
if (!class_exists('Memcached')) {
27+
$this->markTestSkipped('Memcached not available');
28+
}
29+
30+
$var = new \Memcached();
31+
$var->addServer('127.0.0.1', 11211);
32+
$var->addServer('127.0.0.2', 11212);
33+
34+
$expected = <<<EOTXT
35+
Memcached {
36+
servers: array:2 [
37+
0 => array:3 [
38+
"host" => "127.0.0.1"
39+
"port" => 11211
40+
"type" => "TCP"
41+
]
42+
1 => array:3 [
43+
"host" => "127.0.0.2"
44+
"port" => 11212
45+
"type" => "TCP"
46+
]
47+
]
48+
options: {}
49+
}
50+
EOTXT;
51+
$this->assertDumpEquals($expected, $var);
52+
}
53+
54+
public function testCastMemcachedWithCustomOptions()
55+
{
56+
if (!class_exists('Memcached')) {
57+
$this->markTestSkipped('Memcached not available');
58+
}
59+
60+
$var = new \Memcached();
61+
$var->addServer('127.0.0.1', 11211);
62+
$var->addServer('127.0.0.2', 11212);
63+
64+
// set a subset of non default options to test boolean, string and integer output
65+
$var->setOption(\Memcached::OPT_COMPRESSION, false);
66+
$var->setOption(\Memcached::OPT_PREFIX_KEY, 'pre');
67+
$var->setOption(\Memcached::OPT_DISTRIBUTION, \Memcached::DISTRIBUTION_CONSISTENT);
68+
69+
$expected = <<<'EOTXT'
70+
Memcached {
71+
servers: array:2 [
72+
0 => array:3 [
73+
"host" => "127.0.0.1"
74+
"port" => 11211
75+
"type" => "TCP"
76+
]
77+
1 => array:3 [
78+
"host" => "127.0.0.2"
79+
"port" => 11212
80+
"type" => "TCP"
81+
]
82+
]
83+
options: {
84+
OPT_COMPRESSION: false
85+
OPT_PREFIX_KEY: "pre"
86+
OPT_DISTRIBUTION: 1
87+
}
88+
}
89+
EOTXT;
90+
91+
$this->assertDumpEquals($expected, $var);
92+
}
93+
}

0 commit comments

Comments
 (0)