Completed
Push — 2.x ( 406d2e )
by Cy
02:17
created

RedisSentinelServiceProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 10
dl 0
loc 147
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 17 2
B register() 0 24 2
A addRedisSentinelCacheDriver() 0 11 1
A addRedisSentinelSessionHandler() 0 14 1
A addRedisSentinelQueueConnector() 0 8 1
A shouldOverrideLaravelApi() 0 6 1
A getVersionedRedisSentinelManagerClass() 0 8 2
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\Manager;
14
15
/**
16
 * Registers the "redis-sentinel" driver as an available driver for Laravel's
17
 * cache, session, and queue services and loads the appropriate configuration.
18
 *
19
 * @category Package
20
 * @package  Monospice\LaravelRedisSentinel
21
 * @author   Cy Rossignol <[email protected]>
22
 * @license  See LICENSE file
23
 * @link     http://github.com/monospice/laravel-redis-sentinel-drivers
24
 */
25
class RedisSentinelServiceProvider extends ServiceProvider
26
{
27
    /**
28
     * Boot the service by registering extensions with Laravel's cache, queue,
29
     * and session managers for the "redis-sentinel" driver.
30
     *
31
     * @return void
32
     */
33
    public function boot()
34
    {
35
        $this->addRedisSentinelCacheDriver($this->app->make('cache'));
36
        $this->addRedisSentinelSessionHandler($this->app->make('session'));
37
        $this->addRedisSentinelQueueConnector($this->app->make('queue'));
38
39
        // If we want Laravel's Redis API to use Sentinel, we'll remove the
40
        // "redis" service from the list of deferred services in the container:
41
        if ($this->shouldOverrideLaravelApi()) {
42
            $deferredServices = $this->app->getDeferredServices();
43
44
            unset($deferredServices['redis']);
45
            unset($deferredServices['redis.connection']);
46
47
            $this->app->setDeferredServices($deferredServices);
48
        }
49
    }
50
51
    /**
52
     * Bind the "redis-sentinel" database driver to the application service
53
     * container.
54
     *
55
     * @return void
56
     */
57
    public function register()
58
    {
59
        $class = $this->getVersionedRedisSentinelManagerClass();
60
61
        $this->app->singleton('redis-sentinel', function ($app) use ($class) {
62
            $config = $app->make('config')->get('database.redis-sentinel');
63
            $driver = Arr::pull($config, 'client', 'predis');
64
65
            return new RedisSentinelManager(new $class($driver, $config));
66
        });
67
68
        // If we want Laravel's Redis API to use Sentinel, we'll return an
69
        // instance of the RedisSentinelManager when requesting the "redis"
70
        // service:
71
        if ($this->shouldOverrideLaravelApi()) {
72
            $this->app->singleton('redis', function ($app) {
73
                return $app->make('redis-sentinel');
74
            });
75
76
            $this->app->bind('redis.connection', function ($app) {
77
                return $app->make('redis-sentinel')->connection();
78
            });
79
        }
80
    }
81
82
    /**
83
     * Add "redis-sentinel" as an available driver option to the Laravel cache
84
     * manager.
85
     *
86
     * @param CacheManager $cache The Laravel cache manager
87
     *
88
     * @return void
89
     */
90
    protected function addRedisSentinelCacheDriver(CacheManager $cache)
91
    {
92
        $cache->extend('redis-sentinel', function ($app, $conf) use ($cache) {
93
            $redis = $app->make('redis-sentinel')->getVersionedManager();
94
            $prefix = $app->make('config')->get('cache.prefix');
95
            $connection = Arr::get($conf, 'connection', 'default');
96
            $store = new RedisStore($redis, $prefix, $connection);
97
98
            return $cache->repository($store);
99
        });
100
    }
101
102
    /**
103
     * Add "redis-sentinel" as an available driver option to the Laravel
104
     * session manager.
105
     *
106
     * @param SessionManager $session The Laravel session manager
107
     *
108
     * @return void
109
     */
110
    protected function addRedisSentinelSessionHandler(SessionManager $session)
111
    {
112
        $session->extend('redis-sentinel', function ($app) {
113
            $config = $app->make('config');
114
115
            $cacheDriver = clone $app->make('cache')->driver('redis-sentinel');
116
            $minutes = $config->get('session.lifetime');
117
            $connection = $config->get('session.connection');
118
119
            $cacheDriver->getStore()->setConnection($connection);
120
121
            return new CacheBasedSessionHandler($cacheDriver, $minutes);
122
        });
123
    }
124
125
    /**
126
     * Add "redis-sentinel" as an available queue connection driver option to
127
     * the Laravel queue manager.
128
     *
129
     * @param QueueManager $queue The Laravel queue manager
130
     *
131
     * @return void
132
     */
133
    protected function addRedisSentinelQueueConnector(QueueManager $queue)
134
    {
135
        $queue->extend('redis-sentinel', function () {
136
            $redis = $this->app->make('redis-sentinel')->getVersionedManager();
137
138
            return new RedisConnector($redis);
139
        });
140
    }
141
142
    /**
143
     * Determine whether this package should replace Laravel's Redis API
144
     * ("Redis" facade and "redis" service binding).
145
     *
146
     * @return bool True if "database.redis.driver" configuration option is
147
     * set to "sentinel"
148
     */
149
    protected function shouldOverrideLaravelApi()
150
    {
151
        $driver = $this->app->make('config')->get('database.redis.driver');
152
153
        return $driver === 'sentinel';
154
    }
155
156
    /**
157
     * Get the fully-qualified class name of the RedisSentinelManager class
158
     * for the current version of Laravel.
159
     *
160
     * @return string The class name of the appropriate RedisSentinelManager
161
     * with its namespace
162
     */
163
    protected function getVersionedRedisSentinelManagerClass()
164
    {
165
        if (version_compare($this->app::VERSION, '5.4.20', 'lt')) {
166
            return Manager\Laravel540RedisSentinelManager::class;
167
        }
168
169
        return Manager\Laravel5420RedisSentinelManager::class;
170
    }
171
}
172