Completed
Push — master ( 1f85c2...7bbaeb )
by Elf
10:21
created

SupportServiceProvider::mergeConfigForKey()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 12
ccs 0
cts 10
cp 0
crap 12
rs 9.4285
1
<?php
2
3
namespace ElfSundae\Laravel\Support;
4
5
use Illuminate\Support\ServiceProvider;
6
7
class SupportServiceProvider extends ServiceProvider
8
{
9
    /**
10
     * Bootstrap any application services.
11
     *
12
     * @return void
13
     */
14
    public function boot()
15
    {
16
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'support');
17
18
        $this->publishes([
19
            __DIR__.'/../config/support.php' => config_path('support.php'),
20
        ], 'laravel-support');
21
22
        $this->publishes([
23
            __DIR__.'/../resources/views' => resource_path('views/vendor/support'),
24
        ], 'laravel-support-views');
25
    }
26
27
    /**
28
     * Register the service.
29
     *
30
     * @return void
31
     */
32
    public function register()
33
    {
34
        $this->mergeConfigFrom(__DIR__.'/../config/support.php', 'support');
35
36
        $this->setupConfigurations();
37
38
        array_map([$this->app, 'register'], $this->getServiceProviders());
39
40
        if ($this->app->runningInConsole()) {
41
            $this->registerForConsole();
42
        }
43
    }
44
45
    /**
46
     * Setup application configurations.
47
     *
48
     * @return void
49
     */
50
    protected function setupConfigurations()
51
    {
52
        if (! $this->app->configurationIsCached()) {
53
            $this->configureDefaults();
54
        }
55
56
        $this->configureForCurrentRequest();
57
    }
58
59
    /**
60
     * Configure application defaults.
61
     *
62
     * @return void
63
     */
64
    protected function configureDefaults()
65
    {
66
        $config = $this->app['config'];
67
68
        $config['support.domain'] = array_map(function ($value) {
69
            return parse_url($value, PHP_URL_HOST) ?: null;
70
        }, $config->get('support.url', []));
71
72
        // Illuminate\Database\DatabaseServiceProvider reads "app.faker_locale" config
73
        $config['app.faker_locale'] = $config['support.faker_locale'];
74
75
        $config['cache.prefix'] = $config['support.cache_key_prefix'];
76
    }
77
78
    /**
79
     * Merge config data for the given key.
80
     *
81
     * @param  string  $key
82
     * @param  mixed  $value
83
     * @return void
84
     */
85
    protected function mergeConfigForKey($key, $value)
86
    {
87
        $config = $this->app['config']->get($key);
88
89
        if (is_array($config) && is_array($value)) {
90
            $config = array_merge($config, $value);
91
        } else {
92
            $config = $value;
93
        }
94
95
        $this->app['config']->set($key, $config);
96
    }
97
98
    /**
99
     * Configure application for the current request.
100
     *
101
     * @return void
102
     */
103
    protected function configureForCurrentRequest()
104
    {
105
        $config = $this->app['config'];
106
        $request = $this->app['request'];
107
108
        $identifier = array_search($request->getHost(), $config['support.domain']);
109
110
        if ($identifier && $config->has('support.cookie_domain.'.$identifier)) {
111
            $config['session.domain'] = $config['support.cookie_domain.'.$identifier];
112
        }
113
114
        if ($identifier && is_array($auth = $config['support.auth.'.$identifier])) {
115
            $config['auth.defaults'] = $auth;
116
        }
117
    }
118
119
    /**
120
     * Get service providers to be registered.
121
     *
122
     * @return array
123
     */
124
    protected function getServiceProviders()
125
    {
126
        $providers = [];
127
128
        if ($this->app->isLocal()) {
129
            array_push(
130
                $providers,
131
                \Barryvdh\Debugbar\ServiceProvider::class
132
            );
133
134
            if ($this->app->runningInConsole()) {
135
                array_push(
136
                    $providers,
137
                    \Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class
138
                );
139
            }
140
        }
141
142
        if ($this->app->runningInConsole()) {
143
            array_push(
144
                $providers,
145
                \Laravel\Tinker\TinkerServiceProvider::class,
146
                \Spatie\Backup\BackupServiceProvider::class
147
            );
148
        }
149
150
        return $providers;
151
    }
152
153
    /**
154
     * Register for console.
155
     *
156
     * @return void
157
     */
158
    protected function registerForConsole()
159
    {
160
        $this->commands([
161
            Console\Commands\AssetsVersion::class,
162
            Console\Commands\GenerateIdeHelpers::class,
163
            Console\Commands\GenerateInt2stringCharacters::class,
164
            Console\Commands\MergeUpstream::class,
165
        ]);
166
    }
167
}
168