bootComponentDrivers()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
namespace Monospice\LaravelRedisSentinel;
4
5
use Illuminate\Broadcasting\Broadcasters\RedisBroadcaster;
6
use Illuminate\Cache\RedisStore;
7
use Illuminate\Contracts\Broadcasting\Factory as BroadcastFactory;
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\Contracts\Factory;
14
use Monospice\LaravelRedisSentinel\Horizon\HorizonServiceProvider;
15
use Monospice\LaravelRedisSentinel\Manager\VersionedManagerFactory;
16
17
/**
18
 * Registers the "redis-sentinel" driver as an available driver for Laravel's
19
 * cache, session, and queue services and loads the appropriate configuration.
20
 *
21
 * @category Package
22
 * @package  Monospice\LaravelRedisSentinel
23
 * @author   Cy Rossignol <[email protected]>
24
 * @license  See LICENSE file
25
 * @link     https://github.com/monospice/laravel-redis-sentinel-drivers
26
 */
27
class RedisSentinelServiceProvider extends ServiceProvider
28
{
29
    /**
30
     * Loads the package's configuration and provides configuration values.
31
     *
32
     * @var ConfigurationLoader
33
     */
34
    protected $config;
35
36
    /**
37
     * Records whether this provider has already been booted (eg. via auto-boot)
38
     *
39
     * @var boolean
40
     */
41
    private $isBooted = false;
42
43
    /**
44
     * Boot the service by registering extensions with Laravel's cache, queue,
45
     * and session managers for the "redis-sentinel" driver.
46
     *
47
     * @return void
48
     */
49
    public function boot()
50
    {
51
        // If we configured the package to boot its services immediately after
52
        // the registration phase (auto-boot), don't boot the provider again:
53
        if ($this->isBooted) {
54
            return;
55
        }
56
57
        $this->bootComponentDrivers();
58
59
        // If we want Laravel's Redis API to use Sentinel, we'll remove the
60
        // "redis" service from the deferred services in the container:
61
        if ($this->config->shouldOverrideLaravelRedisApi) {
62
            $this->removeDeferredRedisServices();
63
        }
64
65
        if ($this->config->shouldIntegrateHorizon) {
66
            $horizon = new HorizonServiceProvider($this->app, $this->config);
67
            $horizon->register();
68
            $horizon->boot();
69
        }
70
71
        $this->isBooted = true;
72
    }
73
74
    /**
75
     * Bind the "redis-sentinel" database driver to the application service
76
     * container.
77
     *
78
     * @return void
79
     */
80
    public function register()
81
    {
82
        $this->config = ConfigurationLoader::load($this->app);
0 ignored issues
show
Documentation introduced by
$this->app is of type object<Illuminate\Contra...Foundation\Application>, but the function expects a object<Illuminate\Founda...avel\Lumen\Application>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
84
        $this->registerServices();
85
86
        // If we want Laravel's Redis API to use Sentinel, we'll return an
87
        // instance of the RedisSentinelManager when requesting the "redis"
88
        // service:
89
        if ($this->config->shouldOverrideLaravelRedisApi) {
90
            $this->registerOverrides();
91
        }
92
93
        // If we explicitly configured the package to auto-boot, run the boot
94
        // phase now to bind the packages drivers. This overcomes issues with
95
        // other third-party packages that don't follow Laravel's convention:
96
        if ($this->config->shouldAutoBoot()) {
97
            $this->boot();
98
        }
99
    }
100
101
    /**
102
     * Register the core Redis Sentinel connection manager.
103
     *
104
     * @return void
105
     */
106
    protected function registerServices()
107
    {
108
        $this->app->singleton('redis-sentinel', function () {
109
            return VersionedManagerFactory::make($this->app, $this->config);
110
        });
111
112
        $this->app->singleton('redis-sentinel.manager', function ($app) {
113
            return $app->make('redis-sentinel')->getVersionedManager();
114
        });
115
116
        $this->app->alias('redis-sentinel', Factory::class);
117
    }
118
119
    /**
120
     * Replace the standard Laravel Redis service with the Redis Sentinel
121
     * database driver so all Redis operations use Sentinel connections.
122
     *
123
     * @return void
124
     */
125
    protected function registerOverrides()
126
    {
127
        $this->app->singleton('redis', function ($app) {
128
            return $app->make('redis-sentinel');
129
        });
130
131
        $this->app->bind('redis.connection', function ($app) {
132
            return $app->make('redis-sentinel.manager')->connection();
133
        });
134
    }
135
136
    /**
137
     * Extend each of the Laravel services this package supports with the
138
     * corresponding 'redis-sentinel' driver.
139
     *
140
     * @return void
141
     */
142
    protected function bootComponentDrivers()
143
    {
144
        $this->addRedisSentinelBroadcaster();
145
        $this->addRedisSentinelCacheStore();
146
147
        // This package's Horizon service provider will set up the queue
148
        // connector a bit differently, so we don't need to do it twice:
149
        if (! $this->config->shouldIntegrateHorizon) {
150
            $this->addRedisSentinelQueueConnector();
151
        }
152
153
        // Since version 5.2, Lumen does not include support for sessions by
154
        // default, so we'll only register the session handler if enabled:
155
        if ($this->config->supportsSessions) {
156
            $this->addRedisSentinelSessionHandler();
157
        }
158
    }
159
160
    /**
161
     * Remove the standard Laravel Redis service from the bound deferred
162
     * services so they don't overwrite Redis Sentinel registrations.
163
     *
164
     * @return void
165
     */
166
    protected function removeDeferredRedisServices()
167
    {
168
        if ($this->config->isLumen) {
169
            return;
170
        }
171
172
        $deferredServices = $this->app->getDeferredServices();
173
174
        unset($deferredServices['redis']);
175
        unset($deferredServices['redis.connection']);
176
177
        $this->app->setDeferredServices($deferredServices);
178
    }
179
180
    /**
181
     * Add "redis-sentinel" as an available broadcaster option to the Laravel
182
     * event broadcasting manager.
183
     *
184
     * @return void
185
     */
186
    protected function addRedisSentinelBroadcaster()
187
    {
188
        $this->app->make(BroadcastFactory::class)
189
            ->extend('redis-sentinel', function ($app, $conf) {
190
                $redis = $app->make('redis-sentinel.manager');
191
                $connection = Arr::get($conf, 'connection', 'default');
192
193
                return new RedisBroadcaster($redis, $connection);
194
            });
195
    }
196
197
    /**
198
     * Add "redis-sentinel" as an available cache store option to the Laravel
199
     * cache manager.
200
     *
201
     * @return void
202
     */
203
    protected function addRedisSentinelCacheStore()
204
    {
205
        $cache = $this->app->make('cache');
206
207
        $cache->extend('redis-sentinel', function ($app, $conf) use ($cache) {
208
            $redis = $app->make('redis-sentinel.manager');
209
            $prefix = $app->make('config')->get('cache.prefix');
210
            $connection = Arr::get($conf, 'connection', 'default');
211
            $store = new RedisStore($redis, $prefix, $connection);
212
213
            return $cache->repository($store);
214
        });
215
    }
216
217
    /**
218
     * Add "redis-sentinel" as an available driver option to the Laravel
219
     * session manager.
220
     *
221
     * @return void
222
     */
223
    protected function addRedisSentinelSessionHandler()
224
    {
225
        $this->app->make('session')->extend('redis-sentinel', function ($app) {
226
            $cacheDriver = clone $app->make('cache')->driver('redis-sentinel');
227
            $minutes = $this->config->get('session.lifetime');
228
            $connection = $this->config->get('session.connection');
229
230
            $cacheDriver->getStore()->setConnection($connection);
231
232
            return new CacheBasedSessionHandler($cacheDriver, $minutes);
233
        });
234
    }
235
236
    /**
237
     * Add "redis-sentinel" as an available queue connection driver option to
238
     * the Laravel queue manager.
239
     *
240
     * @return void
241
     */
242 View Code Duplication
    protected function addRedisSentinelQueueConnector()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
243
    {
244
        $this->app->make('queue')->extend('redis-sentinel', function () {
245
            $redis = $this->app->make('redis-sentinel.manager');
246
247
            return new RedisConnector($redis);
248
        });
249
    }
250
}
251