1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlphaSnow\AliyunOss; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Foundation\Application; |
6
|
|
|
use Illuminate\Support\ServiceProvider as BaseServiceProvider; |
7
|
|
|
use Aliyun\Flysystem\AliyunOss\Plugins\PutFile; |
8
|
|
|
use League\Flysystem\Config; |
9
|
|
|
use League\Flysystem\Filesystem; |
10
|
|
|
use OSS\OssClient; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ServiceProvider |
14
|
|
|
* @package AlphaSnow\AliyunOss |
15
|
|
|
*/ |
16
|
|
|
class ServiceProvider extends BaseServiceProvider |
17
|
|
|
{ |
18
|
|
|
public function boot() |
19
|
|
|
{ |
20
|
|
|
$this->mergeConfigFrom( |
21
|
|
|
__DIR__.'/config/config.php', |
22
|
|
|
'filesystems.disks.aliyun' |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
$this->app->make('filesystem') |
26
|
|
|
->extend('aliyun', function ($app, array $config) { |
27
|
|
|
$client = $this->makeOssClient($app, $config); |
28
|
|
|
$adapter = new AliyunOssAdapter($client, $config); |
29
|
|
|
$filesystem = new Filesystem($adapter, new Config(['disable_asserts' => true])); |
30
|
|
|
$filesystem->addPlugin(new PutFile()); |
31
|
|
|
return $filesystem; |
32
|
|
|
}); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Application $app |
37
|
|
|
* @param array $config |
38
|
|
|
* @return OssClient |
39
|
|
|
*/ |
40
|
|
|
protected function makeOssClient($app, $config) |
41
|
|
|
{ |
42
|
|
|
return $app->make(OssClient::class, [ |
43
|
|
|
'accessKeyId' => $config['accessId'], |
44
|
|
|
'accessKeySecret' => $config['accessKey'], |
45
|
|
|
'endpoint' => $config['endpoint'], |
46
|
|
|
'isCName' => $config['isCname'], |
47
|
|
|
'securityToken' => $config['securityToken'] |
48
|
|
|
]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function register() |
52
|
|
|
{ |
53
|
|
|
$this->app->singleton('aliyun-oss.client', function ($app) { |
54
|
|
|
$config = $app->get('config')->get('filesystems.disks.aliyun'); |
55
|
|
|
return $this->makeOssClient($app, $config); |
56
|
|
|
}); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|