Completed
Pull Request — stable (#304)
by
unknown
06:09 queued 04:12
created

BuiltServiceProvider   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 22
eloc 34
dl 0
loc 99
ccs 34
cts 36
cp 0.9444
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 7 2
A canReplace() 0 10 4
A register() 0 6 2
A getDefaultConfig() 0 9 1
A boot() 0 4 2
B replaceFileSystems() 0 16 11
1
<?php
2
3
4
/**
5
 * This file is part of Laravel Zero.
6
 *
7
 * (c) Nuno Maduro <[email protected]>
8
 *
9
 *  For the full copyright and license information, please view the LICENSE
10
 *  file that was distributed with this source code.
11
 */
12
13
namespace LaravelZero\Framework\Providers\Built;
14
15
use Illuminate\Support\Str;
16
use Illuminate\Support\Facades\Config;
17
use Illuminate\Support\ServiceProvider;
18
19
class BuiltServiceProvider extends ServiceProvider
20
{
21
    const CONFIG_KEY = 'app-built';
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 32
    public function boot(): void
27
    {
28 32
        if ($this->canReplace('filesystems')) {
29 1
            $this->replaceFileSystems();
30
        }
31 32
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 32
    public function register(): void
37
    {
38 32
        $config = $this->app->make('config');
39
40 32
        if ($config->get(self::CONFIG_KEY) === null) {
41 32
            $config->set(self::CONFIG_KEY, $this->getDefaultConfig());
42
        }
43 32
    }
44
45
    /**
46
     * Replace keys inside the filesystems config.
47
     */
48 1
    protected function replaceFileSystems(): void
49
    {
50 1
        $currentFileSystems = config('filesystems');
51
52 1
        if (isset($currentFileSystems['disks']) && count($currentFileSystems['disks']) > 0) {
53 1
            if ($this->canReplace('filesystems.drivers')) {
54 1
                foreach ($this->get('filesystems.drivers') as $driver => $replace) {
55 1
                    foreach ($replace as $key => $replacements) {
56 1
                        $old = array_shift($replacements);
57 1
                        $new = array_shift($replacements);
58
59 1
                        if (! empty($old) && ! empty($new)) {
60 1
                            foreach ($currentFileSystems['disks'] as $diskName => $diskConfig) {
61 1
                                if ($diskConfig['driver'] === $driver && Str::startsWith($diskConfig[$key], $old)) {
62 1
                                    $diskConfig[$key] = str_replace($old, $new, $diskConfig[$key]);
63 1
                                    Config::set('filesystems.disks.'.$diskName, $diskConfig);
64
                                }
65
                            }
66
                        }
67
                    }
68
                }
69
            }
70
        }
71 1
    }
72
73
    /**
74
     * Verify if we can replace settings when app is in production.
75
     *
76
     * @param null $what
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $what is correct as it would always require null to be passed?
Loading history...
77
     * @return bool
78
     */
79 32
    protected function canReplace($what = null): bool
80
    {
81 32
        $canReplace = true;
82 32
        if (! is_null($what)) {
0 ignored issues
show
introduced by
The condition is_null($what) is always true.
Loading history...
83 32
            if (is_null(config(self::CONFIG_KEY.'.'.$what))) {
84
                $canReplace = false;
85
            }
86
        }
87
88 32
        return config('app.production') === true && $canReplace;
89
    }
90
91
    /**
92
     * @param null $what
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $what is correct as it would always require null to be passed?
Loading history...
93
     *
94
     * @return \Illuminate\Config\Repository|mixed|null
95
     */
96 1
    protected function get($what = null)
97
    {
98 1
        if (is_null($what)) {
0 ignored issues
show
introduced by
The condition is_null($what) is always true.
Loading history...
99
            return config(self::CONFIG_KEY);
100
        }
101
102 1
        return config(self::CONFIG_KEY.'.'.$what);
103
    }
104
105
106
    /**
107
     * Returns the default application build config.
108
     */
109 32
    protected function getDefaultConfig(): array
110
    {
111
        return [
112
            'filesystems' => [
113
                'drivers' => [
114
                    'local' => [
115
                        'root' => [
116 32
                            $this->app->storagePath(),
117 32
                            rtrim(getcwd(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'storage',
118
                        ],
119
                    ],
120
                ],
121
            ],
122
        ];
123
    }
124
}
125