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 |
|
|
|
|
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
32 |
|
protected function canReplace($what = null): bool |
80
|
|
|
{ |
81
|
32 |
|
$canReplace = true; |
82
|
32 |
|
if (! is_null($what)) { |
|
|
|
|
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 |
|
|
|
|
93
|
|
|
* |
94
|
|
|
* @return \Illuminate\Config\Repository|mixed|null |
95
|
|
|
*/ |
96
|
1 |
|
protected function get($what = null) |
97
|
|
|
{ |
98
|
1 |
|
if (is_null($what)) { |
|
|
|
|
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
|
|
|
|