1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Laravel Lodash package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Longman\LaravelLodash\Redis\Connectors; |
13
|
|
|
|
14
|
|
|
use Illuminate\Redis\Connections\PhpRedisClusterConnection; |
15
|
|
|
use Illuminate\Redis\Connectors\PhpRedisConnector as BasePhpRedisConnector; |
16
|
|
|
use Illuminate\Support\Arr; |
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
use Redis; |
19
|
|
|
use RedisArray; |
20
|
|
|
|
21
|
|
|
class PhpRedisConnector extends BasePhpRedisConnector |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Create the Redis client instance. |
25
|
|
|
* |
26
|
|
|
* @param array $config |
27
|
|
|
* @return \Redis |
28
|
|
|
*/ |
29
|
1 |
|
protected function createClient(array $config) |
30
|
|
|
{ |
31
|
|
|
return tap(new Redis(), function (Redis $client) use ($config) { |
32
|
1 |
|
$this->establishConnection($client, $config); |
33
|
|
|
|
34
|
1 |
|
if (! empty($config['password'])) { |
35
|
|
|
$client->auth((string) $config['password']); |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
if (! empty($config['database'])) { |
39
|
1 |
|
$client->select((int) $config['database']); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
if (! empty($config['prefix'])) { |
43
|
1 |
|
$client->setOption(Redis::OPT_PREFIX, (string) $config['prefix']); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
if (! empty($config['read_timeout'])) { |
47
|
1 |
|
$client->setOption(Redis::OPT_READ_TIMEOUT, (string) $config['read_timeout']); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
if (! empty($config['serializer'])) { |
51
|
1 |
|
$serializer = $this->getSerializerFromConfig($config['serializer']); |
52
|
1 |
|
$client->setOption(Redis::OPT_SERIALIZER, (string) $serializer); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
if (! empty($config['scan'])) { |
56
|
|
|
$client->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY); |
57
|
|
|
} |
58
|
1 |
|
}); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Create a new clustered PhpRedis connection. |
63
|
|
|
* |
64
|
|
|
* @param array $config |
65
|
|
|
* @param array $clusterOptions |
66
|
|
|
* @param array $options |
67
|
|
|
* @return \Illuminate\Redis\Connections\PhpRedisClusterConnection |
68
|
|
|
*/ |
69
|
1 |
|
public function connectToCluster(array $config, array $clusterOptions, array $options) |
70
|
|
|
{ |
71
|
1 |
|
$options = array_merge($options, $clusterOptions, Arr::pull($config, 'options', [])); |
72
|
|
|
|
73
|
|
|
// Use native Redis clustering |
74
|
1 |
|
if (Arr::get($options, 'cluster') === 'redis') { |
75
|
|
|
return new PhpRedisClusterConnection($this->createRedisClusterInstance( |
76
|
|
|
array_map([$this, 'buildClusterConnectionString'], $config), |
77
|
|
|
$options |
78
|
|
|
)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Use client-side sharding |
82
|
1 |
|
return new PhpRedisClusterConnection($this->createRedisArrayInstance( |
|
|
|
|
83
|
1 |
|
array_map([$this, 'buildRedisArrayConnectionString'], $config), |
84
|
|
|
$options |
85
|
|
|
)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Build a PhpRedis hosts array. |
90
|
|
|
* |
91
|
|
|
* @param array $server |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
1 |
|
protected function buildRedisArrayConnectionString(array $server) |
95
|
|
|
{ |
96
|
1 |
|
return $server['host'] . ':' . $server['port']; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Create a new redis array instance. |
101
|
|
|
* |
102
|
|
|
* @param array $servers |
103
|
|
|
* @param array $options |
104
|
|
|
* @return \RedisArray |
105
|
|
|
*/ |
106
|
1 |
|
protected function createRedisArrayInstance(array $servers, array $options) |
107
|
|
|
{ |
108
|
1 |
|
$client = new RedisArray($servers, Arr::only($options, [ |
109
|
1 |
|
'function', |
110
|
|
|
'previous', |
111
|
|
|
'retry_interval', |
112
|
|
|
'lazy_connect', |
113
|
|
|
'connect_timeout', |
114
|
|
|
'read_timeout', |
115
|
|
|
'algorithm', |
116
|
|
|
'consistent', |
117
|
|
|
'distributor', |
118
|
|
|
])); |
119
|
|
|
|
120
|
1 |
|
if (! empty($options['password'])) { |
121
|
|
|
// @TODO: Remove after this will be implemented |
122
|
|
|
// https://github.com/phpredis/phpredis/issues/1508 |
123
|
|
|
throw new InvalidArgumentException('RedisArray does not support authorization'); |
124
|
|
|
//$client->auth((string) $options['password']); |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
if (! empty($options['database'])) { |
128
|
1 |
|
$client->select((int) $options['database']); |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
if (! empty($options['prefix'])) { |
132
|
1 |
|
$client->setOption(Redis::OPT_PREFIX, (string) $options['prefix']); |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
if (! empty($options['serializer'])) { |
136
|
1 |
|
$serializer = $this->getSerializerFromConfig($options['serializer']); |
137
|
1 |
|
$client->setOption(Redis::OPT_SERIALIZER, (string) $serializer); |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
return $client; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Resolve serializer |
145
|
|
|
* |
146
|
|
|
* @param string $serializer |
147
|
|
|
* @return int |
148
|
|
|
* @throws \InvalidArgumentException |
149
|
|
|
*/ |
150
|
2 |
|
protected function getSerializerFromConfig(string $serializer): int |
151
|
|
|
{ |
152
|
2 |
|
switch ($serializer) { |
153
|
|
|
default: |
154
|
|
|
$flag = Redis::SERIALIZER_NONE; |
155
|
|
|
break; |
156
|
2 |
|
case 'igbinary': |
157
|
2 |
|
if (! defined('Redis::SERIALIZER_IGBINARY')) { |
158
|
|
|
throw new InvalidArgumentException('Error: phpredis was not compiled with igbinary support!'); |
159
|
|
|
} |
160
|
2 |
|
$flag = Redis::SERIALIZER_IGBINARY; |
161
|
2 |
|
break; |
162
|
|
|
case 'php': |
163
|
|
|
$flag = Redis::SERIALIZER_PHP; |
164
|
|
|
break; |
165
|
|
|
} |
166
|
|
|
|
167
|
2 |
|
return $flag; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: