ServiceProvider::selectStorageRedis()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
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 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Benrowe\Laravel\Config;
4
5
use Benrowe\Laravel\Config\Storage\File;
6
use Benrowe\Laravel\Config\Storage\Pdo;
7
use Benrowe\Laravel\Config\Storage\Redis;
8
use Benrowe\Laravel\Config\Storage\StorageInterface;
9
use Illuminate\Contracts\Config\Repository as LaravelConfig;
10
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
11
12
/**
13
 * Service Provider for Config
14
 *
15
 * @package Benrowe\Laravel\Config;
16
 */
17
class ServiceProvider extends BaseServiceProvider
18
{
19
    protected $defer = false;
20
21
    /**
22
     * @inheritdoc
23
     */
24
    public function boot()
25
    {
26
        # publish necessary files
27
        $this->publishes([
28
            __DIR__ . '/config/config.php' => config_path('config.php'),
29
        ], 'config');
30
31
        $this->publishes([
32
            __DIR__.'/migrations/' => database_path('migrations'),
33
        ], 'migrations');
34
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function register()
40
    {
41
        $this->app->singleton('config-runtime', function () {
42
            $storage = $this->selectStorage($this->app['config']);
43
            if ($storage === null) {
44
                $storage = [];
45
            }
46
            $cfg = new Config($storage);
47
            $modifiers = $this->app['config']->get('config.modifiers', []);
48
            foreach ($modifiers as $className) {
49
                $cfg->modifiers->push(new $className);
50
            }
51
52
            return $cfg;
53
        });
54
    }
55
56
    /**
57
     * Define the services this provider will build & provide
58
     *
59
     * @return string[]
60
     */
61
    public function provides()
62
    {
63
        return [
64
            'Benrowe\Laravel\Config\Config',
65
            'config-runtime'
66
        ];
67
    }
68
69
    /**
70
     * Retrieve the storage engine, based on the current configuration
71
     *
72
     * @param  LaravelConfig $config
73
     * @return StorageInterface
74
     */
75
    protected function selectStorage($config)
76
    {
77
        if (!$config->get('config.storage.enabled')) {
78
            return null;
79
        }
80
        $driver = $config->get('config.storage.driver', 'file');
81
        $method = 'selectStorage'.ucfirst($driver);
82
        return $this->$method($config);
83
    }
84
85
    /**
86
     * Get an instance of the PDO storage engine
87
     *
88
     * @param  LaravelConfig $config
89
     * @return Pdo
90
     */
91
    protected function selectStoragePdo($config)
92
    {
93
        $connection = $config->get('config.storage.connection');
94
        $table = $this->app['db']->getTablePrefix() . 'config';
95
        $pdo = $this->app['db']->connection($connection)->getPdo();
96
        return new Pdo($pdo, $table);
97
    }
98
99
    /**
100
     * Get an instance of the Redis storage engine
101
     *
102
     * @param  LaravelConfig  $config
103
     * @return Redis
104
     */
105
    protected function selectStorageRedis($config)
106
    {
107
        $connection = $config->get('config.storage.connection');
108
        return new Redis($this->app['redis']->connection($connection));
109
    }
110
111
    /**
112
     * Get an instance of a custom defined storage engine
113
     *
114
     * @param  LaravelConfig $config [description]
115
     * @return StorageInterface
116
     */
117
    protected function selectStorageCustom($config)
118
    {
119
        $class = $config->get('config.storage.provider');
120
        return $this->app->make($class);
121
    }
122
123
    /**
124
     * Get an instance of the File storage engine
125
     *
126
     * @param  LaravelConfig $config
127
     * @return File
128
     */
129
    protected function selectStorageFile($config)
130
    {
131
        $path = $config->get('config.storage.path');
132
        return new File($path);
133
    }
134
}
135