|
7 | 7 | use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AppendFile; |
8 | 8 | use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AppendObject; |
9 | 9 | use Illuminate\Contracts\Foundation\Application; |
| 10 | +use Illuminate\Contracts\Foundation\CachesConfiguration; |
10 | 11 | use Illuminate\Filesystem\FilesystemAdapter; |
11 | 12 | use Illuminate\Support\ServiceProvider; |
12 | 13 | use League\Flysystem\Filesystem; |
13 | 14 |
|
14 | 15 | class AliyunServiceProvider extends ServiceProvider |
15 | 16 | { |
16 | 17 | /** |
17 | | - * @throws \Illuminate\Contracts\Container\BindingResolutionException |
| 18 | + * @return void |
18 | 19 | */ |
19 | | - public function boot(): void |
| 20 | + public function register() |
20 | 21 | { |
21 | | - $this->mergeConfigFrom( |
22 | | - __DIR__ . "/../config/config.php", |
23 | | - "filesystems.disks.oss" |
24 | | - ); |
| 22 | + $this->registerConfig(); |
| 23 | + } |
25 | 24 |
|
| 25 | + /** |
| 26 | + * @return void |
| 27 | + */ |
| 28 | + protected function registerConfig() |
| 29 | + { |
| 30 | + if ($this->app instanceof CachesConfiguration && $this->app->configurationIsCached()) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + $config = $this->app->make('config'); |
| 35 | + $disks = $config->get("filesystems.disks", []); |
| 36 | + $drivers = array_column($disks, "driver"); |
| 37 | + if (in_array("oss", $drivers)) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + $config->set("filesystems.disks.oss", array_merge( |
| 42 | + require __DIR__ . "/../config/config.php", |
| 43 | + $config->get("filesystems.disks.oss", []) |
| 44 | + )); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @return void |
| 49 | + */ |
| 50 | + public function boot() |
| 51 | + { |
26 | 52 | $this->app->make("filesystem") |
27 | 53 | ->extend("oss", function (Application $app, array $config) { |
28 | 54 | $adapter = (new AliyunFactory())->createAdapter($config); |
29 | 55 | $driver = (new Filesystem($adapter)); |
30 | 56 | $filesystem = new FilesystemAdapter($driver, $adapter, $config); |
31 | | - $this->registerMicros($filesystem, array_merge($this->defaultMacros, $config["macros"] ?? [])); |
| 57 | + $macros = array_merge($this->defaultMacros, $config["macros"] ?? []); |
| 58 | + $this->registerMicros($filesystem, $macros); |
32 | 59 | return $filesystem; |
33 | 60 | }); |
34 | 61 | } |
|
0 commit comments