1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Monospice\LaravelRedisSentinel\Manager; |
4
|
|
|
|
5
|
|
|
use Illuminate\Redis\RedisManager; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Monospice\LaravelRedisSentinel\Connectors; |
9
|
|
|
use Monospice\LaravelRedisSentinel\Contracts\Factory; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Contains common functionality for the RedisSentinelManager implementations |
13
|
|
|
* for differing Laravel versions. |
14
|
|
|
* |
15
|
|
|
* @category Package |
16
|
|
|
* @package Monospice\LaravelRedisSentinel |
17
|
|
|
* @author Cy Rossignol <[email protected]> |
18
|
|
|
* @license See LICENSE file |
19
|
|
|
* @link http://github.com/monospice/laravel-redis-sentinel-drivers |
20
|
|
|
*/ |
21
|
|
|
abstract class VersionedRedisSentinelManager |
22
|
|
|
extends RedisManager |
23
|
|
|
implements Factory |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Get the Redis Connection instance represented by the specified name |
27
|
|
|
* |
28
|
|
|
* @param string|null $name The name of the connection as defined in the |
29
|
|
|
* configuration |
30
|
|
|
* |
31
|
|
|
* @return \Illuminate\Redis\Connections\PredisConnection The configured |
|
|
|
|
32
|
|
|
* Redis Connection instance |
33
|
|
|
* |
34
|
|
|
* @throws InvalidArgumentException If attempting to initialize a Redis |
35
|
|
|
* Cluster connection |
36
|
|
|
* @throws InvalidArgumentException If the specified connection is not |
37
|
|
|
* defined in the configuration |
38
|
|
|
*/ |
39
|
|
|
protected function resolveConnection($name = null) |
40
|
|
|
{ |
41
|
|
|
$name = $name ?: 'default'; |
42
|
|
|
$options = Arr::get($this->config, 'options', [ ]); |
43
|
|
|
|
44
|
|
|
if (isset($this->config[$name])) { |
45
|
|
|
return $this->connector()->connect($this->config[$name], $options); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (isset($this->config['clusters']['name'])) { |
49
|
|
|
throw new InvalidArgumentException( |
50
|
|
|
'Redis Sentinel connections do not support Redis Cluster.' |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
throw new InvalidArgumentException( |
55
|
|
|
'The Redis Sentinel connection [' . $name . '] is not defined.' |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the appropriate Connector instance for the current client driver |
61
|
|
|
* |
62
|
|
|
* @return Connectors\PredisConnector The Connector instance for the |
63
|
|
|
* current driver |
64
|
|
|
* |
65
|
|
|
* @throws InvalidArgumentException If the current client driver is not |
66
|
|
|
* supported |
67
|
|
|
*/ |
68
|
|
|
protected function connector() |
69
|
|
|
{ |
70
|
|
|
switch ($this->driver) { |
71
|
|
|
case 'predis': |
72
|
|
|
return new Connectors\PredisConnector(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
throw new InvalidArgumentException( |
76
|
|
|
'Unsupported Redis Sentinel client driver [' . $this->driver . ']. ' |
77
|
|
|
. 'The monospice/laravel-redis-sentinel-drivers package currently ' |
78
|
|
|
. 'supports only the "predis" client. Support for the "phpredis" ' |
79
|
|
|
. 'client will be added in the future.' |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
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.