-
-
Notifications
You must be signed in to change notification settings - Fork 995
Expand file tree
/
Copy pathParameters.php
More file actions
229 lines (199 loc) · 5.85 KB
/
Parameters.php
File metadata and controls
229 lines (199 loc) · 5.85 KB
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
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2026 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Connection;
use InvalidArgumentException;
use Predis\Retry\Retry;
use Predis\Retry\Strategy\NoBackoff;
/**
* Container for connection parameters used to initialize connections to Redis.
*
* {@inheritdoc}
*/
class Parameters implements ParametersInterface
{
protected static $defaults = [
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
'protocol' => 2,
];
/**
* Set of connection parameters already filtered
* for NULL or 0-length string values.
*
* @var array
*/
protected $parameters;
/**
* @var bool
*/
private $disabledRetry = true;
/**
* @param array $parameters Named array of connection parameters.
*/
public function __construct(array $parameters = [])
{
if (!array_key_exists('retry', $parameters)) {
// Retries disabled by default
static::$defaults['retry'] = new Retry(new NoBackoff(), 0);
} else {
$this->disabledRetry = false;
}
$this->parameters = $this->filter($parameters + static::$defaults);
}
/**
* Filters parameters removing entries with NULL or 0-length string values.
*
* @param array $parameters Array of parameters to be filtered
*
* @return array
*/
protected function filter(array $parameters)
{
return array_filter($parameters, static function ($value) {
return $value !== null && $value !== '';
});
}
/**
* Creates a new instance by supplying the initial parameters either in the
* form of an URI string or a named array.
*
* @param array|string $parameters Set of connection parameters.
*
* @return Parameters
*/
public static function create($parameters)
{
if (is_string($parameters)) {
$parameters = static::parse($parameters);
}
return new static($parameters ?: []);
}
/**
* Parses an URI string returning an array of connection parameters.
*
* When using the "redis" and "rediss" schemes the URI is parsed according
* to the rules defined by the provisional registration documents approved
* by IANA. If the URI has a password in its "user-information" part or a
* database number in the "path" part these values override the values of
* "password" and "database" if they are present in the "query" part.
*
* @see http://www.iana.org/assignments/uri-schemes/prov/redis
* @see http://www.iana.org/assignments/uri-schemes/prov/rediss
*
* @param string $uri URI string.
*
* @return array
* @throws InvalidArgumentException
*/
public static function parse($uri)
{
if (stripos($uri, 'unix://') === 0) {
// parse_url() can parse unix:/path/to/sock so we do not need the
// unix:///path/to/sock hack, we will support it anyway until 2.0.
$uri = str_ireplace('unix://', 'unix:', $uri);
}
if (!$parsed = parse_url($uri)) {
throw new InvalidArgumentException("Invalid parameters URI: $uri");
}
if (
isset($parsed['host'])
&& false !== strpos($parsed['host'], '[')
&& false !== strpos($parsed['host'], ']')
) {
$parsed['host'] = substr($parsed['host'], 1, -1);
}
if (isset($parsed['query'])) {
parse_str($parsed['query'], $queryarray);
unset($parsed['query']);
$parsed = array_merge($parsed, $queryarray);
}
if (stripos($uri, 'redis') === 0) {
if (isset($parsed['user'])) {
if (strlen($parsed['user'])) {
$parsed['username'] = $parsed['user'];
}
unset($parsed['user']);
}
if (isset($parsed['pass'])) {
if (strlen($parsed['pass'])) {
$parsed['password'] = $parsed['pass'];
}
unset($parsed['pass']);
}
if (isset($parsed['path']) && preg_match('/^\/(\d+)(\/.*)?/', $parsed['path'], $path)) {
$parsed['database'] = $path[1];
if (isset($path[2])) {
$parsed['path'] = $path[2];
} else {
unset($parsed['path']);
}
}
}
return $parsed;
}
/**
* {@inheritdoc}
*/
public function toArray()
{
return $this->parameters;
}
/**
* {@inheritdoc}
*/
public function __get($parameter)
{
if (isset($this->parameters[$parameter])) {
return $this->parameters[$parameter];
}
}
public function __set($parameter, $value)
{
$this->parameters[$parameter] = $value;
}
/**
* {@inheritdoc}
*/
public function __isset($parameter)
{
return isset($this->parameters[$parameter]);
}
/**
* {@inheritdoc}
*/
public function __toString()
{
if ($this->scheme === 'unix') {
return "$this->scheme:$this->path";
}
if (filter_var($this->host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
return "$this->scheme://[$this->host]:$this->port";
}
return "$this->scheme://$this->host:$this->port";
}
/**
* Returns if retries is disabled.
*
* @return bool
*/
public function isDisabledRetry(): bool
{
return $this->disabledRetry;
}
/**
* {@inheritdoc}
*/
public function __sleep()
{
return ['parameters'];
}
}