From f95d943cc42b3b0e527625e00a1c7b430d789362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4dlich?= Date: Thu, 27 Sep 2018 17:00:30 +0200 Subject: [PATCH] [VarDumper] add caster for Memcached --- .../VarDumper/Caster/MemcachedCaster.php | 79 ++++++++++++++++ .../VarDumper/Cloner/AbstractCloner.php | 2 + .../Tests/Caster/MemcachedCasterTest.php | 93 +++++++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 src/Symfony/Component/VarDumper/Caster/MemcachedCaster.php create mode 100644 src/Symfony/Component/VarDumper/Tests/Caster/MemcachedCasterTest.php diff --git a/src/Symfony/Component/VarDumper/Caster/MemcachedCaster.php b/src/Symfony/Component/VarDumper/Caster/MemcachedCaster.php new file mode 100644 index 000000000000..99a33bca636b --- /dev/null +++ b/src/Symfony/Component/VarDumper/Caster/MemcachedCaster.php @@ -0,0 +1,79 @@ + + * + * 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 + */ +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; + } +} diff --git a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php index a6628bf7980e..38891f1d80e5 100644 --- a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php +++ b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php @@ -123,6 +123,8 @@ abstract class AbstractCloner implements ClonerInterface 'IntlCalendar' => array('Symfony\Component\VarDumper\Caster\IntlCaster', 'castIntlCalendar'), 'IntlDateFormatter' => array('Symfony\Component\VarDumper\Caster\IntlCaster', 'castIntlDateFormatter'), + 'Memcached' => array('Symfony\Component\VarDumper\Caster\MemcachedCaster', 'castMemcached'), + ':curl' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'), ':dba' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'), ':dba persistent' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'), diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/MemcachedCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/MemcachedCasterTest.php new file mode 100644 index 000000000000..df48390af9ab --- /dev/null +++ b/src/Symfony/Component/VarDumper/Tests/Caster/MemcachedCasterTest.php @@ -0,0 +1,93 @@ + + * + * 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 + */ +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 = << 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); + } +}