Test Failed
Pull Request — stable (#304)
by
unknown
06:36
created

BuiltServiceProvider::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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\Facades\Config;
16
use Illuminate\Support\ServiceProvider;
17
use Illuminate\Support\Str;
18
19
class BuiltServiceProvider extends ServiceProvider
20
{
21
22
    const CONFIG_KEY  = 'app-built';
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 32
    public function boot(): void
28
    {
29 32
        if( $this->canReplace('filesystems') ){
30 1
            $this->replaceFileSystems();
31
        }
32 32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 32
    public function register(): void
38
    {
39 32
        $config = $this->app->make('config');
40
41 32
        if ($config->get( self::CONFIG_KEY ) === null) {
42 32
            $config->set( self::CONFIG_KEY , $this->getDefaultConfig());
43
        }
44 32
    }
45
46
    /**
47
     * Replace keys inside the filesystems config
48
     */
49 1
    protected function replaceFileSystems(): void
50
    {
51 1
        $currentFileSystems = config('filesystems');
52
53 1
        if( isset( $currentFileSystems['disks'] ) && count( $currentFileSystems['disks'] ) > 0 ){
54
55 1
            if( $this->canReplace('filesystems.drivers') ){
56
57 1
                foreach( $this->get('filesystems.drivers') as $driver => $replace ){
58
59 1
                    foreach( $replace as $key => $replacements ){
60
61 1
                        $old = array_shift( $replacements );
62 1
                        $new = array_shift( $replacements );
63
64 1
                        if( !empty( $old ) && !empty( $new ) ){
65 1
                            foreach( $currentFileSystems['disks'] as $diskName => $diskConfig ){
66 1
                                if( $diskConfig['driver'] === $driver && Str::startsWith( $diskConfig[ $key ], $old ) ){
67 1
                                    $diskConfig[ $key ] = str_replace( $old, $new, $diskConfig[ $key ] );
68 1
                                    Config::set('filesystems.disks.' . $diskName, $diskConfig );
69
                                }
70
                            }
71
                        }
72
                    }
73
                }
74
            }
75
        }
76 1
    }
77
78
    /**
79
     * Verify if we can replace settings when app is in production
80
     *
81
     * @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...
82
     * @return bool
83
     */
84 32
    protected function canReplace( $what=null ): bool
85
    {
86 32
        $canReplace = true;
87 32
        if( !is_null( $what ) ){
0 ignored issues
show
introduced by
The condition is_null($what) is always true.
Loading history...
88 32
            if( is_null( config( self::CONFIG_KEY . '.' . $what ) ) ){
89
                $canReplace = false;
90
            }
91
        }
92
93 32
        return config('app.production') === true && $canReplace;
94
    }
95
96
    /**
97
     * @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...
98
     *
99
     * @return \Illuminate\Config\Repository|mixed|null
100
     */
101 1
    protected function get( $what=null )
102
    {
103 1
        if( is_null( $what ) ){
0 ignored issues
show
introduced by
The condition is_null($what) is always true.
Loading history...
104
            return config( self::CONFIG_KEY );
105
        }
106
107 1
        return config( self::CONFIG_KEY . '.' . $what );
108
    }
109
110
111
    /**
112
     * Returns the default application build config.
113
     */
114 32
    protected function getDefaultConfig(): array
115
    {
116
        return [
117
            'filesystems' => [
118
                'drivers' => [
119
                    'local' => [
120
                        'root' => [
121 32
                            $this->app->storagePath(),
122 32
                            rtrim( getcwd(), DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . 'storage',
123
                        ],
124
                    ],
125
                ],
126
            ],
127
        ];
128
    }
129
}
130