66use AlphaSnow \LaravelFilesystem \Aliyun \Macros \AliyunMacro ;
77use AlphaSnow \LaravelFilesystem \Aliyun \Macros \AppendFile ;
88use AlphaSnow \LaravelFilesystem \Aliyun \Macros \AppendObject ;
9- use Illuminate \Container \ Container ;
9+ use Illuminate \Contracts \ Foundation \ Application ;
1010use Illuminate \Filesystem \FilesystemAdapter ;
11- use Illuminate \Support \ServiceProvider as BaseServiceProvider ;
11+ use Illuminate \Support \ServiceProvider ;
1212use League \Flysystem \Filesystem ;
1313
14- class AliyunServiceProvider extends BaseServiceProvider
14+ class AliyunServiceProvider extends ServiceProvider
1515{
1616 /**
1717 * @throws \Illuminate\Contracts\Container\BindingResolutionException
@@ -24,37 +24,41 @@ public function boot(): void
2424 );
2525
2626 $ this ->app ->make ("filesystem " )
27- ->extend ("oss " , function (Container $ app , array $ config ) {
28- $ adapter = $ app -> make ( AliyunFactory::class )->createAdapter ($ config );
29- $ driver = $ app -> make (Filesystem::class, [ " adapter " => $ adapter] );
30- $ filesystem = $ app -> make (FilesystemAdapter::class, [ " driver " => $ driver ," adapter " => $ adapter ," config " => $ config] );
31- $ this ->registerMicros ($ config ["macros " ] ?? [], $ filesystem , $ app );
27+ ->extend ("oss " , function (Application $ app , array $ config ) {
28+ $ adapter = ( new AliyunFactory () )->createAdapter ($ config );
29+ $ driver = ( new Filesystem ( $ adapter) );
30+ $ filesystem = new FilesystemAdapter ( $ driver , $ adapter , $ config );
31+ $ this ->registerMicros ($ filesystem , array_merge ( $ this -> defaultMacros , $ config ["macros " ] ?? []) );
3232 return $ filesystem ;
3333 });
3434 }
3535
3636 /**
37- * @var string[]
37+ * @var array
3838 */
39- protected $ defaultMacroClasses = [
39+ protected $ defaultMacros = [
4040 AppendFile::class,
4141 AppendObject::class,
4242 ];
4343
4444 /**
45- * @param array $macroClasses
4645 * @param FilesystemAdapter $filesystemAdapter
47- * @param Container $app
48- * @throws \Illuminate\Contracts\Container\BindingResolutionException
46+ * @param array $macros
4947 */
50- protected function registerMicros (array $ macroClasses , FilesystemAdapter $ filesystemAdapter , Container $ app ): void
48+ protected function registerMicros (FilesystemAdapter $ filesystemAdapter , array $ macros ): void
5149 {
52- $ macroClasses = array_merge ($ this ->defaultMacroClasses , $ macroClasses );
53- foreach ($ macroClasses as $ macroClass ) {
54- $ macro = $ app ->make ($ macroClass );
55- if ($ macro instanceof AliyunMacro) {
56- $ filesystemAdapter ::macro ($ macro ->name (), $ macro ->macro ());
50+ foreach ($ macros as $ macro ) {
51+ if (!class_exists ($ macro )) {
52+ continue ;
53+ }
54+ $ aliyunMacro = new $ macro ();
55+ if (!$ aliyunMacro instanceof AliyunMacro) {
56+ continue ;
57+ }
58+ if ($ filesystemAdapter ->hasMacro ($ aliyunMacro ->name ())) {
59+ continue ;
5760 }
61+ $ filesystemAdapter ::macro ($ aliyunMacro ->name (), $ aliyunMacro ->macro ());
5862 }
5963 }
6064}
0 commit comments