1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Monospice\LaravelRedisSentinel; |
4
|
|
|
|
5
|
|
|
use Illuminate\Broadcasting\Broadcasters\RedisBroadcaster; |
6
|
|
|
use Illuminate\Contracts\Broadcasting\Factory as BroadcastFactory; |
7
|
|
|
use Illuminate\Cache\RedisStore; |
8
|
|
|
use Illuminate\Queue\Connectors\RedisConnector; |
9
|
|
|
use Illuminate\Session\CacheBasedSessionHandler; |
10
|
|
|
use Illuminate\Support\Arr; |
11
|
|
|
use Illuminate\Support\ServiceProvider; |
12
|
|
|
use Monospice\LaravelRedisSentinel\Configuration\Loader as ConfigurationLoader; |
13
|
|
|
use Monospice\LaravelRedisSentinel\RedisSentinelManager; |
14
|
|
|
use Monospice\LaravelRedisSentinel\Manager; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Registers the "redis-sentinel" driver as an available driver for Laravel's |
18
|
|
|
* cache, session, and queue services and loads the appropriate configuration. |
19
|
|
|
* |
20
|
|
|
* @category Package |
21
|
|
|
* @package Monospice\LaravelRedisSentinel |
22
|
|
|
* @author Cy Rossignol <[email protected]> |
23
|
|
|
* @license See LICENSE file |
24
|
|
|
* @link https://github.com/monospice/laravel-redis-sentinel-drivers |
25
|
|
|
*/ |
26
|
|
|
class RedisSentinelServiceProvider extends ServiceProvider |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Loads the package's configuration and provides configuration values. |
30
|
|
|
* |
31
|
|
|
* @var ConfigurationLoader |
32
|
|
|
*/ |
33
|
|
|
protected $config; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Boot the service by registering extensions with Laravel's cache, queue, |
37
|
|
|
* and session managers for the "redis-sentinel" driver. |
38
|
|
|
* |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function boot() |
42
|
|
|
{ |
43
|
|
|
$this->addRedisSentinelBroadcaster(); |
44
|
|
|
$this->addRedisSentinelCacheStore(); |
45
|
|
|
$this->addRedisSentinelQueueConnector(); |
46
|
|
|
|
47
|
|
|
// Since version 5.2, Lumen does not include support for sessions by |
48
|
|
|
// default, so we'll only register the session handler if enabled: |
49
|
|
|
if ($this->config->supportsSessions) { |
50
|
|
|
$this->addRedisSentinelSessionHandler(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// If we want Laravel's Redis API to use Sentinel, we'll remove the |
54
|
|
|
// "redis" service from the deferred services in the container: |
55
|
|
|
if ($this->config->shouldOverrideLaravelRedisApi()) { |
56
|
|
|
$this->removeDeferredRedisServices(); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Bind the "redis-sentinel" database driver to the application service |
62
|
|
|
* container. |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function register() |
67
|
|
|
{ |
68
|
|
|
$this->config = ConfigurationLoader::load($this->app); |
|
|
|
|
69
|
|
|
|
70
|
|
|
$this->app->singleton('redis-sentinel', function ($app) { |
71
|
|
|
$class = $this->getVersionedRedisSentinelManagerClass(); |
72
|
|
|
$config = $app->make('config')->get('database.redis-sentinel', [ ]); |
73
|
|
|
$driver = Arr::pull($config, 'client', 'predis'); |
74
|
|
|
|
75
|
|
|
return new RedisSentinelManager(new $class($driver, $config)); |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
|
// If we want Laravel's Redis API to use Sentinel, we'll return an |
79
|
|
|
// instance of the RedisSentinelManager when requesting the "redis" |
80
|
|
|
// service: |
81
|
|
|
if ($this->config->shouldOverrideLaravelRedisApi()) { |
82
|
|
|
$this->registerOverrides(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Replace the standard Laravel Redis service with the Redis Sentinel |
88
|
|
|
* database driver so all Redis operations use Sentinel connections. |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
protected function registerOverrides() |
93
|
|
|
{ |
94
|
|
|
$this->app->singleton('redis', function ($app) { |
95
|
|
|
return $app->make('redis-sentinel'); |
96
|
|
|
}); |
97
|
|
|
|
98
|
|
|
$this->app->bind('redis.connection', function ($app) { |
99
|
|
|
return $app->make('redis-sentinel')->connection(); |
100
|
|
|
}); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Remove the standard Laravel Redis service from the bound deferred |
105
|
|
|
* services so they don't overwrite Redis Sentinel registrations. |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
protected function removeDeferredRedisServices() |
110
|
|
|
{ |
111
|
|
|
if ($this->config->isLumen) { |
112
|
|
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$deferredServices = $this->app->getDeferredServices(); |
116
|
|
|
|
117
|
|
|
unset($deferredServices['redis']); |
118
|
|
|
unset($deferredServices['redis.connection']); |
119
|
|
|
|
120
|
|
|
$this->app->setDeferredServices($deferredServices); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Add "redis-sentinel" as an available brodcaster option to the Laravel |
125
|
|
|
* event broadcasting manager. |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
protected function addRedisSentinelBroadcaster() |
130
|
|
|
{ |
131
|
|
|
$this->app->make(BroadcastFactory::class) |
132
|
|
|
->extend('redis-sentinel', function ($app, $conf) { |
133
|
|
|
$redis = $app->make('redis-sentinel')->getVersionedManager(); |
134
|
|
|
$connection = Arr::get($conf, 'connection', 'default'); |
135
|
|
|
|
136
|
|
|
return new RedisBroadcaster($redis, $connection); |
137
|
|
|
}); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Add "redis-sentinel" as an available cache store option to the Laravel |
142
|
|
|
* cache manager. |
143
|
|
|
* |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
protected function addRedisSentinelCacheStore() |
147
|
|
|
{ |
148
|
|
|
$this->app->make('cache') |
149
|
|
|
->extend('redis-sentinel', function ($app, $conf) { |
150
|
|
|
$redis = $app->make('redis-sentinel')->getVersionedManager(); |
151
|
|
|
$prefix = $app->make('config')->get('cache.prefix'); |
152
|
|
|
$connection = Arr::get($conf, 'connection', 'default'); |
153
|
|
|
$store = new RedisStore($redis, $prefix, $connection); |
154
|
|
|
|
155
|
|
|
return $this->repository($store); |
|
|
|
|
156
|
|
|
}); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Add "redis-sentinel" as an available driver option to the Laravel |
161
|
|
|
* session manager. |
162
|
|
|
* |
163
|
|
|
* @return void |
164
|
|
|
*/ |
165
|
|
|
protected function addRedisSentinelSessionHandler() |
166
|
|
|
{ |
167
|
|
|
$this->app->make('session')->extend('redis-sentinel', function ($app) { |
168
|
|
|
$config = $app->make('config'); |
169
|
|
|
$cacheDriver = clone $app->make('cache')->driver('redis-sentinel'); |
170
|
|
|
$minutes = $config->get('session.lifetime'); |
171
|
|
|
$connection = $config->get('session.connection'); |
172
|
|
|
|
173
|
|
|
$cacheDriver->getStore()->setConnection($connection); |
174
|
|
|
|
175
|
|
|
return new CacheBasedSessionHandler($cacheDriver, $minutes); |
176
|
|
|
}); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Add "redis-sentinel" as an available queue connection driver option to |
181
|
|
|
* the Laravel queue manager. |
182
|
|
|
* |
183
|
|
|
* @return void |
184
|
|
|
*/ |
185
|
|
|
protected function addRedisSentinelQueueConnector() |
186
|
|
|
{ |
187
|
|
|
$this->app->make('queue')->extend('redis-sentinel', function () { |
188
|
|
|
$redis = $this->app->make('redis-sentinel')->getVersionedManager(); |
189
|
|
|
|
190
|
|
|
return new RedisConnector($redis); |
191
|
|
|
}); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get the fully-qualified class name of the RedisSentinelManager class |
196
|
|
|
* for the current version of Laravel or Lumen. |
197
|
|
|
* |
198
|
|
|
* @return string The class name of the appropriate RedisSentinelManager |
199
|
|
|
* with its namespace |
200
|
|
|
*/ |
201
|
|
|
protected function getVersionedRedisSentinelManagerClass() |
202
|
|
|
{ |
203
|
|
|
if ($this->config->isLumen) { |
204
|
|
|
$appVersion = substr($this->app->version(), 7, 3); // ex. "5.4" |
205
|
|
|
$frameworkVersion = '5.4'; |
206
|
|
|
} else { |
207
|
|
|
$appVersion = \Illuminate\Foundation\Application::VERSION; |
208
|
|
|
$frameworkVersion = '5.4.20'; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if (version_compare($appVersion, $frameworkVersion, 'lt')) { |
212
|
|
|
return Manager\Laravel540RedisSentinelManager::class; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return Manager\Laravel5420RedisSentinelManager::class; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
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: