1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlphaSnow\LaravelFilesystem\Aliyun; |
4
|
|
|
|
5
|
|
|
use 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; |
9
|
|
|
use Illuminate\Contracts\Foundation\Application; |
10
|
|
|
use Illuminate\Contracts\Foundation\CachesConfiguration; |
11
|
|
|
use Illuminate\Filesystem\FilesystemAdapter; |
12
|
|
|
use Illuminate\Support\ServiceProvider; |
13
|
|
|
use League\Flysystem\Filesystem; |
14
|
|
|
|
15
|
|
|
class AliyunServiceProvider extends ServiceProvider |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
10 |
|
public function register() |
21
|
|
|
{ |
22
|
10 |
|
$this->app->singleton(AliyunFactory::class, function ($app) { |
|
|
|
|
23
|
7 |
|
return new AliyunFactory(); |
24
|
|
|
}); |
25
|
|
|
|
26
|
10 |
|
if ($this->app::class == "Laravel\Lumen\Application") { |
27
|
|
|
return; |
28
|
|
|
} |
29
|
|
|
|
30
|
10 |
|
$this->registerConfig(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
10 |
|
protected function registerConfig() |
37
|
|
|
{ |
38
|
10 |
|
if ($this->app instanceof CachesConfiguration && $this->app->configurationIsCached()) { |
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
10 |
|
$config = $this->app->make('config'); |
43
|
10 |
|
$disks = $config->get("filesystems.disks", []); |
44
|
10 |
|
$drivers = array_column($disks, "driver"); |
45
|
10 |
|
if (in_array("oss", $drivers)) { |
46
|
1 |
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
9 |
|
$config->set("filesystems.disks.oss", array_merge( |
50
|
9 |
|
require __DIR__ . "/../config/config.php", |
51
|
9 |
|
$config->get("filesystems.disks.oss", []) |
52
|
|
|
)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
10 |
|
public function boot() |
59
|
|
|
{ |
60
|
10 |
|
$this->app->make("filesystem") |
61
|
10 |
|
->extend("oss", function (Application $app, array $config) { |
62
|
8 |
|
$adapter = $app->make(AliyunFactory::class)->createAdapter($config); |
63
|
8 |
|
$driver = new Filesystem($adapter); |
64
|
8 |
|
$filesystem = new FilesystemAdapter($driver, $adapter, $config); |
65
|
8 |
|
$macros = array_merge($this->defaultMacros, $config["macros"] ?? []); |
66
|
8 |
|
$this->registerMicros($filesystem, $macros); |
67
|
8 |
|
return $filesystem; |
68
|
|
|
}); |
69
|
|
|
} |
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
|
8 |
|
protected function registerMicros(FilesystemAdapter $filesystemAdapter, array $macros): void |
84
|
|
|
{ |
85
|
8 |
|
foreach ($macros as $macro) { |
86
|
8 |
|
if (!class_exists($macro)) { |
87
|
|
|
continue; |
88
|
|
|
} |
89
|
8 |
|
$aliyunMacro = new $macro(); |
90
|
8 |
|
if (!$aliyunMacro instanceof AliyunMacro) { |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
8 |
|
if ($filesystemAdapter->hasMacro($aliyunMacro->name())) { |
94
|
7 |
|
continue; |
95
|
|
|
} |
96
|
1 |
|
$filesystemAdapter::macro($aliyunMacro->name(), $aliyunMacro->macro()); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.