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\RedisSentinelDatabase; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Registers the "redis-sentinel" driver as an available driver for Laravel's |
17
|
|
|
* cache, session, and queue services and loads the appropriate configuration. |
18
|
|
|
* |
19
|
|
|
* @category Package |
20
|
|
|
* @package Monospice\LaravelRedisSentinel |
21
|
|
|
* @author Cy Rossignol <[email protected]> |
22
|
|
|
* @license See LICENSE file |
23
|
|
|
* @link https://github.com/monospice/laravel-redis-sentinel-drivers |
24
|
|
|
*/ |
25
|
|
|
class RedisSentinelServiceProvider extends ServiceProvider |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Loads the package's configuration and provides configuration values. |
29
|
|
|
* |
30
|
|
|
* @var ConfigurationLoader |
31
|
|
|
*/ |
32
|
|
|
protected $config; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Boot the service by registering extensions with Laravel's cache, queue, |
36
|
|
|
* and session managers for the "redis-sentinel" driver. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function boot() |
41
|
|
|
{ |
42
|
|
|
$this->addRedisSentinelCacheDriver(); |
43
|
|
|
$this->addRedisSentinelQueueConnector(); |
44
|
|
|
|
45
|
|
|
// The Laravel broadcasting API exists in version 5.1 and later, so we |
46
|
|
|
// will only register the broadcaster if available: |
47
|
|
|
if ($this->config->supportsBroadcasting) { |
48
|
|
|
$this->addRedisSentinelBroadcaster(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Since version 5.2, Lumen does not include support for sessions by |
52
|
|
|
// default, so we'll only register the session handler if enabled: |
53
|
|
|
if ($this->config->supportsSessions) { |
54
|
|
|
$this->addRedisSentinelSessionHandler(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// If we want Laravel's Redis API to use Sentinel, we'll remove the |
58
|
|
|
// "redis" service from the deferred services in the container: |
59
|
|
|
if ($this->config->shouldOverrideLaravelRedisApi()) { |
60
|
|
|
$this->removeDeferredRedisServices(); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Bind the "redis-sentinel" database driver to the application service |
66
|
|
|
* container. |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function register() |
71
|
|
|
{ |
72
|
|
|
$this->config = ConfigurationLoader::load($this->app); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$this->app->singleton('redis-sentinel', function ($app) { |
75
|
|
|
$config = $app->make('config')->get('database.redis-sentinel', [ ]); |
76
|
|
|
|
77
|
|
|
return new RedisSentinelDatabase($config); |
78
|
|
|
}); |
79
|
|
|
|
80
|
|
|
// If we want Laravel's Redis API to use Sentinel, we'll return an |
81
|
|
|
// instance of the RedisSentinelDatabase when requesting the "redis" |
82
|
|
|
// service: |
83
|
|
|
if ($this->config->shouldOverrideLaravelRedisApi()) { |
84
|
|
|
$this->registerOverrides(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Replace the standard Laravel Redis service with the Redis Sentinel |
90
|
|
|
* database driver so all Redis operations use Sentinel connections. |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
protected function registerOverrides() |
95
|
|
|
{ |
96
|
|
|
$this->app->singleton('redis', function ($app) { |
97
|
|
|
return $app->make('redis-sentinel'); |
98
|
|
|
}); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Remove the standard Laravel Redis service from the bound deferred |
103
|
|
|
* services so they don't overwrite Redis Sentinel registrations. |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
protected function removeDeferredRedisServices() |
108
|
|
|
{ |
109
|
|
|
if ($this->config->isLumen) { |
110
|
|
|
unset($this->app->availableBindings['redis']); |
111
|
|
|
|
112
|
|
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$deferredServices = $this->app->getDeferredServices(); |
116
|
|
|
|
117
|
|
|
unset($deferredServices['redis']); |
118
|
|
|
|
119
|
|
|
$this->app->setDeferredServices($deferredServices); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Add "redis-sentinel" as an available broadcaster option to the Laravel |
124
|
|
|
* event broadcasting manager. |
125
|
|
|
* |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
|
|
protected function addRedisSentinelBroadcaster() |
129
|
|
|
{ |
130
|
|
|
$this->app->make(BroadcastFactory::class) |
131
|
|
|
->extend('redis-sentinel', function ($app, $conf) { |
132
|
|
|
$redis = $app->make('redis-sentinel'); |
133
|
|
|
$connection = Arr::get($conf, 'connection', 'default'); |
134
|
|
|
|
135
|
|
|
return new RedisBroadcaster($redis, $connection); |
136
|
|
|
}); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Add "redis-sentinel" as an available driver option to the Laravel cache |
141
|
|
|
* manager. |
142
|
|
|
* |
143
|
|
|
* @return void |
144
|
|
|
*/ |
145
|
|
|
protected function addRedisSentinelCacheDriver() |
146
|
|
|
{ |
147
|
|
|
$cache = $this->app->make('cache'); |
148
|
|
|
|
149
|
|
|
$cache->extend('redis-sentinel', function ($app, $conf) use ($cache) { |
150
|
|
|
$redis = $app->make('redis-sentinel'); |
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 $cache->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'); |
189
|
|
|
|
190
|
|
|
return new RedisConnector($redis); |
191
|
|
|
}); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
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: