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\ServiceProvider; |
8
|
|
|
use Laravel\Horizon\Connectors\RedisConnector as HorizonRedisConnector; |
9
|
|
|
use Monospice\LaravelRedisSentinel\Configuration\Loader as ConfigurationLoader; |
10
|
|
|
use Monospice\LaravelRedisSentinel\Horizon\HorizonServiceBindings; |
11
|
|
|
use Monospice\LaravelRedisSentinel\Manager\VersionedManagerFactory; |
12
|
|
|
use Monospice\LaravelRedisSentinel\RedisSentinelServiceProvider; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Configures the application to use Redis Sentinel connections for Laravel |
16
|
|
|
* Horizon. |
17
|
|
|
* |
18
|
|
|
* For applications that use Sentinel connections only for the queue API and |
19
|
|
|
* Horizon, we can register this service provider without the package's main |
20
|
|
|
* service provider. |
21
|
|
|
* |
22
|
|
|
* @category Package |
23
|
|
|
* @package Monospice\LaravelRedisSentinel |
24
|
|
|
* @author Cy Rossignol <[email protected]> |
25
|
|
|
* @license See LICENSE file |
26
|
|
|
* @link https://github.com/monospice/laravel-redis-sentinel-drivers |
27
|
|
|
*/ |
28
|
|
|
class HorizonServiceProvider extends ServiceProvider |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Loads the package's configuration and provides configuration values. |
32
|
|
|
* |
33
|
|
|
* @var ConfigurationLoader |
34
|
|
|
*/ |
35
|
|
|
protected $config; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a new service provider instance. |
39
|
|
|
* |
40
|
|
|
* @param Container $app The current Laravel/Lumen application |
41
|
|
|
* instance. |
42
|
|
|
* @param ConfigurationLoader $config Loads the package's configuration and |
|
|
|
|
43
|
|
|
* provides configuration values. |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
Container $app, |
47
|
|
|
ConfigurationLoader $config = null |
48
|
|
|
) { |
49
|
|
|
parent::__construct($app); |
|
|
|
|
50
|
|
|
|
51
|
|
|
if ($config === null) { |
52
|
|
|
$config = ConfigurationLoader::load($app); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->config = $config; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set up any additional components needed for Horizon. |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public function boot() |
64
|
|
|
{ |
65
|
|
|
if (! $this->config->shouldIntegrateHorizon) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->addHorizonSentinelQueueConnector(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Configure the package's services for use with Laravel Horizon. |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
public function register() |
78
|
|
|
{ |
79
|
|
|
if (! $this->config->shouldIntegrateHorizon) { |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->config->loadHorizonConfiguration(); |
84
|
|
|
|
85
|
|
|
$this->registerServices(); |
86
|
|
|
|
87
|
|
|
if ($this->shouldRebindHorizonRedisFactory()) { |
88
|
|
|
$this->rebindHorizonRedisFactory(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Register the core Redis Sentinel connection manager if not already bound |
94
|
|
|
* (for using this service provider by itself). |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
protected function registerServices() |
99
|
|
|
{ |
100
|
|
|
$this->app->bindIf('redis-sentinel', function ($app) { |
|
|
|
|
101
|
|
|
return VersionedManagerFactory::make($this->config); |
102
|
|
|
}, true); |
103
|
|
|
|
104
|
|
|
$this->app->bindIf('redis-sentinel.manager', function ($app) { |
105
|
|
|
return $app->make('redis-sentinel')->getVersionedManager(); |
106
|
|
|
}, true); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Determine whether the package needs to override the Redis service |
111
|
|
|
* injected into Horizon classes with the Sentinel service. |
112
|
|
|
* |
113
|
|
|
* @return bool True if configured as such and the package doesn't already |
114
|
|
|
* override the application's Redis API. |
115
|
|
|
*/ |
116
|
|
|
protected function shouldRebindHorizonRedisFactory() |
117
|
|
|
{ |
118
|
|
|
// If we're using this package for Horizon only, we only register this |
119
|
|
|
// service provider, so nothing overrides Laravel's standard Redis API: |
120
|
|
|
if (! $this->app->bound(RedisSentinelServiceProvider::class)) { |
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// If we're already overriding Laravel's standard Redis API, we don't |
125
|
|
|
// need to rebind the "redis" service for Horizon. |
126
|
|
|
return ! $this->config->shouldOverrideLaravelRedisApi; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Add contextual bindings for Horizon's services that inject the package's |
131
|
|
|
* Redis Sentinel manager. |
132
|
|
|
* |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
|
|
protected function rebindHorizonRedisFactory() |
136
|
|
|
{ |
137
|
|
|
// Although not all of the classes that Horizon registers need an |
138
|
|
|
// instance of the Redis service, we'll set up contextual bindings |
139
|
|
|
// for any declared so we don't need to update this package in the |
140
|
|
|
// future every time Horizon adds or removes one: |
141
|
|
|
foreach ((new HorizonServiceBindings()) as $serviceClass) { |
142
|
|
|
$this->app->when($serviceClass) |
143
|
|
|
->needs(RedisFactory::class) |
144
|
|
|
->give(function () { |
145
|
|
|
return $this->app->make('redis-sentinel.manager'); |
146
|
|
|
}); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Add "redis-sentinel" as an available queue connection driver option to |
152
|
|
|
* the Laravel queue manager using Horizon's modified Redis connector. |
153
|
|
|
* |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
View Code Duplication |
protected function addHorizonSentinelQueueConnector() |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
$this->app->make('queue')->extend('redis-sentinel', function () { |
159
|
|
|
$redis = $this->app->make('redis-sentinel.manager'); |
160
|
|
|
|
161
|
|
|
return new HorizonRedisConnector($redis); |
162
|
|
|
}); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
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.