1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlphaSnow\AliyunOss; |
4
|
|
|
|
5
|
|
|
use think\Service as BaseService; |
6
|
|
|
use AlphaSnow\AliyunOss\Plugins\PutRemoteFile; |
7
|
|
|
use League\Flysystem\Config; |
8
|
|
|
use League\Flysystem\Filesystem; |
9
|
|
|
use OSS\OssClient; |
10
|
|
|
|
11
|
|
|
class AliyunOssService extends BaseService |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
public function boot() |
15
|
|
|
{ |
16
|
|
|
$appConfig = $this->app->get('config'); |
17
|
|
|
if(!$appConfig->has('filesystem.disks.aliyun')){ |
18
|
|
|
$filesystemConfig = $appConfig->get('filesystem'); |
19
|
|
|
if($appConfig->has('aliyun-oss')){ |
20
|
|
|
$filesystemConfig['disks']['aliyun'] = $appConfig->get('aliyun-oss'); |
21
|
|
|
}else{ |
22
|
|
|
$filesystemConfig['disks']['aliyun'] = require __DIR__.'/config/config.php'; |
23
|
|
|
} |
24
|
|
|
$filesystemConfig['disks']['aliyun']['type'] = AliyunOssDriver::class; |
25
|
|
|
$appConfig->set($filesystemConfig,'filesystem'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$this->app->bind('aliyun.oss.filesystem',function(){ |
29
|
|
|
$adapter = $this->app->get('aliyun.oss.adapter'); |
30
|
|
|
|
31
|
|
|
$filesystem = new Filesystem($adapter, new Config(['disable_asserts' => true])); |
32
|
|
|
$filesystem->addPlugin(new PutRemoteFile()); |
33
|
|
|
|
34
|
|
|
return $filesystem; |
35
|
|
|
}); |
36
|
|
|
|
37
|
|
|
$this->app->bind('aliyun.oss.adapter',function(){ |
38
|
|
|
$config = $this->app->make(AliyunOssConfig::class); |
39
|
|
|
$client = $this->app->get(OssClient::class); |
40
|
|
|
|
41
|
|
|
$adapter = new AliyunOssAdapter($client, $config); |
42
|
|
|
|
43
|
|
|
return $adapter; |
44
|
|
|
}); |
45
|
|
|
|
46
|
|
|
$this->app->bind(AliyunOssConfig::class, function () { |
47
|
|
|
$config = $this->app->get('config')->get('aliyun-oss'); |
48
|
|
|
$ossConfig = new AliyunOssConfig($config); |
49
|
|
|
$ossConfig->checkRequired(); |
50
|
|
|
return $ossConfig; |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
View Code Duplication |
$this->app->bind(OssClient::class, function () { |
|
|
|
|
54
|
|
|
$ossConfig = $this->app->get(AliyunOssConfig::class); |
55
|
|
|
$ossClient = new OssClient( |
56
|
|
|
$ossConfig->getAccessId(), |
57
|
|
|
$ossConfig->getAccessKey(), |
58
|
|
|
$ossConfig->getOssEndpoint(), |
59
|
|
|
$ossConfig->isCname(), |
60
|
|
|
$ossConfig->getSecurityToken(), |
61
|
|
|
$ossConfig->getRequestProxy() |
62
|
|
|
); |
63
|
|
|
return $ossClient; |
64
|
|
|
}); |
65
|
|
|
$this->app->bind( 'aliyun.oss.client',OssClient::class); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function register() |
69
|
|
|
{ |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.