Completed
Push — master ( 779cfb...d323c2 )
by Elf
18:21
created

SupportServiceProvider::setupConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 6
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->publishes([
17
            __DIR__.'/../config/support.php' => config_path('support.php'),
18
        ], 'laravel-support');
19
    }
20
21
    /**
22
     * Register the service.
23
     *
24
     * @return void
25
     */
26
    public function register()
27
    {
28
        $this->mergeConfigFrom(__DIR__.'/../config/support.php', 'support');
29
30
        $this->setupConfigurations();
31
32
        array_map([$this->app, 'register'], $this->getServiceProviders());
33
34
        if ($this->app->runningInConsole()) {
35
            $this->registerForConsole();
36
        }
37
    }
38
39
    /**
40
     * Setup application configurations.
41
     *
42
     * @return void
43
     */
44
    protected function setupConfigurations()
45
    {
46
        if (! $this->app->configurationIsCached()) {
47
            $this->configureDefaults();
48
        }
49
50
        $this->configureForCurrentRequest();
51
    }
52
53
    /**
54
     * Configure application defaults.
55
     *
56
     * @return void
57
     */
58
    protected function configureDefaults()
59
    {
60
        $config = $this->app['config'];
61
62
        // Append "app.domains"
63
        $config['app.domains'] = array_map(function ($value) {
64
            if (is_string($domain = parse_url($value, PHP_URL_HOST))) {
65
                if (str_contains($domain, '.')) {
66
                    return $domain;
67
                }
68
            }
69
        }, $config['support.url']);
70
    }
71
72
    /**
73
     * Configure application for the current request.
74
     *
75
     * @return void
76
     */
77
    protected function configureForCurrentRequest()
78
    {
79
        $config = $this->app['config'];
80
        $request = $this->app['request'];
81
82
        $identifier = array_search($request->getHost(), $config['app.domains']);
83
84
        if ($identifier && $config->has('support.cookie_domain.'.$identifier)) {
85
            $config['session.domain'] = $config['support.cookie_domain.'.$identifier];
86
        }
87
88
        if ($identifier && is_array($auth = $config['support.auth.'.$identifier])) {
89
            $config['auth.defaults'] = $auth;
90
        }
91
    }
92
93
    /**
94
     * Get service providers to be registered.
95
     *
96
     * @return array
97
     */
98
    protected function getServiceProviders()
99
    {
100
        $providers = [
101
            Providers\RoutingServiceProvider::class,
102
        ];
103
104
        if ($this->app->isLocal()) {
105
            array_push(
106
                $providers,
107
                \Barryvdh\Debugbar\ServiceProvider::class
108
            );
109
110
            if ($this->app->runningInConsole()) {
111
                array_push(
112
                    $providers,
113
                    \Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class
114
                );
115
            }
116
        }
117
118
        if ($this->app->runningInConsole()) {
119
            array_push(
120
                $providers,
121
                \Laravel\Tinker\TinkerServiceProvider::class,
122
                \Spatie\Backup\BackupServiceProvider::class
123
            );
124
        }
125
126
        return $providers;
127
    }
128
129
    /**
130
     * Register for console.
131
     *
132
     * @return void
133
     */
134
    protected function registerForConsole()
135
    {
136
        $this->commands([
137
            Console\Commands\AssetsVersion::class,
138
            Console\Commands\GenerateIdeHelpers::class,
139
            Console\Commands\GenerateInt2stringCharacters::class,
140
        ]);
141
    }
142
}
143