1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Monospice\LaravelRedisSentinel\Horizon; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Container\Container; |
6
|
|
|
use Illuminate\Contracts\Redis\Factory as RedisFactory; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Support\ServiceProvider; |
9
|
|
|
use Laravel\Horizon\Connectors\RedisConnector as HorizonRedisConnector; |
10
|
|
|
use Monospice\LaravelRedisSentinel\Configuration\Loader as ConfigurationLoader; |
11
|
|
|
use Monospice\LaravelRedisSentinel\Horizon\HorizonServiceBindings; |
12
|
|
|
use Monospice\LaravelRedisSentinel\RedisSentinelManager; |
13
|
|
|
use RuntimeException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Configures the application to use Redis Sentinel connections for Laravel |
17
|
|
|
* Horizon. |
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 HorizonServiceProvider 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
|
|
|
* Create a new service provider instance. |
36
|
|
|
* |
37
|
|
|
* @param Container $app The current Laravel/Lumen application |
38
|
|
|
* instance. |
39
|
|
|
* @param ConfigurationLoader $config Loads the package's configuration and |
|
|
|
|
40
|
|
|
* provides configuration values. |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
Container $app, |
44
|
|
|
ConfigurationLoader $config = null |
45
|
|
|
) { |
46
|
|
|
parent::__construct($app); |
|
|
|
|
47
|
|
|
|
48
|
|
|
if ($config === null) { |
49
|
|
|
$config = ConfigurationLoader::load($app); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->config = $config; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Configure the package for use with Laravel Horizon. |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function register() |
61
|
|
|
{ |
62
|
|
View Code Duplication |
$this->app->singleton('redis-sentinel', function ($app) { |
|
|
|
|
63
|
|
|
$this->createHorizonConnectionConfiguration(); |
64
|
|
|
|
65
|
|
|
$class = $this->config->getVersionedRedisSentinelManagerClass(); |
66
|
|
|
$config = $this->config->get('database.redis-sentinel', [ ]); |
67
|
|
|
$driver = Arr::pull($config, 'client', 'predis'); |
68
|
|
|
|
69
|
|
|
return new RedisSentinelManager(new $class($driver, $config)); |
70
|
|
|
}); |
71
|
|
|
|
72
|
|
|
if ($this->shouldRebindHorizonRedisFactory()) { |
73
|
|
|
$this->rebindHorizonRedisFactory(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->addHorizonSentinelQueueConnector(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Sets the Horizon Redis Sentinel connection configuration. |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
protected function createHorizonConnectionConfiguration() |
85
|
|
|
{ |
86
|
|
|
$horizonConfig = $this->getSelectedConnectionConfiguration(); |
87
|
|
|
$options = Arr::get($horizonConfig, 'options', [ ]); |
88
|
|
|
$options['prefix'] = $this->config->get('horizon.prefix', 'horizon:'); |
89
|
|
|
|
90
|
|
|
$horizonConfig['options'] = $options; |
91
|
|
|
|
92
|
|
|
$this->config->set('database.redis-sentinel.horizon', $horizonConfig); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Copy the Redis Sentinel connection configuration to use for Horizon |
97
|
|
|
* connections from the connection specified by "horizon.use". |
98
|
|
|
* |
99
|
|
|
* @return array The configuration matching the connection name specified |
100
|
|
|
* by the "horizon.use" config directive. |
101
|
|
|
* |
102
|
|
|
* @throws RuntimeException If no Redis Sentinel connection matches the |
103
|
|
|
* name declared by "horizon.use". |
104
|
|
|
*/ |
105
|
|
|
protected function getSelectedConnectionConfiguration() |
106
|
|
|
{ |
107
|
|
|
$use = $this->config->get('horizon.use', 'default'); |
108
|
|
|
$connectionConfig = $this->config->get("database.redis-sentinel.$use"); |
|
|
|
|
109
|
|
|
|
110
|
|
|
if ($connectionConfig === null) { |
111
|
|
|
throw new RuntimeException( |
112
|
|
|
"The Redis Sentinel connection [$use] is not defined." |
|
|
|
|
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $connectionConfig; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Determine whether the package needs to override the Redis service |
121
|
|
|
* injected into Horizon classes with the Sentinel service. |
122
|
|
|
* |
123
|
|
|
* @return bool True if configured as such and the package doesn't already |
124
|
|
|
* override the application's Redis API. |
125
|
|
|
*/ |
126
|
|
|
protected function shouldRebindHorizonRedisFactory() |
127
|
|
|
{ |
128
|
|
|
// If we're already overriding Laravel's standard Redis API, we don't |
129
|
|
|
// need to rebind the "redis" service for Horizon. |
130
|
|
|
return ! $this->config->shouldOverrideLaravelRedisApi |
131
|
|
|
&& $this->config->get('horizon.driver') === 'redis-sentinel'; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Add contextual bindings for Horizon's services that inject the package's |
136
|
|
|
* Redis Sentinel manager. |
137
|
|
|
* |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
protected function rebindHorizonRedisFactory() |
141
|
|
|
{ |
142
|
|
|
// Although not all of the classes that Horizon registers need an |
143
|
|
|
// instance of the Redis service, we'll set up contextual bindings |
144
|
|
|
// for any declared so we don't need to update this package in the |
145
|
|
|
// future every time Horizon adds or removes one: |
146
|
|
|
foreach ((new HorizonServiceBindings()) as $serviceClass) { |
147
|
|
|
$this->app->when($serviceClass) |
148
|
|
|
->needs(RedisFactory::class) |
149
|
|
|
->give(function () { |
150
|
|
|
return $this->app->make('redis-sentinel.manager'); |
151
|
|
|
}); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Add "redis-sentinel" as an available queue connection driver option to |
157
|
|
|
* the Laravel queue manager using Horizon's modified Redis connector. |
158
|
|
|
* |
159
|
|
|
* @return void |
160
|
|
|
*/ |
161
|
|
View Code Duplication |
protected function addHorizonSentinelQueueConnector() |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
$this->app->make('queue')->extend('redis-sentinel', function () { |
164
|
|
|
$redis = $this->app->make('redis-sentinel.manager'); |
165
|
|
|
|
166
|
|
|
return new HorizonRedisConnector($redis); |
167
|
|
|
}); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.