Completed
Push — master ( 4b9267...e3ae77 )
by Elf
04:01
created

configureForCurrentRequest()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 2
nop 0
dl 0
loc 10
ccs 0
cts 9
cp 0
crap 12
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace ElfSundae\Laravel\Support;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
use Illuminate\Support\ServiceProvider;
8
9
class SupportServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Bootstrap any application services.
13
     *
14
     * @return void
15
     */
16
    public function boot()
17
    {
18
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'support');
19
20
        $this->publishes([
21
            __DIR__.'/../config/support.php' => config_path('support.php'),
22
        ], 'laravel-support');
23
24
        $this->publishes([
25
            __DIR__.'/../resources/views' => resource_path('views/vendor/support'),
26
        ], 'laravel-support-views');
27
    }
28
29
    /**
30
     * Register the service.
31
     *
32
     * @return void
33
     */
34
    public function register()
35
    {
36
        $this->mergeConfigFrom(__DIR__.'/../config/support.php', 'support');
37
38
        $this->setupConfigurations();
39
40
        array_map([$this->app, 'register'], $this->getServiceProviders());
41
42
        if ($this->app->runningInConsole()) {
43
            $this->registerForConsole();
44
        }
45
    }
46
47
    /**
48
     * Setup application configurations.
49
     *
50
     * @return void
51
     */
52
    protected function setupConfigurations()
53
    {
54
        if (! $this->app->configurationIsCached()) {
55
            $this->configureDefaults();
56
        }
57
58
        $this->configureForCurrentRequest();
59
    }
60
61
    /**
62
     * Configure application defaults.
63
     *
64
     * @return void
65
     */
66
    protected function configureDefaults()
67
    {
68
        $config = $this->app['config'];
69
70
        $config['support.domain'] = array_map(function ($value) {
71
            return parse_url($value, PHP_URL_HOST);
72
        }, $config->get('support.url', []));
73
74
        $config->set($config->get('support.config.default', []));
2 ignored issues
show
Bug introduced by
The method get cannot be called on $config (of type array<string,array,{"support.domain":"array"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug introduced by
The method set cannot be called on $config (of type array<string,array,{"support.domain":"array"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
75
    }
76
77
    /**
78
     * Configure application for the current request.
79
     *
80
     * @return void
81
     */
82
    protected function configureForCurrentRequest()
83
    {
84
        $identifier = $this->getCurrentAppIdentifier();
85
86
        if ($identifier &&
87
            $config = $this->app['config']->get('support.config.'.$identifier)
88
        ) {
89
            $this->app['config']->set($config);
90
        }
91
    }
92
93
    /**
94
     * Get the current sub application identifier.
95
     *
96
     * @return string|null
97
     */
98
    protected function getCurrentAppIdentifier()
99
    {
100
        $requestUrl = $this->removeUrlScheme($this->app['request']->url());
101
102
        $apps = array_map(
103
            [$this, 'removeUrlScheme'],
104
            $this->app['config']->get('support.url', [])
105
        );
106
        arsort($apps);
107
108
        foreach ($apps as $id => $url) {
109
            if (Str::startsWith($requestUrl, $url)) {
110
                return $id;
111
            }
112
        }
113
    }
114
115
    /**
116
     * Remove the scheme for the given URL.
117
     *
118
     * @param  string  $url
119
     * @return string
120
     */
121
    protected function removeUrlScheme($url)
122
    {
123
        return preg_replace('#^https?://#', '', $url);
124
    }
125
126
    /**
127
     * Get service providers to be registered.
128
     *
129
     * @return array
130
     */
131
    protected function getServiceProviders()
132
    {
133
        $providers = [];
134
135
        if ($this->app->isLocal()) {
136
            array_push(
137
                $providers,
138
                \Barryvdh\Debugbar\ServiceProvider::class
139
            );
140
141
            if ($this->app->runningInConsole()) {
142
                array_push(
143
                    $providers,
144
                    \Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class
145
                );
146
            }
147
        }
148
149
        if ($this->app->runningInConsole()) {
150
            array_push(
151
                $providers,
152
                \Laravel\Tinker\TinkerServiceProvider::class,
153
                \Spatie\Backup\BackupServiceProvider::class
154
            );
155
        }
156
157
        return $providers;
158
    }
159
160
    /**
161
     * Register for console.
162
     *
163
     * @return void
164
     */
165
    protected function registerForConsole()
166
    {
167
        $this->commands([
168
            Console\Commands\AssetsVersion::class,
169
            Console\Commands\GenerateIdeHelpers::class,
170
            Console\Commands\GenerateInt2stringCharacters::class,
171
            Console\Commands\MergeUpstream::class,
172
        ]);
173
    }
174
}
175