Completed
Push — 1.x ( d9ab4d...3295a0 )
by Cy
08:55
created

shouldOverrideLaravelApi()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Monospice\LaravelRedisSentinel;
4
5
use Illuminate\Cache\CacheManager;
6
use Illuminate\Cache\RedisStore;
7
use Illuminate\Queue\QueueManager;
8
use Illuminate\Queue\Connectors\RedisConnector;
9
use Illuminate\Session\CacheBasedSessionHandler;
10
use Illuminate\Session\SessionManager;
11
use Illuminate\Support\Arr;
12
use Illuminate\Support\ServiceProvider;
13
use Monospice\LaravelRedisSentinel\Configuration\Loader as ConfigurationLoader;
14
use Monospice\LaravelRedisSentinel\RedisSentinelDatabase;
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->addRedisSentinelCacheDriver($this->app->make('cache'));
44
        $this->addRedisSentinelQueueConnector($this->app->make('queue'));
45
46
        // Since version 5.2, Lumen does not include support for sessions by
47
        // default, so we'll only register the session handler if enabled:
48
        if ($this->config->supportsSessions) {
49
            $this->addRedisSentinelSessionHandler($this->app->make('session'));
50
        }
51
52
        // If we want Laravel's Redis API to use Sentinel, we'll remove the
53
        // "redis" service from the deferred services in the container:
54
        if ($this->config->shouldOverrideLaravelRedisApi()) {
55
            $this->removeDeferredRedisServices();
56
        }
57
    }
58
59
    /**
60
     * Bind the "redis-sentinel" database driver to the application service
61
     * container.
62
     *
63
     * @return void
64
     */
65
    public function register()
66
    {
67
        $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...
68
69
        $this->app->singleton('redis-sentinel', function ($app) {
70
            $config = $app->make('config')->get('database.redis-sentinel', [ ]);
71
72
            return new RedisSentinelDatabase($config);
73
        });
74
75
        // If we want Laravel's Redis API to use Sentinel, we'll return an
76
        // instance of the RedisSentinelDatabase when requesting the "redis"
77
        // service:
78
        if ($this->config->shouldOverrideLaravelRedisApi()) {
79
            $this->registerOverrides();
80
        }
81
    }
82
83
    /**
84
     * Replace the standard Laravel Redis service with the Redis Sentinel
85
     * database driver so all Redis operations use Sentinel connections.
86
     *
87
     * @return void
88
     */
89
    protected function registerOverrides()
90
    {
91
        $this->app->singleton('redis', function ($app) {
92
            return $app->make('redis-sentinel');
93
        });
94
    }
95
96
    /**
97
     * Remove the standard Laravel Redis service from the bound deferred
98
     * services so they don't overwrite Redis Sentinel registrations.
99
     *
100
     * @return void
101
     */
102
    protected function removeDeferredRedisServices()
103
    {
104
        if ($this->config->isLumen) {
105
            return;
106
        }
107
108
        $deferredServices = $this->app->getDeferredServices();
109
110
        unset($deferredServices['redis']);
111
112
        $this->app->setDeferredServices($deferredServices);
113
    }
114
115
    /**
116
     * Add "redis-sentinel" as an available driver option to the Laravel cache
117
     * manager.
118
     *
119
     * @param CacheManager $cache The Laravel cache manager
120
     *
121
     * @return void
122
     */
123
    protected function addRedisSentinelCacheDriver(CacheManager $cache)
124
    {
125
        $cache->extend('redis-sentinel', function ($app, $conf) use ($cache) {
126
            $redis = $app->make('redis-sentinel');
127
            $prefix = $app->make('config')->get('cache.prefix');
128
            $connection = Arr::get($conf, 'connection', 'default');
129
            $store = new RedisStore($redis, $prefix, $connection);
130
131
            return $cache->repository($store);
132
        });
133
    }
134
135
    /**
136
     * Add "redis-sentinel" as an available driver option to the Laravel
137
     * session manager.
138
     *
139
     * @param SessionManager $session The Laravel session manager
140
     *
141
     * @return void
142
     */
143
    protected function addRedisSentinelSessionHandler(SessionManager $session)
144
    {
145
        $session->extend('redis-sentinel', function ($app) {
146
            $config = $app->make('config');
147
            $cacheDriver = clone $app->make('cache')->driver('redis-sentinel');
148
            $minutes = $config->get('session.lifetime');
149
            $connection = $config->get('session.connection');
150
151
            $cacheDriver->getStore()->setConnection($connection);
152
153
            return new CacheBasedSessionHandler($cacheDriver, $minutes);
154
        });
155
    }
156
157
    /**
158
     * Add "redis-sentinel" as an available queue connection driver option to
159
     * the Laravel queue manager.
160
     *
161
     * @param QueueManager $queue The Laravel queue manager
162
     *
163
     * @return void
164
     */
165
    protected function addRedisSentinelQueueConnector(QueueManager $queue)
166
    {
167
        $queue->extend('redis-sentinel', function () {
168
            $redis = $this->app->make('redis-sentinel');
169
170
            return new RedisConnector($redis);
171
        });
172
    }
173
}
174