8000 [VarDumper] introduce reflection to discover memcached constants · symfony/symfony@2ee39b8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ee39b8

Browse files
committed
[VarDumper] introduce reflection to discover memcached constants
1 parent 792efb3 commit 2ee39b8

File tree

2 files changed

+95
-99
lines changed

2 files changed

+95
-99
lines changed

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

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,54 @@ public static function castMemcached(\Memcached $c, array $a, Stub $stub, $isNes
2323
$a += array(
2424
Caster::PREFIX_VIRTUAL.'servers' => $c->getServerList(),
2525
Caster::PREFIX_VIRTUAL.'options' => new EnumStub(
26-
self::getNonDefaultValueOptions($c)
26+
self::getMemcachedNonDefaultValueOptions($c)
2727
),
2828
);
2929

3030
return $a;
3131
}
3232

33-
private static function getNonDefaultValueOptions(\Memcached $c)
33+
public static function getMemcachedNonDefaultValueOptions(\Memcached $c)
3434
{
35-
$options = array();
35+
$defaultOptions = self::discoverDefaultMemcachedOptions();
36+
$optionConstants = self::getMemcachedOptionConstants();
3637

37-
$reflectedMemcached = new \ReflectionClass(\Memcached::class);
38-
39-
foreach ($reflectedMemcached->getConstants() as $constantKey => $value) {
40-
if ('OPT_' === substr($constantKey, 0, 4)) {
41-
$options[$constantKey] = self::getOptionValue($c, $value);
38+
$nonDefaultOptions = array();
39+
foreach ($optionConstants as $constantKey => $value) {
40+
if ($defaultOptions[$constantKey] !== $option = $c->getOption($value)) {
41+
$nonDefaultOptions[$constantKey] = $option;
4242
}
4343
}
4444

45-
return array_filter($options, function ($option) {
46-
return !is_null($option) ?? $option;
47-
});
45+
return $nonDefaultOptions;
4846
}
4947

50-
private static function getOptionValue(\Memcached $c, int $option)
48+
public static function discoverDefaultMemcachedOptions()
5149
{
5250
$defaultMemcached = new \Memcached();
5351
$defaultMemcached->addServer('127.0.0.1', 11211);
5452

55-
$defaultOption = $defaultMemcached->getOption($option);
56-
$currentOption = $c->getOption($option);
53+
$defaultOptions = array();
54+
$optionConstants = self::getMemcachedOptionConstants();
55+
56+
foreach ($optionConstants as $constantKey => $value) {
57+
$defaultOptions[$constantKey] = $defaultMemcached->getOption($value);
58+
}
59+
60+
return $defaultOptions;
61+
}
62+
63+
public static function getMemcachedOptionConstants()
64+
{
65+
$reflectedMemcached = new \ReflectionClass(\Memcached::class);
66+
67+
$optionConstants = array();
68+
foreach ($reflectedMemcached->getConstants() as $constantKey => $value) {
69+
if ('OPT_' === substr($constantKey, 0, 4)) {
70+
$optionConstants[$constantKey] = $value;
71+
}
72+
}
5773

58-
return $defaultOption !== $currentOption ? $currentOption : null;
74+
return $optionConstants;
5975
}
6076
}

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

Lines changed: 64 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\VarDumper\Tests\Caster;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\VarDumper\Caster\MemcachedCaster;
1516
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
1617

1718
class MemcachedCasterTest extends TestCase
@@ -25,30 +26,21 @@ public function testCastMemcachedWithDefaultOptions()
2526
}
2627

2728
$var = new \Memcached();
28-
29-
$expectedServerHost1 = '127.0.0.1';
30-
$expectedServerPort1 = 11211;
31-
$expectedConnectionType1 = 'TCP';
32-
33-
$expectedServerHost2 = '127.0.0.2';
34-
$expectedServerPort2 = 11211;
35-
$expectedConnectionType2 = 'TCP';
36-
37-
$var->addServer($expectedServerHost1, $expectedServerPort1);
38-
$var->addServer($expectedServerHost2, $expectedServerPort2);
29+
$var->addServer('127.0.0.1', 11211);
30+
$var->addServer('127.0.0.2', 11212);
3931

4032
$expected = <<<EOTXT
4133
Memcached {
4234
servers: array:2 [
4335
0 => array:3 [
44-
"host" => "$expectedServerHost1"
45-
"port" => $expectedServerPort1
46-
"type" => "$expectedConnectionType1"
36+
"host" => "127.0.0.1"
37+
"port" => 11211
38+
"type" => "TCP"
4739
]
4840
1 => array:3 [
49-
"host" => "$expectedServerHost2"
50-
"port" => $expectedServerPort2
51-
"type" => "$expectedConnectionType2"
41+
"host" => "127.0.0.2"
42+
"port" => 11212
43+
"type" => "TCP"
5244
]
5345
]
5446
options: {}
@@ -64,71 +56,27 @@ public function testCastMemcachedWithCustomOptions()
6456
}
6557

6658
$var = new \Memcached();
59+
$var->addServer('127.0.0.1', 11211);
60+
$var->addServer('127.0.0.2', 11212);
6761

68-
$expectedServerHost1 = '127.0.0.1';
69-
$expectedServerPort1 = 11211;
70-
$expectedConnectionType1 = 'TCP';
71-
72-
$expectedServerHost2 = '127.0.0.2';
73-
$expectedServerPort2 = 11211;
74-
$expectedConnectionType2 = 'TCP';
75-
76-
$var->addServer($expectedServerHost1, $expectedServerPort1);
77-
$var->addServer($expectedServerHost2, $expectedServerPort2);
78-
79-
$var->setOptions(
80-
array(
81-
\Memcached::OPT_COMPRESSION => false,
82-
\Memcached::OPT_COMPRESSION_TYPE => false,
83-
//\Memcached::OPT_SERIALIZER => \Memcached::SERIALIZER_IGBINARY, // throws Memcached::setOptions(): invalid serializer provided
84-
\Memcached::OPT_PREFIX_KEY => 'prefix',
85-
\Memcached::OPT_HASH => \Memcached::HASH_MD5,
86-
\Memcached::OPT_DISTRIBUTION => \Memcached::DISTRIBUTION_CONSISTENT,
87-
\Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
88-
\Memcached::OPT_BUFFER_WRITES => true,
89-
\Memcached::OPT_BINARY_PROTOCOL => true,
90-
\Memcached::OPT_NO_BLOCK => true,
91-
\Memcached::OPT_TCP_NODELAY => true,
92-
\Memcached::OPT_SOCKET_SEND_SIZE => 100, // varies by platform/kernel
93-
\Memcached::OPT_SOCKET_RECV_SIZE => 100, // varies by platform/kernel
94-
\Memcached::OPT_CONNECT_TIMEOUT => 100,
95-
\Memcached::OPT_RETRY_TIMEOUT => 100,
96-
\Memcached::OPT_DEAD_TIMEOUT => 100,
97-
\Memcached::OPT_SEND_TIMEOUT => 100,
98-
\Memcached::OPT_RECV_TIMEOUT => 100,
99-
\Memcached::OPT_POLL_TIMEOUT => 100,
100-
//\Memcached::OPT_CACHE_LOOKUPS => true, // option deprecated
101-
\Memcached::OPT_SERVER_FAILURE_LIMIT => 1,
102-
)
103-
);
104-
105-
$expectedOptions = array();
106-
107-
$reflectedMemcached = new \ReflectionClass(\Memcached::class);
62+
$var->setOptions($this->getCustomOptions());
10863

109-
foreach ($reflectedMemcached->getConstants() as $constantKey => $value) {
110-
if (substr($constantKey, 0, 4) === 'OPT_') {
111-
if (null === $option = $this->getOptionValue($var, $value)) {
112-
continue;
113-
}
114-
$expectedOptions[] = ' '.$constantKey.': '.$this->getOptionValue($var, $value);
115-
}
116-
}
64+
$expectedOptions = $this->getExpectedOptions($var);
11765

11866
$expectedOptionsAsString = implode(PHP_EOL, $expectedOptions);
11967

12068
$expected = <<<EOTXT
12169
Memcached {
12270
servers: array:2 [
12371
0 => array:3 [
124-
"host" => "$expectedServerHost1"
125-
"port" => $expectedServerPort1
126-
"type" => "$expectedConnectionType1"
72+
"host" => "127.0.0.1"
73+
"port" => 11211
74+
"type" => "TCP"
12775
]
12876
1 => array:3 [
129-
"host" => "$expectedServerHost2"
130-
"port" => $expectedServerPort2
131-
"type" => "$expectedConnectionType2"
77+
"host" => "127.0.0.2"
78+
"port" => 11212
79+
"type" => "TCP"
13280
]
13381
]
13482
options: {
@@ -140,26 +88,58 @@ public function testCastMemcachedWithCustomOptions()
14088
$this->assertDumpEquals($expected, $var);
14189
}
14290

143-
private function getOptionValue(\Memcached $c, int $option)
91+
private function getCustomOptions()
14492
{
145-
$defaultMemcached = new \Memcached();
146-
$defaultMemcached->addServer('127.0.0.1', 11211);
93+
$optionsToIgnore = array(
94+
\Memcached::OPT_SERIALIZER, // trying to set this option to something else then default throws Memcached::setOptions(): invalid serializer provided
95+
\Memcached::OPT_CACHE_LOOKUPS, // option is deprecated
96+
\Memcached::OPT_VERIFY_KEY, // trying to set this option throws Memcached::setOption(): error setting memcached option: INVALID ARGUMENTS
97+
);
14798

148-
$defaultOption = $defaultMemcached->getOption($option);
149-
$currentOption = $c->getOption($option);
99+
$optionConstants = MemcachedCaster::getMemcachedOptionConstants();
100+
$defaultOptions = MemcachedCaster::discoverDefaultMemcachedOptions();
101+
$customOptions = array();
150102

151-
if ($defaultOption === $currentOption) {
152-
return null;
153-
}
103+
foreach ($optionConstants as $optionConstantKey => $optionConstantValue) {
104+
if (\in_array($optionConstantValue, $optionsToIgnore, true)) {
105+
continue;
106+
}
107+
108+
$defaultOptionValue = $defaultOptions[$optionConstantKey];
154109

155-
if (is_bool($currentOption)) {
156-
return $currentOption ? 'true' : 'false';
110+
if (\is_bool($defaultOptionValue)) {
111+
$customOptions[$optionConstants[$optionConstantKey]] = !$defaultOptionValue;
112+
}
113+
114+
if (\is_string($defaultOptionValue)) {
115+
$customOptions[$optionConstants[$optionConstantKey]] = 'custom';
116+
}
117+
118+
if (\is_int($defaultOptionValue)) {
119+
$customOptions[$optionConstants[$optionConstantKey]] = $defaultOptionValue + 1;
120+
}
157121
}
158122

159-
if (is_string($currentOption)) {
160-
return '"'.$currentOption.'"';
123+
return $customOptions;
124+
}
125+
126+
private function getExpectedOptions(\Memcached $memcached)
127+
{
128+
$expectedOptions = array();
129+
$nonDefaultOptions = MemcachedCaster::getMemcachedNonDefaultValueOptions($memcached);
130+
131+
foreach ($nonDefaultOptions as $key => $value) {
132+
if (\is_string($value)) {
133+
$value = '"'.$value.'"';
134+
}
135+
136+
if (\is_bool($value)) {
137+
$value = $value ? 'true' : 'false';
138+
}
139+
140+
$expectedOptions[] = ' '.$key.': '.$value;
161141
}
162142

163-
return $currentOption;
143+
return $expectedOptions;
164144
}
165145
}

0 commit comments

Comments
 (0)
0