Completed
Push — master ( 2cf7c3...82abf3 )
by Elf
08:58 queued 01:59
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 Carbon\Carbon;
6
use Illuminate\Support\Str;
7
use Illuminate\Support\ServiceProvider;
8
9
class SupportServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Bootstrap the application services.
13
     *
14
     * @return void
15
     */
16
    public function boot()
17
    {
18
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'support');
19
20
        if ($this->app->runningInConsole()) {
21
            $this->publishes([
22
                __DIR__.'/../config/support.php' => config_path('support.php'),
23
            ], 'laravel-support-config');
24
25
            $this->publishes([
26
                __DIR__.'/../resources/views' => resource_path('views/vendor/support'),
27
            ], 'laravel-support-views');
28
        }
29
    }
30
31
    /**
32
     * Register the application service.
33
     *
34
     * @return void
35
     */
36
    public function register()
37
    {
38
        $this->mergeConfigFrom(__DIR__.'/../config/support.php', 'support');
39
40
        $this->setupConfiguration();
41
42
        if ($this->app->runningInConsole()) {
43
            $this->commands([
44
                Console\Commands\AssetsVersion::class,
45
                Console\Commands\GenerateIdeHelpers::class,
46
                Console\Commands\GenerateInt2stringCharacters::class,
47
                Console\Commands\MergeUpstream::class,
48
            ]);
49
        }
50
    }
51
52
    /**
53
     * Setup application configurations.
54
     *
55
     * @return void
56
     */
57
    protected function setupConfiguration()
58
    {
59
        if (! $this->app->configurationIsCached()) {
60
            $this->configureDefaults();
61
        }
62
63
        $this->configureForCurrentRequest();
64
    }
65
66
    /**
67
     * Configure application defaults.
68
     *
69
     * @return void
70
     */
71
    protected function configureDefaults()
72
    {
73
        $config = $this->app['config'];
74
75
        $config['support.domain'] = array_map(function ($value) {
76
            return parse_url($value, PHP_URL_HOST);
77
        }, $config->get('support.url', []));
78
79
        $config->set($config->get('support.config.default', []));
80
    }
81
82
    /**
83
     * Configure application for the current request.
84
     *
85
     * @return void
86
     */
87
    protected function configureForCurrentRequest()
88
    {
89
        if (
90
            ($identifier = $this->getCurrentAppIdentifier()) &&
91
            $config = $this->app['config']['support.config.'.$identifier]
92
        ) {
93
            $this->app['config']->set($config);
94
        }
95
96
        if ($carbonLocale = $this->app['config']['support.carbon_locale']) {
97
            Carbon::setLocale($carbonLocale);
98
        }
99
    }
100
101
    /**
102
     * Get the current sub application identifier.
103
     *
104
     * @return string|null
105
     */
106
    protected function getCurrentAppIdentifier()
107
    {
108
        $requestUrl = $this->removeUrlScheme($this->app['request']->url());
109
110
        $apps = array_map(
111
            [$this, 'removeUrlScheme'],
112
            $this->app['config']->get('support.url', [])
113
        );
114
        arsort($apps);
115
116
        foreach ($apps as $id => $url) {
117
            if (Str::startsWith($requestUrl, $url)) {
118
                return $id;
119
            }
120
        }
121
    }
122
123
    /**
124
     * Remove the scheme for the given URL.
125
     *
126
     * @param  string  $url
127
     * @return string
128
     */
129
    protected function removeUrlScheme($url)
130
    {
131
        return preg_replace('#^https?://#', '', $url);
132
    }
133
}
134