[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <[email protected]> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 namespace Symfony\Component\VarDumper\Caster; 13 14 use Symfony\Component\VarDumper\Cloner\Stub; 15 16 /** 17 * Casts Redis class from ext-redis to array representation. 18 * 19 * @author Nicolas Grekas <[email protected]> 20 * 21 * @final 22 */ 23 class RedisCaster 24 { 25 private const SERIALIZERS = [ 26 \Redis::SERIALIZER_NONE => 'NONE', 27 \Redis::SERIALIZER_PHP => 'PHP', 28 2 => 'IGBINARY', // Optional Redis::SERIALIZER_IGBINARY 29 ]; 30 31 private const MODES = [ 32 \Redis::ATOMIC => 'ATOMIC', 33 \Redis::MULTI => 'MULTI', 34 \Redis::PIPELINE => 'PIPELINE', 35 ]; 36 37 private const COMPRESSION_MODES = [ 38 0 => 'NONE', // Redis::COMPRESSION_NONE 39 1 => 'LZF', // Redis::COMPRESSION_LZF 40 ]; 41 42 private const FAILOVER_OPTIONS = [ 43 \RedisCluster::FAILOVER_NONE => 'NONE', 44 \RedisCluster::FAILOVER_ERROR => 'ERROR', 45 \RedisCluster::FAILOVER_DISTRIBUTE => 'DISTRIBUTE', 46 \RedisCluster::FAILOVER_DISTRIBUTE_SLAVES => 'DISTRIBUTE_SLAVES', 47 ]; 48 49 public static function castRedis(\Redis $c, array $a, Stub $stub, bool $isNested) 50 { 51 $prefix = Caster::PREFIX_VIRTUAL; 52 53 if (!$connected = $c->isConnected()) { 54 return $a + [ 55 $prefix.'isConnected' => $connected, 56 ]; 57 } 58 59 $mode = $c->getMode(); 60 61 return $a + [ 62 $prefix.'isConnected' => $connected, 63 $prefix.'host' => $c->getHost(), 64 $prefix.'port' => $c->getPort(), 65 $prefix.'auth' => $c->getAuth(), 66 $prefix.'mode' => isset(self::MODES[$mode]) ? new ConstStub(self::MODES[$mode], $mode) : $mode, 67 $prefix.'dbNum' => $c->getDbNum(), 68 $prefix.'timeout' => $c->getTimeout(), 69 $prefix.'lastError' => $c->getLastError(), 70 $prefix.'persistentId' => $c->getPersistentID(), 71 $prefix.'options' => self::getRedisOptions($c), 72 ]; 73 } 74 75 public static function castRedisArray(\RedisArray $c, array $a, Stub $stub, bool $isNested) 76 { 77 $prefix = Caster::PREFIX_VIRTUAL; 78 79 return $a + [ 80 $prefix.'hosts' => $c->_hosts(), 81 $prefix.'function' => ClassStub::wrapCallable($c->_function()), 82 $prefix.'lastError' => $c->getLastError(), 83 $prefix.'options' => self::getRedisOptions($c), 84 ]; 85 } 86 87 public static function castRedisCluster(\RedisCluster $c, array $a, Stub $stub, bool $isNested) 88 { 89 $prefix = Caster::PREFIX_VIRTUAL; 90 $failover = $c->getOption(\RedisCluster::OPT_SLAVE_FAILOVER); 91 92 $a += [ 93 $prefix.'_masters' => $c->_masters(), 94 $prefix.'_redir' => $c->_redir(), 95 $prefix.'mode' => new ConstStub($c->getMode() ? 'MULTI' : 'ATOMIC', $c->getMode()), 96 $prefix.'lastError' => $c->getLastError(), 97 $prefix.'options' => self::getRedisOptions($c, [ 98 'SLAVE_FAILOVER' => isset(self::FAILOVER_OPTIONS[$failover]) ? new ConstStub(self::FAILOVER_OPTIONS[$failover], $failover) : $failover, 99 ]), 100 ]; 101 102 return $a; 103 } 104 105 /** 106 * @param \Redis|\RedisArray|\RedisCluster $redis 107 */ 108 private static function getRedisOptions($redis, array $options = []): EnumStub 109 { 110 $serializer = $redis->getOption(\Redis::OPT_SERIALIZER); 111 if (\is_array($serializer)) { 112 foreach ($serializer as &$v) { 113 if (isset(self::SERIALIZERS[$v])) { 114 $v = new ConstStub(self::SERIALIZERS[$v], $v); 115 } 116 } 117 } elseif (isset(self::SERIALIZERS[$serializer])) { 118 $serializer = new ConstStub(self::SERIALIZERS[$serializer], $serializer); 119 } 120 121 $compression = \defined('Redis::OPT_COMPRESSION') ? $redis->getOption(\Redis::OPT_COMPRESSION) : 0; 122 if (\is_array($compression)) { 123 foreach ($compression as &$v) { 124 if (isset(self::COMPRESSION_MODES[$v])) { 125 $v = new ConstStub(self::COMPRESSION_MODES[$v], $v); 126 } 127 } 128 } elseif (isset(self::COMPRESSION_MODES[$compression])) { 129 $compression = new ConstStub(self::COMPRESSION_MODES[$compression], $compression); 130 } 131 132 $retry = \defined('Redis::OPT_SCAN') ? $redis->getOption(\Redis::OPT_SCAN) : 0; 133 if (\is_array($retry)) { 134 foreach ($retry as &$v) { 135 $v = new ConstStub($v ? 'RETRY' : 'NORETRY', $v); 136 } 137 } else { 138 $retry = new ConstStub($retry ? 'RETRY' : 'NORETRY', $retry); 139 } 140 141 $options += [ 142 'TCP_KEEPALIVE' => \defined('Redis::OPT_TCP_KEEPALIVE') ? $redis->getOption(\Redis::OPT_TCP_KEEPALIVE) : 0, 143 'READ_TIMEOUT' => $redis->getOption(\Redis::OPT_READ_TIMEOUT), 144 'COMPRESSION' => $compression, 145 'SERIALIZER' => $serializer, 146 'PREFIX' => $redis->getOption(\Redis::OPT_PREFIX), 147 'SCAN' => $retry, 148 ]; 149 150 return new EnumStub($options); 151 } 152 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |