forked from callmez/yii2-file-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collection.php
112 lines (103 loc) · 3.07 KB
/
Collection.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
<?php
namespace callmez\file\system;
use Yii;
use yii\base\Component;
use yii\base\InvalidCallException;
use yii\base\InvalidParamException;
use yii\base\InvalidConfigException;
use League\Flysystem\Filesystem;
class Collection extends Component
{
private $_fileSystems = [];
public function getFileSystems()
{
$fileSystems = [];
foreach ($this->_fileSystems as $id => $fileSystem) {
$fileSystems[$id] = $this->get($id);
}
return $fileSystems;
}
public function setFileSystems(array $fileSystems)
{
$this->_fileSystems = $fileSystems;
}
/**
* 获取指定的文件存储
* @param $id
* @return mixed
* @throws \yii\base\InvalidParamException
*/
public function get($id)
{
if (!$this->has($id)) {
throw new InvalidParamException("Unknown file system '{$id}'.");
}
if (!is_object($this->_fileSystems[$id]) || is_callable($this->_fileSystems[$id])) {
$this->_fileSystems[$id] = $this->create($id, $this->_fileSystems[$id]);
}
return $this->_fileSystems[$id];
}
/**
* 判断是否有文件存储
* @param $id
* @return bool
*/
public function has($id)
{
return array_key_exists($id, $this->_fileSystems);
}
/**
* 初始化文件存储
* @param $id
* @param $config
* @return object
* @throws \yii\base\InvalidConfigException
*/
protected function create($id, $config)
{
$object = Yii::createObject($config);
if (!($object instanceof Filesystem)) {
throw new InvalidConfigException("The file system class {$id} must extend from League\\Flysystem\\Filesystem.");
}
return $object;
}
/**
* Retrieve the prefix form an arguments array
*
* @param array $arguments
* @return array [:prefix, :arguments]
*/
protected function filterPrefix(array $arguments)
{
if (empty($arguments)) {
throw new InvalidCallException('At least one argument needed');
}
$path = array_shift($arguments);
if (!is_string($path)) {
throw new InvalidParamException('First argument should be a string');
}
if (!preg_match('#^[a-zA-Z0-9]+\:\/\/.*#', $path)) {
throw new InvalidParamException('No prefix detected in for path: ' . $path);
}
list ($prefix, $path) = explode('://', $path, 2);
array_unshift($arguments, $path);
return array($prefix, $arguments);
}
/**
* Call forwarder
*
* @param string $method
* @param array $arguments
* @return mixed
*/
public function __call($method, $arguments)
{
list($prefix, $arguments) = $this->filterPrefix($arguments);
$filesystem = $this->get($prefix);
if (method_exists($filesystem, $method)) {
$callback = array($filesystem, $method);
return call_user_func_array($callback, $arguments);
}
return parent::__call($method, $arguments);
}
}