1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the 2amigos/yii2-flysystem-component project. |
4
|
|
|
* |
5
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view |
8
|
|
|
* the LICENSE file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace dosamigos\flysystem; |
11
|
|
|
|
12
|
|
|
use dosamigos\flysystem\cache\YiiCache; |
13
|
|
|
use League\Flysystem\AdapterInterface; |
14
|
|
|
use League\Flysystem\Cached\CachedAdapter; |
15
|
|
|
use League\Flysystem\Filesystem; |
16
|
|
|
use League\Flysystem\Replicate\ReplicateAdapter; |
17
|
|
|
use Yii; |
18
|
|
|
use yii\base\Component; |
19
|
|
|
use yii\base\InvalidConfigException; |
20
|
|
|
use yii\caching\Cache; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Filesystem |
24
|
|
|
* |
25
|
|
|
* @method \League\Flysystem\FilesystemInterface addPlugin(\League\Flysystem\PluginInterface $plugin) |
26
|
|
|
* @method void assertAbsent(string $path) |
27
|
|
|
* @method void assertPresent(string $path) |
28
|
|
|
* @method boolean copy(string $path, string $newpath) |
29
|
|
|
* @method boolean createDir(string $dirname, array $config = null) |
30
|
|
|
* @method boolean delete(string $path) |
31
|
|
|
* @method boolean deleteDir(string $dirname) |
32
|
|
|
* @method \League\Flysystem\Handler get(string $path, \League\Flysystem\Handler $handler = null) |
33
|
|
|
* @method \League\Flysystem\AdapterInterface getAdapter() |
34
|
|
|
* @method \League\Flysystem\Config getConfig() |
35
|
|
|
* @method array|false getMetadata(string $path) |
36
|
|
|
* @method string|false getMimetype(string $path) |
37
|
|
|
* @method integer|false getSize(string $path) |
38
|
|
|
* @method integer|false getTimestamp(string $path) |
39
|
|
|
* @method string|false getVisibility(string $path) |
40
|
|
|
* @method array getWithMetadata(string $path, array $metadata) |
41
|
|
|
* @method boolean has(string $path) |
42
|
|
|
* @method array listContents(string $directory = '', boolean $recursive = false) |
43
|
|
|
* @method array listFiles(string $path = '', boolean $recursive = false) |
44
|
|
|
* @method array listPaths(string $path = '', boolean $recursive = false) |
45
|
|
|
* @method array listWith(array $keys = [], $directory = '', $recursive = false) |
46
|
|
|
* @method boolean put(string $path, string $contents, array $config = []) |
47
|
|
|
* @method boolean putStream(string $path, resource $resource, array $config = []) |
48
|
|
|
* @method string|false read(string $path) |
49
|
|
|
* @method string|false readAndDelete(string $path) |
50
|
|
|
* @method resource|false readStream(string $path) |
51
|
|
|
* @method boolean rename(string $path, string $newpath) |
52
|
|
|
* @method boolean setVisibility(string $path, string $visibility) |
53
|
|
|
* @method boolean update(string $path, string $contents, array $config = []) |
54
|
|
|
* @method boolean updateStream(string $path, resource $resource, array $config = []) |
55
|
|
|
* @method boolean write(string $path, string $contents, array $config = []) |
56
|
|
|
* @method boolean writeStream(string $path, resource $resource, array $config = []) |
57
|
|
|
*/ |
58
|
|
|
abstract class AbstractFsComponent extends Component |
59
|
|
|
{ |
60
|
|
|
/** |
61
|
|
|
* @var \League\Flysystem\Config|array|string|null |
62
|
|
|
*/ |
63
|
|
|
public $config; |
64
|
|
|
/** |
65
|
|
|
* @var string|null |
66
|
|
|
*/ |
67
|
|
|
public $cache; |
68
|
|
|
/** |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
public $cacheKey = 'flysystem'; |
72
|
|
|
/** |
73
|
|
|
* @var integer |
74
|
|
|
*/ |
75
|
|
|
public $cacheDuration = 3600; |
76
|
|
|
/** |
77
|
|
|
* @var string|null |
78
|
|
|
*/ |
79
|
|
|
public $replica; |
80
|
|
|
/** |
81
|
|
|
* @var \League\Flysystem\FilesystemInterface |
82
|
|
|
*/ |
83
|
|
|
protected $filesystem; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritdoc |
87
|
|
|
*/ |
88
|
|
|
public function __call($method, $parameters) |
89
|
|
|
{ |
90
|
|
|
return call_user_func_array([$this->filesystem, $method], $parameters); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritdoc |
95
|
|
|
*/ |
96
|
|
|
public function init() |
97
|
|
|
{ |
98
|
|
|
$adapter = $this->checkReplica($this->checkCached($this->initAdapter())); |
99
|
|
|
|
100
|
|
|
$this->filesystem = new Filesystem($adapter, $this->config); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param AdapterInterface $adapter |
105
|
|
|
* |
106
|
|
|
* @return AdapterInterface|CachedAdapter |
107
|
|
|
* @throws InvalidConfigException |
108
|
|
|
*/ |
109
|
|
|
protected function checkCached(AdapterInterface $adapter) |
110
|
|
|
{ |
111
|
|
|
if (null !== $this->cache) { |
112
|
|
|
/* @var Cache $cache */ |
113
|
|
|
$cache = Yii::$app->get($this->cache); |
114
|
|
|
if (!$cache instanceof Cache) { |
115
|
|
|
throw new InvalidConfigException( |
116
|
|
|
printf('The "cache" property must be an instance of %s subclasses.', Cache::class) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
$adapter = new CachedAdapter($adapter, new YiiCache($cache, $this->cacheKey, $this->cacheDuration)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $adapter; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param AdapterInterface $adapter |
127
|
|
|
* |
128
|
|
|
* @return ReplicateAdapter|AdapterInterface |
129
|
|
|
* @throws InvalidConfigException |
130
|
|
|
*/ |
131
|
|
|
protected function checkReplica(AdapterInterface $adapter) |
132
|
|
|
{ |
133
|
|
|
if ($this->replica !== null) { |
134
|
|
|
/* @var Filesystem $filesystem */ |
135
|
|
|
$filesystem = Yii::$app->get($this->replica); |
136
|
|
|
if (!$filesystem instanceof Filesystem) { |
137
|
|
|
throw new InvalidConfigException( |
138
|
|
|
printf('The "replica" property must be an instance of %s subclasses.', AbstractFsComponent::class) |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
$adapter = new ReplicateAdapter($adapter, $filesystem->getAdapter()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $adapter; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return AdapterInterface $adapter |
149
|
|
|
*/ |
150
|
|
|
abstract protected function initAdapter(); |
151
|
|
|
} |
152
|
|
|
|