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->bootComponentDrivers(); |
44
|
|
|
|
45
|
|
|
// If we want Laravel's Redis API to use Sentinel, we'll remove the |
46
|
|
|
// "redis" service from the deferred services in the container: |
47
|
|
|
if ($this->config->shouldOverrideLaravelRedisApi()) { |
48
|
|
|
$this->removeDeferredRedisServices(); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Bind the "redis-sentinel" database driver to the application service |
54
|
|
|
* container. |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function register() |
59
|
|
|
{ |
60
|
|
|
$this->config = ConfigurationLoader::load($this->app); |
|
|
|
|
61
|
|
|
|
62
|
|
|
$this->app->singleton('redis-sentinel', function ($app) { |
63
|
|
|
$class = $this->getVersionedRedisSentinelManagerClass(); |
64
|
|
|
$config = $app->make('config')->get('database.redis-sentinel', [ ]); |
65
|
|
|
$driver = Arr::pull($config, 'client', 'predis'); |
66
|
|
|
|
67
|
|
|
return new RedisSentinelManager(new $class($driver, $config)); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
// If we want Laravel's Redis API to use Sentinel, we'll return an |
71
|
|
|
// instance of the RedisSentinelManager when requesting the "redis" |
72
|
|
|
// service: |
73
|
|
|
if ($this->config->shouldOverrideLaravelRedisApi()) { |
74
|
|
|
$this->registerOverrides(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Replace the standard Laravel Redis service with the Redis Sentinel |
80
|
|
|
* database driver so all Redis operations use Sentinel connections. |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
protected function registerOverrides() |
85
|
|
|
{ |
86
|
|
|
$this->app->singleton('redis', function ($app) { |
87
|
|
|
return $app->make('redis-sentinel'); |
88
|
|
|
}); |
89
|
|
|
|
90
|
|
|
$this->app->bind('redis.connection', function ($app) { |
91
|
|
|
return $app->make('redis-sentinel')->connection(); |
92
|
|
|
}); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Extend each of the Laravel services this package supports with the |
97
|
|
|
* corresponding 'redis-sentinel' driver. |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
protected function bootComponentDrivers() |
102
|
|
|
{ |
103
|
|
|
$this->addRedisSentinelBroadcaster(); |
104
|
|
|
$this->addRedisSentinelCacheStore(); |
105
|
|
|
$this->addRedisSentinelQueueConnector(); |
106
|
|
|
|
107
|
|
|
// Since version 5.2, Lumen does not include support for sessions by |
108
|
|
|
// default, so we'll only register the session handler if enabled: |
109
|
|
|
if ($this->config->supportsSessions) { |
110
|
|
|
$this->addRedisSentinelSessionHandler(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Remove the standard Laravel Redis service from the bound deferred |
116
|
|
|
* services so they don't overwrite Redis Sentinel registrations. |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
protected function removeDeferredRedisServices() |
121
|
|
|
{ |
122
|
|
|
if ($this->config->isLumen) { |
123
|
|
|
return; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$deferredServices = $this->app->getDeferredServices(); |
127
|
|
|
|
128
|
|
|
unset($deferredServices['redis']); |
129
|
|
|
unset($deferredServices['redis.connection']); |
130
|
|
|
|
131
|
|
|
$this->app->setDeferredServices($deferredServices); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Add "redis-sentinel" as an available broadcaster option to the Laravel |
136
|
|
|
* event broadcasting manager. |
137
|
|
|
* |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
protected function addRedisSentinelBroadcaster() |
141
|
|
|
{ |
142
|
|
|
$this->app->make(BroadcastFactory::class) |
143
|
|
|
->extend('redis-sentinel', function ($app, $conf) { |
144
|
|
|
$redis = $app->make('redis-sentinel')->getVersionedManager(); |
145
|
|
|
$connection = Arr::get($conf, 'connection', 'default'); |
146
|
|
|
|
147
|
|
|
return new RedisBroadcaster($redis, $connection); |
148
|
|
|
}); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Add "redis-sentinel" as an available cache store option to the Laravel |
153
|
|
|
* cache manager. |
154
|
|
|
* |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
protected function addRedisSentinelCacheStore() |
158
|
|
|
{ |
159
|
|
|
$cache = $this->app->make('cache'); |
160
|
|
|
|
161
|
|
|
$cache->extend('redis-sentinel', function ($app, $conf) use ($cache) { |
162
|
|
|
$redis = $app->make('redis-sentinel')->getVersionedManager(); |
163
|
|
|
$prefix = $app->make('config')->get('cache.prefix'); |
164
|
|
|
$connection = Arr::get($conf, 'connection', 'default'); |
165
|
|
|
$store = new RedisStore($redis, $prefix, $connection); |
166
|
|
|
|
167
|
|
|
return $cache->repository($store); |
168
|
|
|
}); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Add "redis-sentinel" as an available driver option to the Laravel |
173
|
|
|
* session manager. |
174
|
|
|
* |
175
|
|
|
* @return void |
176
|
|
|
*/ |
177
|
|
|
protected function addRedisSentinelSessionHandler() |
178
|
|
|
{ |
179
|
|
|
$this->app->make('session')->extend('redis-sentinel', function ($app) { |
180
|
|
|
$config = $app->make('config'); |
181
|
|
|
$cacheDriver = clone $app->make('cache')->driver('redis-sentinel'); |
182
|
|
|
$minutes = $config->get('session.lifetime'); |
183
|
|
|
$connection = $config->get('session.connection'); |
184
|
|
|
|
185
|
|
|
$cacheDriver->getStore()->setConnection($connection); |
186
|
|
|
|
187
|
|
|
return new CacheBasedSessionHandler($cacheDriver, $minutes); |
188
|
|
|
}); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Add "redis-sentinel" as an available queue connection driver option to |
193
|
|
|
* the Laravel queue manager. |
194
|
|
|
* |
195
|
|
|
* @return void |
196
|
|
|
*/ |
197
|
|
|
protected function addRedisSentinelQueueConnector() |
198
|
|
|
{ |
199
|
|
|
$this->app->make('queue')->extend('redis-sentinel', function () { |
200
|
|
|
$redis = $this->app->make('redis-sentinel')->getVersionedManager(); |
201
|
|
|
|
202
|
|
|
return new RedisConnector($redis); |
203
|
|
|
}); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get the fully-qualified class name of the RedisSentinelManager class |
208
|
|
|
* for the current version of Laravel or Lumen. |
209
|
|
|
* |
210
|
|
|
* @return string The class name of the appropriate RedisSentinelManager |
211
|
|
|
* with its namespace |
212
|
|
|
*/ |
213
|
|
|
protected function getVersionedRedisSentinelManagerClass() |
214
|
|
|
{ |
215
|
|
|
if ($this->config->isLumen) { |
216
|
|
|
$appVersion = substr($this->app->version(), 7, 3); // ex. "5.4" |
217
|
|
|
$frameworkVersion = '5.4'; |
218
|
|
|
} else { |
219
|
|
|
$appVersion = \Illuminate\Foundation\Application::VERSION; |
220
|
|
|
$frameworkVersion = '5.4.20'; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
if (version_compare($appVersion, $frameworkVersion, 'lt')) { |
224
|
|
|
return Manager\Laravel540RedisSentinelManager::class; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return Manager\Laravel5420RedisSentinelManager::class; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
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: