Skip to content

Commit d648626

Browse files
committed
feat: Dynamic loading config
1 parent c6181a2 commit d648626

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

src/AliyunServiceProvider.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,55 @@
77
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AppendFile;
88
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AppendObject;
99
use Illuminate\Contracts\Foundation\Application;
10+
use Illuminate\Contracts\Foundation\CachesConfiguration;
1011
use Illuminate\Filesystem\FilesystemAdapter;
1112
use Illuminate\Support\ServiceProvider;
1213
use League\Flysystem\Filesystem;
1314

1415
class AliyunServiceProvider extends ServiceProvider
1516
{
1617
/**
17-
* @throws \Illuminate\Contracts\Container\BindingResolutionException
18+
* @return void
1819
*/
19-
public function boot(): void
20+
public function register()
2021
{
21-
$this->mergeConfigFrom(
22-
__DIR__ . "/../config/config.php",
23-
"filesystems.disks.oss"
24-
);
22+
$this->registerConfig();
23+
}
2524

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+
{
2652
$this->app->make("filesystem")
2753
->extend("oss", function (Application $app, array $config) {
2854
$adapter = (new AliyunFactory())->createAdapter($config);
2955
$driver = (new Filesystem($adapter));
3056
$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);
3259
return $filesystem;
3360
});
3461
}

0 commit comments

Comments
 (0)