|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/creocoder/yii2-flysystem |
|
4
|
|
|
* @copyright Copyright (c) 2015 Alexander Kochetov |
|
5
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace creocoder\flysystem; |
|
9
|
|
|
|
|
10
|
|
|
use League\Flysystem\AdapterInterface; |
|
11
|
|
|
use League\Flysystem\Cached\CachedAdapter; |
|
12
|
|
|
use League\Flysystem\Filesystem as NativeFilesystem; |
|
13
|
|
|
use League\Flysystem\Replicate\ReplicateAdapter; |
|
14
|
|
|
use Yii; |
|
15
|
|
|
use yii\base\Component; |
|
16
|
|
|
use yii\base\InvalidConfigException; |
|
17
|
|
|
use yii\caching\Cache; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Filesystem |
|
21
|
|
|
* |
|
22
|
|
|
* @method \League\Flysystem\FilesystemInterface addPlugin(\League\Flysystem\PluginInterface $plugin) |
|
23
|
|
|
* @method void assertAbsent(string $path) |
|
24
|
|
|
* @method void assertPresent(string $path) |
|
25
|
|
|
* @method boolean copy(string $path, string $newpath) |
|
26
|
|
|
* @method boolean createDir(string $dirname, array $config = null) |
|
27
|
|
|
* @method boolean delete(string $path) |
|
28
|
|
|
* @method boolean deleteDir(string $dirname) |
|
29
|
|
|
* @method \League\Flysystem\Handler get(string $path, \League\Flysystem\Handler $handler = null) |
|
30
|
|
|
* @method \League\Flysystem\AdapterInterface getAdapter() |
|
31
|
|
|
* @method \League\Flysystem\Config getConfig() |
|
32
|
|
|
* @method array|false getMetadata(string $path) |
|
33
|
|
|
* @method string|false getMimetype(string $path) |
|
34
|
|
|
* @method integer|false getSize(string $path) |
|
35
|
|
|
* @method integer|false getTimestamp(string $path) |
|
36
|
|
|
* @method string|false getVisibility(string $path) |
|
37
|
|
|
* @method array getWithMetadata(string $path, array $metadata) |
|
38
|
|
|
* @method boolean has(string $path) |
|
39
|
|
|
* @method array listContents(string $directory = '', boolean $recursive = false) |
|
40
|
|
|
* @method array listFiles(string $path = '', boolean $recursive = false) |
|
41
|
|
|
* @method array listPaths(string $path = '', boolean $recursive = false) |
|
42
|
|
|
* @method array listWith(array $keys = [], $directory = '', $recursive = false) |
|
43
|
|
|
* @method boolean put(string $path, string $contents, array $config = []) |
|
44
|
|
|
* @method boolean putStream(string $path, resource $resource, array $config = []) |
|
45
|
|
|
* @method string|false read(string $path) |
|
46
|
|
|
* @method string|false readAndDelete(string $path) |
|
47
|
|
|
* @method resource|false readStream(string $path) |
|
48
|
|
|
* @method boolean rename(string $path, string $newpath) |
|
49
|
|
|
* @method boolean setVisibility(string $path, string $visibility) |
|
50
|
|
|
* @method boolean update(string $path, string $contents, array $config = []) |
|
51
|
|
|
* @method boolean updateStream(string $path, resource $resource, array $config = []) |
|
52
|
|
|
* @method boolean write(string $path, string $contents, array $config = []) |
|
53
|
|
|
* @method boolean writeStream(string $path, resource $resource, array $config = []) |
|
54
|
|
|
* |
|
55
|
|
|
* @author Alexander Kochetov <[email protected]> |
|
56
|
|
|
*/ |
|
57
|
|
|
abstract class Filesystem extends Component |
|
58
|
|
|
{ |
|
59
|
|
|
/** |
|
60
|
|
|
* @var \League\Flysystem\Config|array|string|null |
|
61
|
|
|
*/ |
|
62
|
|
|
public $config; |
|
63
|
|
|
/** |
|
64
|
|
|
* @var string|null |
|
65
|
|
|
*/ |
|
66
|
|
|
public $cache; |
|
67
|
|
|
/** |
|
68
|
|
|
* @var string |
|
69
|
|
|
*/ |
|
70
|
|
|
public $cacheKey = 'flysystem'; |
|
71
|
|
|
/** |
|
72
|
|
|
* @var integer |
|
73
|
|
|
*/ |
|
74
|
|
|
public $cacheDuration = 3600; |
|
75
|
|
|
/** |
|
76
|
|
|
* @var string|null |
|
77
|
|
|
*/ |
|
78
|
|
|
public $replica; |
|
79
|
|
|
/** |
|
80
|
|
|
* @var \League\Flysystem\FilesystemInterface |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $filesystem; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @inheritdoc |
|
86
|
|
|
*/ |
|
87
|
|
|
public function init() |
|
88
|
|
|
{ |
|
89
|
|
|
$adapter = $this->prepareAdapter(); |
|
90
|
|
|
|
|
91
|
|
|
if ($this->cache !== null) { |
|
92
|
|
|
/* @var Cache $cache */ |
|
93
|
|
|
$cache = Yii::$app->get($this->cache); |
|
94
|
|
|
|
|
95
|
|
|
if (!$cache instanceof Cache) { |
|
96
|
|
|
throw new InvalidConfigException('The "cache" property must be an instance of \yii\caching\Cache subclasses.'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$adapter = new CachedAdapter($adapter, new YiiCache($cache, $this->cacheKey, $this->cacheDuration)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if ($this->replica !== null) { |
|
103
|
|
|
/* @var Filesystem $filesystem */ |
|
104
|
|
|
$filesystem = Yii::$app->get($this->replica); |
|
105
|
|
|
|
|
106
|
|
|
if (!$filesystem instanceof Filesystem) { |
|
107
|
|
|
throw new InvalidConfigException('The "replica" property must be an instance of \creocoder\flysystem\Filesystem subclasses.'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$adapter = new ReplicateAdapter($adapter, $filesystem->getAdapter()); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$this->filesystem = new NativeFilesystem($adapter, $this->config); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return AdapterInterface |
|
118
|
|
|
*/ |
|
119
|
|
|
abstract protected function prepareAdapter(); |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $method |
|
123
|
|
|
* @param array $parameters |
|
124
|
|
|
* @return mixed |
|
125
|
|
|
*/ |
|
126
|
|
|
public function __call($method, $parameters) |
|
127
|
|
|
{ |
|
128
|
|
|
return call_user_func_array([$this->filesystem, $method], $parameters); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|