RedisCaster.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 (!$connected = $c->isConnected()) {
  28. return $a + array(
  29. $prefix.'isConnected' => $connected,
  30. );
  31. }
  32. $ser = $c->getOption(\Redis::OPT_SERIALIZER);
  33. $retry = \defined('Redis::OPT_SCAN') ? $c->getOption(\Redis::OPT_SCAN) : 0;
  34. return $a + array(
  35. $prefix.'isConnected' => $connected,
  36. $prefix.'host' => $c->getHost(),
  37. $prefix.'port' => $c->getPort(),
  38. $prefix.'auth' => $c->getAuth(),
  39. $prefix.'dbNum' => $c->getDbNum(),
  40. $prefix.'timeout' => $c->getTimeout(),
  41. $prefix.'persistentId' => $c->getPersistentID(),
  42. $prefix.'options' => new EnumStub(array(
  43. 'READ_TIMEOUT' => $c->getOption(\Redis::OPT_READ_TIMEOUT),
  44. 'SERIALIZER' => isset(self::$serializer[$ser]) ? new ConstStub(self::$serializer[$ser], $ser) : $ser,
  45. 'PREFIX' => $c->getOption(\Redis::OPT_PREFIX),
  46. 'SCAN' => new ConstStub($retry ? 'RETRY' : 'NORETRY', $retry),
  47. )),
  48. );
  49. }
  50. public static function castRedisArray(\RedisArray $c, array $a, Stub $stub, $isNested)
  51. {
  52. $prefix = Caster::PREFIX_VIRTUAL;
  53. return $a + array(
  54. $prefix.'hosts' => $c->_hosts(),
  55. $prefix.'function' => ClassStub::wrapCallable($c->_function()),
  56. );
  57. }
  58. }