Nenhuma Descrição

RedisCaster.php 2.4KB

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