Passed
Push — master ( 0e9c59...109301 )
by alpha
11:56
created

ServiceProvider::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
rs 9.552
cc 2
nc 2
nop 0
1
<?php
2
3
namespace AlphaSnow\AliyunOss;
4
5
use Aliyun\Flysystem\AliyunOss\Plugins\PutFile;
6
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
7
use League\Flysystem\Config;
8
use League\Flysystem\Filesystem;
9
use OSS\OssClient;
10
11
/**
12
 * Class ServiceProvider
13
 * @package AlphaSnow\AliyunOss
14
 */
15
class ServiceProvider extends BaseServiceProvider
16
{
17
    public function boot()
18
    {
19
        if ($this->app->runningInConsole()) {
20
            $this->publishes([
21
                __DIR__ . '/config/config.php' => config_path('aliyun-oss.php'),
22
            ], 'config');
23
        }
24
        $this->mergeConfigFrom(
25
            __DIR__.'/config/config.php',
26
            'filesystems.disks.aliyun'
27
        );
28
29
        $this->app->make('filesystem')
30
            ->extend('aliyun', function ($app, array $config) {
31
                $client = $app->get(OssClient::class);
32
33
                $adapter = new AliyunOssAdapter($client, $config['bucket'],$config['prefix'],$config['options']);
34
                $filesystem = new Filesystem($adapter, new Config(['disable_asserts' => true]));
35
                $filesystem->addPlugin(new PutFile());
36
37
                return $filesystem;
38
            });
39
    }
40
41
    public function register()
42
    {
43
        $this->app->singleton(OssClient::class, function ($app) {
44
            $config = $app->get('config')->get('filesystems.disks.aliyun');
45
            $ossClient = new OssClient(
46
                $config['accessId'],
47
                $config['accessKey'],
48
                $config['endpoint'],
49
                $config['isCname'],
50
                $config['securityToken']
51
            );
52
            return $ossClient;
53
        });
54
        $this->app->alias(OssClient::class, 'aliyun.oss.client');
55
    }
56
}
57