1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Monospice\LaravelRedisSentinel\Manager; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Container\Container; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Monospice\LaravelRedisSentinel\Configuration\Loader as ConfigurationLoader; |
8
|
|
|
use Monospice\LaravelRedisSentinel\RedisSentinelManager; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Creates instances of the core Redis Sentinel connection manager for the |
12
|
|
|
* current version of the framework. |
13
|
|
|
* |
14
|
|
|
* @category Package |
15
|
|
|
* @package Monospice\LaravelRedisSentinel |
16
|
|
|
* @author Cy Rossignol <[email protected]> |
17
|
|
|
* @license See LICENSE file |
18
|
|
|
* @link https://github.com/monospice/laravel-redis-sentinel-drivers |
19
|
|
|
*/ |
20
|
|
|
class VersionedManagerFactory |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The current application instance that Laravel's RedisManager depends on |
24
|
|
|
* in version 5.7+. |
25
|
|
|
* |
26
|
|
|
* @var Container |
27
|
|
|
*/ |
28
|
|
|
protected $app; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Detects the application version and provides configuration values. |
32
|
|
|
* |
33
|
|
|
* @var ConfigurationLoader |
34
|
|
|
*/ |
35
|
|
|
protected $config; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a factory using the provided configuration. |
39
|
|
|
* |
40
|
|
|
* @param Container $app The current application instance that |
41
|
|
|
* Laravel's RedisManager depends on in version 5.7+. |
42
|
|
|
* @param ConfigurationLoader $config Detects the application version and |
43
|
|
|
* provides configuration values. |
44
|
|
|
*/ |
45
|
|
|
public function __construct(Container $app, ConfigurationLoader $config) |
46
|
|
|
{ |
47
|
|
|
$this->app = $app; |
48
|
|
|
$this->config = $config; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create an instance of the package's core Redis Sentinel service. |
53
|
|
|
* |
54
|
|
|
* @param Container $app The current application instance that |
55
|
|
|
* Laravel's RedisManager depends on in version 5.7+. |
56
|
|
|
* @param ConfigurationLoader $config Detects the application version and |
57
|
|
|
* provides configuration values. |
58
|
|
|
* |
59
|
|
|
* @return \Monospice\LaravelRedisSentinel\Contracts\Factory A configured |
60
|
|
|
* Redis Sentinel connection manager. |
61
|
|
|
*/ |
62
|
|
|
public static function make(Container $app, ConfigurationLoader $config) |
63
|
|
|
{ |
64
|
|
|
return (new static($app, $config))->makeInstance(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Create an instance of the package's core Redis Sentinel service. |
69
|
|
|
* |
70
|
|
|
* @return \Monospice\LaravelRedisSentinel\Contracts\Factory A configured |
71
|
|
|
* Redis Sentinel connection manager. |
72
|
|
|
*/ |
73
|
|
|
public function makeInstance() |
74
|
|
|
{ |
75
|
|
|
$class = $this->getVersionedRedisSentinelManagerClass(); |
76
|
|
|
$config = $this->config->get('database.redis-sentinel', [ ]); |
77
|
|
|
$driver = Arr::pull($config, 'client', 'predis'); |
78
|
|
|
|
79
|
|
|
// Laravel 5.7 introduced the app as the first parameter: |
80
|
|
|
if ($this->appVersionLessThan('5.7')) { |
81
|
|
|
return new RedisSentinelManager(new $class($driver, $config)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return new RedisSentinelManager(new $class($this->app, $driver, $config)); |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the fully-qualified class name of the RedisSentinelManager class |
90
|
|
|
* for the current version of Laravel or Lumen. |
91
|
|
|
* |
92
|
|
|
* @return string The class name of the appropriate RedisSentinelManager |
93
|
|
|
* with its namespace. |
94
|
|
|
*/ |
95
|
|
|
protected function getVersionedRedisSentinelManagerClass() |
96
|
|
|
{ |
97
|
|
|
if ($this->config->isLumen) { |
98
|
|
|
$frameworkVersion = '5.4'; |
99
|
|
|
} else { |
100
|
|
|
$frameworkVersion = '5.4.20'; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($this->appVersionLessThan($frameworkVersion)) { |
104
|
|
|
return Laravel540RedisSentinelManager::class; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return Laravel5420RedisSentinelManager::class; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Determine whether the current Laravel framework version is less than the |
112
|
|
|
* specified version. |
113
|
|
|
* |
114
|
|
|
* @param string $version The version to compare to the current version. |
115
|
|
|
* |
116
|
|
|
* @return bool TRUE current framework version is less than the specified |
117
|
|
|
* version. |
118
|
|
|
*/ |
119
|
|
|
protected function appVersionLessThan($version) |
120
|
|
|
{ |
121
|
|
|
$appVersion = $this->config->getApplicationVersion(); |
122
|
|
|
|
123
|
|
|
return version_compare($appVersion, $version, 'lt'); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|