Passed
Push — master ( 6843e9...26c301 )
by alpha
13:27
created

AliyunServiceProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 86.11%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 36
c 5
b 0
f 0
dl 0
loc 82
ccs 31
cts 36
cp 0.8611
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A registerMicros() 0 14 5
A register() 0 11 2
A registerConfig() 0 16 4
A boot() 0 10 1
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 2
    public function register()
21
    {
22 2
        $this->app->singleton(AliyunFactory::class, function () {
23 2
            return new AliyunFactory();
24
        });
25
26 2
        if ($this->app::class == "Laravel\Lumen\Application") {
27
            return;
28
        }
29
30 2
        $this->registerConfig();
31
    }
32
33
    /**
34
     * @return void
35
     */
36 2
    protected function registerConfig()
37
    {
38 2
        if ($this->app instanceof CachesConfiguration && $this->app->configurationIsCached()) {
39
            return;
40
        }
41
42 2
        $config = $this->app->make('config');
43 2
        $disks = $config->get("filesystems.disks", []);
44 2
        $drivers = array_column($disks, "driver");
45 2
        if (in_array("oss", $drivers)) {
46
            return;
47
        }
48
49 2
        $config->set("filesystems.disks.oss", array_merge(
50 2
            require __DIR__ . "/../config/config.php",
51 2
            $config->get("filesystems.disks.oss", [])
52
        ));
53
    }
54
55
    /**
56
     * @return void
57
     */
58 2
    public function boot()
59
    {
60 2
        $this->app->make("filesystem")
61 2
            ->extend("oss", function (Application $app, array $config) {
62 2
                $adapter = $app->make(AliyunFactory::class)->createAdapter($config);
63 2
                $driver = new Filesystem($adapter);
64 2
                $filesystem = new FilesystemAdapter($driver, $adapter, $config);
65 2
                $macros = array_merge($this->defaultMacros, $config["macros"] ?? []);
66 2
                $this->registerMicros($filesystem, $macros);
67 2
                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 2
    protected function registerMicros(FilesystemAdapter $filesystemAdapter, array $macros): void
84
    {
85 2
        foreach ($macros as $macro) {
86 2
            if (!class_exists($macro)) {
87
                continue;
88
            }
89 2
            $aliyunMacro = new $macro();
90 2
            if (!$aliyunMacro instanceof AliyunMacro) {
91
                continue;
92
            }
93 2
            if ($filesystemAdapter->hasMacro($aliyunMacro->name())) {
94 1
                continue;
95
            }
96 1
            $filesystemAdapter::macro($aliyunMacro->name(), $aliyunMacro->macro());
97
        }
98
    }
99
}
100