1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Monospice\LaravelRedisSentinel; |
4
|
|
|
|
5
|
|
|
use Illuminate\Redis\RedisManager; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Monospice\LaravelRedisSentinel\Connectors; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Enables Laravel's Redis database driver to accept configuration options for |
12
|
|
|
* Redis Sentinel connections independently. |
13
|
|
|
* |
14
|
|
|
* By default, Laravel's Redis service permits a single set of configuration |
15
|
|
|
* options for all of the Redis connections passed to the Predis client. This |
16
|
|
|
* prevents us from declaring separate parameters for individual Redis services |
17
|
|
|
* managed by Sentinel. For example, we may wish to connect to a separate Redis |
18
|
|
|
* Sentinel service cluster set, or use a separate Redis database, for caching |
19
|
|
|
* queues, and sessions. This wrapper class enables us to declare parameters |
20
|
|
|
* for each connection in the "redis-sentinel" block of the database |
21
|
|
|
* configuration which it will use to configure individual clients. |
22
|
|
|
* |
23
|
|
|
* @category Package |
24
|
|
|
* @package Monospice\LaravelRedisSentinel |
25
|
|
|
* @author Cy Rossignol <[email protected]> |
26
|
|
|
* @license See LICENSE file |
27
|
|
|
* @link http://github.com/monospice/laravel-redis-sentinel-driver |
28
|
|
|
*/ |
29
|
|
|
class RedisSentinelManager extends RedisManager |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Get the Redis Connection instance represented by the specified name |
33
|
|
|
* |
34
|
|
|
* @param string $name The name of the connection as defined in the |
35
|
|
|
* configuration |
36
|
|
|
* |
37
|
|
|
* @return \Illuminate\Redis\Connections\Connection The configured Redis |
|
|
|
|
38
|
|
|
* Connection instance |
39
|
|
|
* |
40
|
|
|
* @throws InvalidArgumentException If attempting to initialize a Redis |
41
|
|
|
* Cluster connection |
42
|
|
|
* @throws InvalidArgumentException If the specified connection is not |
43
|
|
|
* defined in the configuration |
44
|
|
|
*/ |
45
|
|
|
protected function resolve($name) |
46
|
|
|
{ |
47
|
|
|
$options = Arr::get($this->config, 'options', [ ]); |
48
|
|
|
|
49
|
|
|
if (isset($this->config[$name])) { |
50
|
|
|
return $this->connector()->connect($this->config[$name], $options); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (isset($this->config['clusters']['name'])) { |
54
|
|
|
throw new InvalidArgumentException( |
55
|
|
|
'Redis Sentinel connections do not support Redis Cluster.' |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
throw new InvalidArgumentException( |
60
|
|
|
"The Redis Sentinel connection [$name] is not defined." |
|
|
|
|
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the appropriate Connector instance for the current client driver |
66
|
|
|
* |
67
|
|
|
* @return Connectors\PredisConnector The Connector instance for the |
68
|
|
|
* current driver |
69
|
|
|
*/ |
70
|
|
|
protected function connector() |
71
|
|
|
{ |
72
|
|
|
switch ($this->driver) { |
73
|
|
|
case 'predis': |
74
|
|
|
return new Connectors\PredisConnector(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
throw new InvalidArgumentException( |
78
|
|
|
'The monospice/laravel-redis-sentinel-drivers package currently ' |
79
|
|
|
. 'supports only the "predis" client. Support for the "phpredis" ' |
80
|
|
|
. 'client will be added in the future.' |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.