菜谱项目

RedisCaster.php 2.5KB

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