Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #28622 [VarDumper] add caster for Memcached (jschaedl)
This PR was squashed before being merged into the 4.2-dev branch (closes #28622). Discussion ---------- [VarDumper] add caster for Memcached | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #28597 | License | MIT | Doc PR | - Commits ------- f95d943 [VarDumper] add caster for Memcached
- Loading branch information
Showing
3 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
src/Symfony/Component/VarDumper/Caster/MemcachedCaster.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\VarDumper\Caster; | ||
|
||
use Symfony\Component\VarDumper\Cloner\Stub; | ||
|
||
/** | ||
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de> | ||
*/ | ||
class MemcachedCaster | ||
{ | ||
private static $optionConstants; | ||
private static $defaultOptions; | ||
|
||
public static function castMemcached(\Memcached $c, array $a, Stub $stub, $isNested) | ||
{ | ||
$a += array( | ||
Caster::PREFIX_VIRTUAL.'servers' => $c->getServerList(), | ||
Caster::PREFIX_VIRTUAL.'options' => new EnumStub( | ||
self::getNonDefaultOptions($c) | ||
), | ||
); | ||
|
||
return $a; | ||
} | ||
|
||
private static function getNonDefaultOptions(\Memcached $c) | ||
{ | ||
self::$defaultOptions = self::$defaultOptions ?? self::discoverDefaultOptions(); | ||
self::$optionConstants = self::$optionConstants ?? self::getOptionConstants(); | ||
|
||
$nonDefaultOptions = array(); | ||
foreach (self::$optionConstants as $constantKey => $value) { | ||
if (self::$defaultOptions[$constantKey] !== $option = $c->getOption($value)) { | ||
$nonDefaultOptions[$constantKey] = $option; | ||
} | ||
} | ||
|
||
return $nonDefaultOptions; | ||
} | ||
|
||
private static function discoverDefaultOptions() | ||
{ | ||
$defaultMemcached = new \Memcached(); | ||
$defaultMemcached->addServer('127.0.0.1', 11211); | ||
|
||
$defaultOptions = array(); | ||
self::$optionConstants = self::$optionConstants ?? self::getOptionConstants(); | ||
|
||
foreach (self::$optionConstants as $constantKey => $value) { | ||
$defaultOptions[$constantKey] = $defaultMemcached->getOption($value); | ||
} | ||
|
||
return $defaultOptions; | ||
} | ||
|
||
private static function getOptionConstants() | ||
{ | ||
$reflectedMemcached = new \ReflectionClass(\Memcached::class); | ||
|
||
$optionConstants = array(); | ||
foreach ($reflectedMemcached->getConstants() as $constantKey => $value) { | ||
if (0 === strpos($constantKey, 'OPT_')) { | ||
$optionConstants[$constantKey] = $value; | ||
} | ||
} | ||
|
||
return $optionConstants; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/Symfony/Component/VarDumper/Tests/Caster/MemcachedCasterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\VarDumper\Tests\Caster; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\VarDumper\Test\VarDumperTestTrait; | ||
|
||
/** | ||
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de> | ||
*/ | ||
class MemcachedCasterTest extends TestCase | ||
{ | ||
use VarDumperTestTrait; | ||
|
||
public function testCastMemcachedWithDefaultOptions() | ||
{ | ||
if (!class_exists('Memcached')) { | ||
$this->markTestSkipped('Memcached not available'); | ||
} | ||
|
||
$var = new \Memcached(); | ||
$var->addServer('127.0.0.1', 11211); | ||
$var->addServer('127.0.0.2', 11212); | ||
|
||
$expected = <<<EOTXT | ||
Memcached { | ||
servers: array:2 [ | ||
0 => array:3 [ | ||
"host" => "127.0.0.1" | ||
"port" => 11211 | ||
"type" => "TCP" | ||
] | ||
1 => array:3 [ | ||
"host" => "127.0.0.2" | ||
"port" => 11212 | ||
"type" => "TCP" | ||
] | ||
] | ||
options: {} | ||
} | ||
EOTXT; | ||
$this->assertDumpEquals($expected, $var); | ||
} | ||
|
||
public function testCastMemcachedWithCustomOptions() | ||
{ | ||
if (!class_exists('Memcached')) { | ||
$this->markTestSkipped('Memcached not available'); | ||
} | ||
|
||
$var = new \Memcached(); | ||
$var->addServer('127.0.0.1', 11211); | ||
$var->addServer('127.0.0.2', 11212); | ||
|
||
// set a subset of non default options to test boolean, string and integer output | ||
$var->setOption(\Memcached::OPT_COMPRESSION, false); | ||
$var->setOption(\Memcached::OPT_PREFIX_KEY, 'pre'); | ||
$var->setOption(\Memcached::OPT_DISTRIBUTION, \Memcached::DISTRIBUTION_CONSISTENT); | ||
|
||
$expected = <<<'EOTXT' | ||
Memcached { | ||
servers: array:2 [ | ||
0 => array:3 [ | ||
"host" => "127.0.0.1" | ||
"port" => 11211 | ||
"type" => "TCP" | ||
] | ||
1 => array:3 [ | ||
"host" => "127.0.0.2" | ||
"port" => 11212 | ||
"type" => "TCP" | ||
] | ||
] | ||
options: { | ||
OPT_COMPRESSION: false | ||
OPT_PREFIX_KEY: "pre" | ||
OPT_DISTRIBUTION: 1 | ||
} | ||
} | ||
EOTXT; | ||
|
||
$this->assertDumpEquals($expected, $var); | ||
} | ||
} |