-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathRedisConnectionFactory.php
167 lines (139 loc) · 5.02 KB
/
RedisConnectionFactory.php
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
<?php
declare(strict_types=1);
namespace Enqueue\Redis;
use Interop\Queue\PsrConnectionFactory;
use Interop\Queue\PsrContext;
class RedisConnectionFactory implements PsrConnectionFactory
{
/**
* @var array
*/
private $config;
/**
* @var \Redis
*/
private $redis;
/**
* $config = [
* 'host' => can be a host, or the path to a unix domain socket
* 'port' => optional
* 'timeout' => value in seconds (optional, default is 0.0 meaning unlimited)
* 'reserved' => should be null if $retry_interval is specified
* 'retry_interval' => retry interval in milliseconds.
* 'vendor' => 'The library used internally to interact with Redis server
* 'redis' => 'Used only if vendor is custom, should contain an instance of \Enqueue\Redis\Redis interface.
* 'persisted' => bool, Whether it use single persisted connection or open a new one for every context
* 'lazy' => the connection will be performed as later as possible, if the option set to true
* 'database' => Database index to select when connected (default value: 0)
* user - The user name to use.
* pass - Password.
* ].
*
* or
*
* redis:
* redis:?vendor=predis
*
* or
*
* instance of Enqueue\Redis
*
* @param array|string|Redis|null $config
*/
public function __construct($config = 'redis:')
{
if ($config instanceof Redis) {
$this->redis = $config;
$this->config = $this->defaultConfig();
return;
}
if (empty($config) || 'redis:' === $config) {
$config = [];
} elseif (is_string($config)) {
$config = $this->parseDsn($config);
} elseif (is_array($config)) {
} else {
throw new \LogicException('The config must be either an array of options, a DSN string or null');
}
$this->config = array_replace($this->defaultConfig(), $config);
$supportedVendors = ['predis', 'phpredis', 'custom'];
if (false == in_array($this->config['vendor'], $supportedVendors, true)) {
throw new \LogicException(sprintf(
'Unsupported redis vendor given. It must be either "%s". Got "%s"',
implode('", "', $supportedVendors),
$this->config['vendor']
));
}
}
/**
* @return RedisContext
*/
public function createContext(): PsrContext
{
if ($this->config['lazy']) {
return new RedisContext(function () {
return $this->createRedis();
});
}
return new RedisContext($this->createRedis());
}
private function createRedis(): Redis
{
if (false == $this->redis) {
if ('phpredis' == $this->config['vendor'] && false == $this->redis) {
$this->redis = new PhpRedis($this->config);
}
if ('predis' == $this->config['vendor'] && false == $this->redis) {
$this->redis = new PRedis($this->config);
}
if ('custom' == $this->config['vendor'] && false == $this->redis) {
if (empty($this->config['redis'])) {
throw new \LogicException('The redis option should be set if vendor is custom.');
}
if (false == $this->config['redis'] instanceof Redis) {
throw new \LogicException(sprintf('The redis option should be instance of "%s".', Redis::class));
}
$this->redis = $this->config['redis'];
}
$this->redis->connect();
}
return $this->redis;
}
private function parseDsn(string $dsn): array
{
if ((false === strpos($dsn, 'redis:')) and (false === strpos($dsn, 'rediss:'))) {
throw new \LogicException(sprintf('The given DSN "%s" is not supported. Must start with "redis:" or "rediss:".', $dsn));
}
if (false === $config = parse_url($dsn)) {
throw new \LogicException(sprintf('Failed to parse DSN "%s"', $dsn));
}
if (array_key_exists('port', $config)) {
$config['port'] = (int) $config['port'];
}
if ($query = parse_url($dsn, PHP_URL_QUERY)) {
$queryConfig = [];
parse_str($query, $queryConfig);
$config = array_replace($queryConfig, $config);
}
unset($config['query']);
$config['lazy'] = empty($config['lazy']) ? false : true;
$config['persisted'] = empty($config['persisted']) ? false : true;
return $config;
}
private function defaultConfig(): array
{
return [
'host' => 'localhost',
'scheme' => 'redis',
'port' => 6379,
'timeout' => null,
'reserved' => null,
'retry_interval' => null,
'vendor' => 'phpredis',
'redis' => null,
'persisted' => false,
'lazy' => true,
'database' => 0,
];
}
}