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