blob: eac25a12a8b56f082ab0be4814a52243a43fda9e [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\VarDumper\Caster;
13
14use Symfony\Component\VarDumper\Cloner\Stub;
15
16/**
17 * Casts Redis class from ext-redis to array representation.
18 *
19 * @author Nicolas Grekas <p@tchwork.com>
20 *
21 * @final
22 */
23class 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
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100105 private static function getRedisOptions(\Redis|\RedisArray|\RedisCluster $redis, array $options = []): EnumStub
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200106 {
107 $serializer = $redis->getOption(\Redis::OPT_SERIALIZER);
108 if (\is_array($serializer)) {
109 foreach ($serializer as &$v) {
110 if (isset(self::SERIALIZERS[$v])) {
111 $v = new ConstStub(self::SERIALIZERS[$v], $v);
112 }
113 }
114 } elseif (isset(self::SERIALIZERS[$serializer])) {
115 $serializer = new ConstStub(self::SERIALIZERS[$serializer], $serializer);
116 }
117
118 $compression = \defined('Redis::OPT_COMPRESSION') ? $redis->getOption(\Redis::OPT_COMPRESSION) : 0;
119 if (\is_array($compression)) {
120 foreach ($compression as &$v) {
121 if (isset(self::COMPRESSION_MODES[$v])) {
122 $v = new ConstStub(self::COMPRESSION_MODES[$v], $v);
123 }
124 }
125 } elseif (isset(self::COMPRESSION_MODES[$compression])) {
126 $compression = new ConstStub(self::COMPRESSION_MODES[$compression], $compression);
127 }
128
129 $retry = \defined('Redis::OPT_SCAN') ? $redis->getOption(\Redis::OPT_SCAN) : 0;
130 if (\is_array($retry)) {
131 foreach ($retry as &$v) {
132 $v = new ConstStub($v ? 'RETRY' : 'NORETRY', $v);
133 }
134 } else {
135 $retry = new ConstStub($retry ? 'RETRY' : 'NORETRY', $retry);
136 }
137
138 $options += [
139 'TCP_KEEPALIVE' => \defined('Redis::OPT_TCP_KEEPALIVE') ? $redis->getOption(\Redis::OPT_TCP_KEEPALIVE) : 0,
140 'READ_TIMEOUT' => $redis->getOption(\Redis::OPT_READ_TIMEOUT),
141 'COMPRESSION' => $compression,
142 'SERIALIZER' => $serializer,
143 'PREFIX' => $redis->getOption(\Redis::OPT_PREFIX),
144 'SCAN' => $retry,
145 ];
146
147 return new EnumStub($options);
148 }
149}