1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlphaSnow\AliyunOssThink; |
4
|
|
|
|
5
|
|
|
use think\Cache; |
6
|
|
|
use think\Service as BaseService; |
7
|
|
|
use Aliyun\Flysystem\AliyunOss\Plugins\PutFile; |
8
|
|
|
use League\Flysystem\Config; |
9
|
|
|
use League\Flysystem\Filesystem; |
10
|
|
|
use OSS\OssClient; |
11
|
|
|
|
12
|
|
|
class AliyunOssService extends BaseService |
13
|
|
|
{ |
14
|
2 |
|
protected function setupConfig() |
15
|
|
|
{ |
16
|
2 |
|
$appConfig = $this->app->get('config'); |
17
|
2 |
|
if (!$appConfig->has('filesystem.disks.aliyun')) { |
18
|
2 |
|
$filesystemConfig = $appConfig->get('filesystem'); |
19
|
2 |
|
$filesystemConfig['disks']['aliyun'] = require __DIR__.'/config/config.php'; |
20
|
2 |
|
$filesystemConfig['disks']['aliyun']['type'] = AliyunOssDriver::class; |
21
|
2 |
|
$appConfig->set($filesystemConfig, 'filesystem'); |
22
|
|
|
} |
23
|
2 |
|
} |
24
|
2 |
|
public function boot() |
25
|
|
|
{ |
26
|
2 |
|
$this->setupConfig(); |
27
|
2 |
|
} |
28
|
|
|
|
29
|
|
|
public function register() |
30
|
|
|
{ |
31
|
2 |
|
$this->app->bind(AliyunOssDriver::class, function () { |
32
|
1 |
|
$cache = $this->app->get(Cache::class); |
33
|
1 |
|
$config = $this->app->get('config')->get('filesystem.disks.aliyun'); |
34
|
1 |
|
$driver = new AliyunOssDriver($cache, $config); |
35
|
1 |
|
return $driver; |
36
|
2 |
|
}); |
37
|
2 |
|
$this->app->bind('aliyun-oss.driver', AliyunOssDriver::class); |
38
|
|
|
|
39
|
2 |
|
$this->app->bind('aliyun-oss.filesystem', function () { |
40
|
|
|
$adapter = $this->app->get('aliyun-oss.adapter'); |
41
|
|
|
|
42
|
|
|
$filesystem = new Filesystem($adapter, new Config(['disable_asserts' => true])); |
43
|
|
|
$filesystem->addPlugin(new PutFile()); |
44
|
|
|
|
45
|
|
|
return $filesystem; |
46
|
2 |
|
}); |
47
|
|
|
|
48
|
2 |
|
$this->app->bind('aliyun-oss.adapter', function () { |
49
|
2 |
|
$config = $this->app->get('config')->get('filesystem.disks.aliyun'); |
50
|
2 |
|
$client = $this->app->get('aliyun-oss.client'); |
51
|
|
|
|
52
|
2 |
|
$adapter = new AliyunOssAdapter($client, $config); |
53
|
|
|
|
54
|
2 |
|
return $adapter; |
55
|
2 |
|
}); |
56
|
|
|
|
57
|
2 |
|
$this->app->bind('aliyun-oss.client', function () { |
58
|
1 |
|
$config = $this->app->get('config')->get('filesystem.disks.aliyun'); |
59
|
1 |
|
return new OssClient( |
60
|
1 |
|
$config['access_id'], |
61
|
1 |
|
$config['access_key'], |
62
|
1 |
|
$config['endpoint'], |
63
|
1 |
|
$config['is_cname'], |
64
|
1 |
|
$config['security_token'] |
65
|
|
|
); |
66
|
2 |
|
}); |
67
|
2 |
|
} |
68
|
|
|
} |
69
|
|
|
|