33namespace AlphaSnow \LaravelFilesystem \Aliyun ;
44
55use AlphaSnow \Flysystem \Aliyun \AliyunFactory ;
6- use AlphaSnow \LaravelFilesystem \Aliyun \Macros \AliyunMacro ;
7- use AlphaSnow \LaravelFilesystem \Aliyun \Macros \AppendFile ;
8- use AlphaSnow \LaravelFilesystem \Aliyun \Macros \AppendObject ;
6+ use Illuminate \Contracts \Container \BindingResolutionException ;
97use Illuminate \Contracts \Foundation \Application ;
108use Illuminate \Contracts \Foundation \CachesConfiguration ;
119use Illuminate \Filesystem \FilesystemAdapter ;
@@ -16,29 +14,25 @@ class AliyunServiceProvider extends ServiceProvider
1614{
1715 /**
1816 * @return void
17+ * @throws BindingResolutionException
1918 */
2019 public function register ()
2120 {
22- $ this ->app ->singleton (AliyunFactory::class, function ($ app ) {
23- return new AliyunFactory ();
24- });
25-
26- if ($ this ->app ::class == "Laravel\Lumen\Application " ) {
27- return ;
28- }
29-
30- $ this ->registerConfig ();
21+ $ this ->mergeOssConfig ();
3122 }
3223
3324 /**
3425 * @return void
26+ * @throws BindingResolutionException
3527 */
36- protected function registerConfig ()
28+ protected function mergeOssConfig ()
3729 {
3830 if ($ this ->app instanceof CachesConfiguration && $ this ->app ->configurationIsCached ()) {
3931 return ;
4032 }
4133
34+ // If a driver for OSS has been defined
35+ // Then configuration merge will not be performed
4236 $ config = $ this ->app ->make ('config ' );
4337 $ disks = $ config ->get ("filesystems.disks " , []);
4438 $ drivers = array_column ($ disks , "driver " );
@@ -54,46 +48,17 @@ protected function registerConfig()
5448
5549 /**
5650 * @return void
51+ * @throws BindingResolutionException
5752 */
5853 public function boot ()
5954 {
6055 $ this ->app ->make ("filesystem " )
6156 ->extend ("oss " , function (Application $ app , array $ config ) {
62- $ adapter = $ app ->make (AliyunFactory::class)->createAdapter ($ config );
57+ $ adapter = $ this -> app ->make (AliyunFactory::class)->createAdapter ($ config );
6358 $ driver = new Filesystem ($ adapter );
6459 $ filesystem = new FilesystemAdapter ($ driver , $ adapter , $ config );
65- $ macros = array_merge ($ this ->defaultMacros , $ config ["macros " ] ?? []);
66- $ this ->registerMicros ($ filesystem , $ macros );
60+ (new FilesystemMacroManager ($ filesystem ))->defaultRegister ()->register ($ config ["macros " ] ?? []);
6761 return $ filesystem ;
6862 });
6963 }
70-
71- /**
72- * @var array
73- */
74- protected $ defaultMacros = [
75- AppendFile::class,
76- AppendObject::class,
77- ];
78-
79- /**
80- * @param FilesystemAdapter $filesystemAdapter
81- * @param array $macros
82- */
83- protected function registerMicros (FilesystemAdapter $ filesystemAdapter , array $ macros ): void
84- {
85- foreach ($ macros as $ macro ) {
86- if (!class_exists ($ macro )) {
87- continue ;
88- }
89- $ aliyunMacro = new $ macro ();
90- if (!$ aliyunMacro instanceof AliyunMacro) {
91- continue ;
92- }
93- if ($ filesystemAdapter ->hasMacro ($ aliyunMacro ->name ())) {
94- continue ;
95- }
96- $ filesystemAdapter ::macro ($ aliyunMacro ->name (), $ aliyunMacro ->macro ());
97- }
98- }
9964}
0 commit comments