Completed
Push — 2.x ( 341981...b9f40f )
by Cy
01:39
created

RedisSentinelServiceProvider::getVersionedRedisSentinelManagerClass()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 10
nc 4
nop 0
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\Contracts\Factory;
14
use Monospice\LaravelRedisSentinel\RedisSentinelManager;
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);
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...
61
62
        $this->registerServices();
63
64
        // If we want Laravel's Redis API to use Sentinel, we'll return an
65
        // instance of the RedisSentinelManager when requesting the "redis"
66
        // service:
67
        if ($this->config->shouldOverrideLaravelRedisApi) {
68
            $this->registerOverrides();
69
        }
70
    }
71
72
   /**
73
    * Register the core Redis Sentinel connection manager.
74
    *
75
    * @return void
76
    */
77
   protected function registerServices()
78
   {
79
        $this->app->singleton('redis-sentinel', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
            $class = $this->config->getVersionedRedisSentinelManagerClass();
81
            $config = $this->config->get('database.redis-sentinel', [ ]);
82
            $driver = Arr::pull($config, 'client', 'predis');
83
84
            return new RedisSentinelManager(new $class($driver, $config));
85
        });
86
87
        $this->app->singleton('redis-sentinel.manager', function ($app) {
88
            return $app->make('redis-sentinel')->getVersionedManager();
89
        });
90
91
        $this->app->alias('redis-sentinel', Factory::class);
92
    }
93
94
    /**
95
     * Replace the standard Laravel Redis service with the Redis Sentinel
96
     * database driver so all Redis operations use Sentinel connections.
97
     *
98
     * @return void
99
     */
100
    protected function registerOverrides()
101
    {
102
        $this->app->singleton('redis', function ($app) {
103
            return $app->make('redis-sentinel');
104
        });
105
106
        $this->app->bind('redis.connection', function ($app) {
107
            return $app->make('redis-sentinel.manager')->connection();
108
        });
109
    }
110
111
    /**
112
     * Extend each of the Laravel services this package supports with the
113
     * corresponding 'redis-sentinel' driver.
114
     *
115
     * @return void
116
     */
117
    protected function bootComponentDrivers()
118
    {
119
        $this->addRedisSentinelBroadcaster();
120
        $this->addRedisSentinelCacheStore();
121
        $this->addRedisSentinelQueueConnector();
122
123
        // Since version 5.2, Lumen does not include support for sessions by
124
        // default, so we'll only register the session handler if enabled:
125
        if ($this->config->supportsSessions) {
126
            $this->addRedisSentinelSessionHandler();
127
        }
128
    }
129
130
    /**
131
     * Remove the standard Laravel Redis service from the bound deferred
132
     * services so they don't overwrite Redis Sentinel registrations.
133
     *
134
     * @return void
135
     */
136
    protected function removeDeferredRedisServices()
137
    {
138
        if ($this->config->isLumen) {
139
            return;
140
        }
141
142
        $deferredServices = $this->app->getDeferredServices();
143
144
        unset($deferredServices['redis']);
145
        unset($deferredServices['redis.connection']);
146
147
        $this->app->setDeferredServices($deferredServices);
148
    }
149
150
    /**
151
     * Add "redis-sentinel" as an available broadcaster option to the Laravel
152
     * event broadcasting manager.
153
     *
154
     * @return void
155
     */
156
    protected function addRedisSentinelBroadcaster()
157
    {
158
        $this->app->make(BroadcastFactory::class)
159
            ->extend('redis-sentinel', function ($app, $conf) {
160
                $redis = $app->make('redis-sentinel.manager');
161
                $connection = Arr::get($conf, 'connection', 'default');
162
163
                return new RedisBroadcaster($redis, $connection);
164
            });
165
    }
166
167
    /**
168
     * Add "redis-sentinel" as an available cache store option to the Laravel
169
     * cache manager.
170
     *
171
     * @return void
172
     */
173
    protected function addRedisSentinelCacheStore()
174
    {
175
        $cache = $this->app->make('cache');
176
177
        $cache->extend('redis-sentinel', function ($app, $conf) use ($cache) {
178
            $redis = $app->make('redis-sentinel.manager');
179
            $prefix = $app->make('config')->get('cache.prefix');
180
            $connection = Arr::get($conf, 'connection', 'default');
181
            $store = new RedisStore($redis, $prefix, $connection);
182
183
            return $cache->repository($store);
184
        });
185
    }
186
187
    /**
188
     * Add "redis-sentinel" as an available driver option to the Laravel
189
     * session manager.
190
     *
191
     * @return void
192
     */
193
    protected function addRedisSentinelSessionHandler()
194
    {
195
        $this->app->make('session')->extend('redis-sentinel', function ($app) {
196
            $cacheDriver = clone $app->make('cache')->driver('redis-sentinel');
197
            $minutes = $this->config->get('session.lifetime');
198
            $connection = $this->config->get('session.connection');
199
200
            $cacheDriver->getStore()->setConnection($connection);
201
202
            return new CacheBasedSessionHandler($cacheDriver, $minutes);
203
        });
204
    }
205
206
    /**
207
     * Add "redis-sentinel" as an available queue connection driver option to
208
     * the Laravel queue manager.
209
     *
210
     * @return void
211
     */
212
    protected function addRedisSentinelQueueConnector()
213
    {
214
        $this->app->make('queue')->extend('redis-sentinel', function () {
215
            $redis = $this->app->make('redis-sentinel.manager');
216
217
            return new RedisConnector($redis);
218
        });
219
    }
220
}
221