1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
declare(strict_types=1);
namespace Laminas\Cache\Storage\Adapter;
use Laminas\Cache\Exception\ExtensionNotLoadedException;
use Laminas\Cache\Exception\RuntimeException;
use Laminas\Cache\Storage\Adapter\Exception\RedisRuntimeException;
use Laminas\Cache\Storage\Plugin\PluginInterface;
use Laminas\Cache\Storage\Plugin\Serializer;
use Laminas\Cache\Storage\PluginCapableInterface;
use RedisCluster as RedisClusterFromExtension;
use RedisClusterException;
use function array_key_exists;
use function assert;
use function extension_loaded;
/**
* @psalm-type RedisClusterInfoType = array<string,mixed>&array{redis_version:string}
*/
final class RedisClusterResourceManager implements RedisClusterResourceManagerInterface
{
/** @var RedisClusterOptions */
private $options;
/** @psalm-var array<positive-int,mixed> */
private $libraryOptions = [];
public function __construct(RedisClusterOptions $options)
{
$this->options = $options;
if (! extension_loaded('redis')) {
throw new ExtensionNotLoadedException('Redis extension is not loaded');
}
}
public function getVersion(): string
{
$versionFromOptions = $this->options->getRedisVersion();
if ($versionFromOptions) {
return $versionFromOptions;
}
$resource = $this->getResource();
try {
$info = $this->info($resource);
} catch (RedisClusterException $exception) {
throw RedisRuntimeException::fromClusterException($exception, $resource);
}
$version = $info['redis_version'];
assert($version !== '');
$this->options->setRedisVersion($version);
return $version;
}
public function getResource(): RedisClusterFromExtension
{
try {
$resource = $this->createRedisResource($this->options);
} catch (RedisClusterException $exception) {
throw RedisRuntimeException::fromFailedConnection($exception);
}
$libraryOptions = $this->options->getLibOptions();
try {
$resource = $this->applyLibraryOptions($resource, $libraryOptions);
$this->libraryOptions = $this->mergeLibraryOptionsFromCluster($libraryOptions, $resource);
} catch (RedisClusterException $exception) {
throw RedisRuntimeException::fromClusterException($exception, $resource);
}
return $resource;
}
private function createRedisResource(RedisClusterOptions $options): RedisClusterFromExtension
{
if ($options->hasName()) {
return $this->createRedisResourceFromName(
$options->getName(),
$options->getTimeout(),
$options->getReadTimeout(),
$options->isPersistent(),
$options->getPassword()
);
}
$password = $options->getPassword();
if ($password === '') {
$password = null;
}
return new RedisClusterFromExtension(
null,
$options->getSeeds(),
$options->getTimeout(),
$options->getReadTimeout(),
$options->isPersistent(),
$password
);
}
/**
* @psalm-param non-empty-string $name
*/
private function createRedisResourceFromName(
string $name,
float $fallbackTimeout,
float $fallbackReadTimeout,
bool $persistent,
string $fallbackPassword
): RedisClusterFromExtension {
$options = new RedisClusterOptionsFromIni();
$seeds = $options->getSeeds($name);
$timeout = $options->getTimeout($name, $fallbackTimeout);
$readTimeout = $options->getReadTimeout($name, $fallbackReadTimeout);
$password = $options->getPasswordByName($name, $fallbackPassword);
return new RedisClusterFromExtension(
null,
$seeds,
$timeout,
$readTimeout,
$persistent,
$password
);
}
/**
* @psalm-param array<positive-int,mixed> $options
*/
private function applyLibraryOptions(
RedisClusterFromExtension $resource,
array $options
): RedisClusterFromExtension {
/** @psalm-suppress MixedAssignment */
foreach ($options as $option => $value) {
/**
* @see https://github.com/phpredis/phpredis#setoption
*
* @psalm-suppress InvalidArgument
* @psalm-suppress MixedArgument
*/
$resource->setOption($option, $value);
}
return $resource;
}
/**
* @psalm-param array<positive-int,mixed> $options
* @psalm-return array<positive-int,mixed>
*/
private function mergeLibraryOptionsFromCluster(array $options, RedisClusterFromExtension $resource): array
{
foreach (RedisClusterOptions::LIBRARY_OPTIONS as $option) {
if (array_key_exists($option, $options)) {
continue;
}
/**
* @see https://github.com/phpredis/phpredis#getoption
*
* @psalm-suppress InvalidArgument
*/
$options[$option] = $resource->getOption($option);
}
return $options;
}
/**
* @psalm-param RedisClusterOptions::OPT_* $option
* @return mixed
*/
public function getLibOption(int $option)
{
if (array_key_exists($option, $this->libraryOptions)) {
return $this->libraryOptions[$option];
}
/**
* @see https://github.com/phpredis/phpredis#getoption
*
* @psalm-suppress InvalidArgument
*/
return $this->libraryOptions[$option] = $this->getResource()->getOption($option);
}
public function hasSerializationSupport(PluginCapableInterface $adapter): bool
{
/**
* NOTE: we are not using {@see RedisClusterResourceManager::getLibOption} here
* as this would create a connection to redis even tho it wont be needed.
* Theoretically, it would be possible for upstream projects to receive the resource directly from the
* resource manager and then apply changes to it. As this is not the common use-case, this is not
* considered in this check.
*/
$options = $this->options;
$serializer = $options->getLibOption(
RedisClusterFromExtension::OPT_SERIALIZER,
RedisClusterFromExtension::SERIALIZER_NONE
);
if ($serializer !== RedisClusterFromExtension::SERIALIZER_NONE) {
return true;
}
/** @var iterable<PluginInterface> $plugins */
$plugins = $adapter->getPluginRegistry();
foreach ($plugins as $plugin) {
if (! $plugin instanceof Serializer) {
continue;
}
return true;
}
return false;
}
/**
* @psalm-return RedisClusterInfoType
*/
private function info(RedisClusterFromExtension $resource): array
{
if ($this->options->hasName()) {
$name = $this->options->getName();
/** @psalm-var RedisClusterInfoType $info */
$info = $resource->info($name);
return $info;
}
$seeds = $this->options->getSeeds();
if ($seeds === []) {
throw new RuntimeException('Neither the node name nor any seed is configured.');
}
$seed = $seeds[0];
/** @psalm-var RedisClusterInfoType $info */
$info = $resource->info($seed);
return $info;
}
}