Completed
Push — master ( 01716a...87843a )
by Xu
07:15
created

Filesystem::__isset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link http://www.tintsoft.com/
4
 * @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
5
 * @license http://www.tintsoft.com/license/
6
 */
7
8
namespace yuncms\filesystem;
9
10
use Closure;
11
use Yii;
12
use yii\base\Component;
13
use yii\base\InvalidConfigException;
14
15
/**
16
 * Class Storage
17
 *
18
 * @author Tongle Xu <[email protected]>
19
 * @since 3.0
20
 */
21
class Filesystem extends Component
22
{
23
    public $default = 'local';
24
25
    /**
26
     * @var array filesystem parameters (name => value).
27
     */
28
    public $params = [];
29
30
    /**
31
     * @var array shared disk instances indexed by their IDs
32
     */
33
    private $_filesystems = [];
34
35
    /**
36
     * @var array filesystem definitions indexed by their IDs
37
     */
38
    private $_definitions = [];
39
40
    /**
41
     * Getter magic method.
42
     * This method is overridden to support accessing filesystems like reading properties.
43
     * @param string $name filesystem or property name
44
     * @return mixed the named property value
45
     * @throws InvalidConfigException
46
     * @throws \yii\base\UnknownPropertyException
47
     */
48
    public function __get($name)
49
    {
50
        if ($this->has($name)) {
51
            return $this->get($name);
52
        }
53
54
        return parent::__get($name);
55
    }
56
57
    /**
58
     * Checks if a property value is null.
59
     * This method overrides the parent implementation by checking if the named filesystem is loaded.
60
     * @param string $name the property name or the event name
61
     * @return bool whether the property value is null
62
     */
63
    public function __isset($name)
64
    {
65
        if ($this->has($name)) {
66
            return true;
67
        }
68
69
        return parent::__isset($name);
70
    }
71
72
    /**
73
     * Returns a value indicating whether the locator has the specified filesystem definition or has instantiated the filesystem.
74
     * This method may return different results depending on the value of `$checkInstance`.
75
     *
76
     * - If `$checkInstance` is false (default), the method will return a value indicating whether the locator has the specified
77
     *   filesystem definition.
78
     * - If `$checkInstance` is true, the method will return a value indicating whether the locator has
79
     *   instantiated the specified filesystem.
80
     *
81
     * @param string $id filesystem ID (e.g. `local`).
82
     * @param bool $checkInstance whether the method should check if the filesystem is shared and instantiated.
83
     * @return bool whether the locator has the specified filesystem definition or has instantiated the filesystem.
84
     * @see set()
85
     */
86
    public function has($id, $checkInstance = false)
87
    {
88
        return $checkInstance ? isset($this->_filesystems[$id]) : isset($this->_definitions[$id]);
89
    }
90
91
    /**
92
     * Returns the filesystem instance with the specified ID.
93
     *
94
     * @param string $id filesystem ID (e.g. `db`).
95
     * @param bool $throwException whether to throw an exception if `$id` is not registered with the locator before.
96
     * @return \League\Flysystem\Filesystem|object|null the filesystem of the specified ID. If `$throwException` is false and `$id`
97
     * is not registered before, null will be returned.
98
     * @throws InvalidConfigException if `$id` refers to a nonexistent filesystem ID
99
     * @see has()
100
     * @see set()
101
     */
102
    public function get($id, $throwException = true)
103
    {
104
        if (isset($this->_filesystems[$id])) {
105
            return $this->_filesystems[$id];
106
        }
107
108
        if (isset($this->_definitions[$id])) {
109
            $definition = $this->_definitions[$id];
110
            if (is_object($definition) && !$definition instanceof Closure) {
111
                return $this->_filesystems[$id] = $definition;
112
            }
113
114
            return $this->_filesystems[$id] = Yii::createObject($definition);
115
        } elseif ($throwException) {
116
            throw new InvalidConfigException("Unknown filesystem ID: $id");
117
        }
118
119
        return null;
120
    }
121
122
    /**
123
     * Registers a filesystem definition with this locator.
124
     *
125
     * For example,
126
     *
127
     * ```php
128
     * // a class name
129
     * $locator->set('cache', 'yii\caching\FileCache');
130
     *
131
     * // a configuration array
132
     * $locator->set('db', [
133
     *     'class' => 'yii\db\Connection',
134
     *     'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
135
     *     'username' => 'root',
136
     *     'password' => '',
137
     *     'charset' => 'utf8',
138
     * ]);
139
     *
140
     * // an anonymous function
141
     * $locator->set('cache', function ($params) {
142
     *     return new \yii\caching\FileCache;
143
     * });
144
     *
145
     * // an instance
146
     * $locator->set('cache', new \yii\caching\FileCache);
147
     * ```
148
     *
149
     * If a filesystem definition with the same ID already exists, it will be overwritten.
150
     *
151
     * @param string $id filesystem ID (e.g. `db`).
152
     * @param mixed $definition the filesystem definition to be registered with this locator.
153
     * It can be one of the following:
154
     *
155
     * - a class name
156
     * - a configuration array: the array contains name-value pairs that will be used to
157
     *   initialize the property values of the newly created object when [[get()]] is called.
158
     *   The `class` element is required and stands for the the class of the object to be created.
159
     * - a PHP callable: either an anonymous function or an array representing a class method (e.g. `['Foo', 'bar']`).
160
     *   The callable will be called by [[get()]] to return an object associated with the specified filesystem ID.
161
     * - an object: When [[get()]] is called, this object will be returned.
162
     *
163
     * @throws InvalidConfigException if the definition is an invalid configuration array
164
     */
165
    public function set($id, $definition)
166
    {
167
        unset($this->_filesystems[$id]);
168
169
        if ($definition === null) {
170
            unset($this->_definitions[$id]);
171
            return;
172
        }
173
174
        if (is_object($definition) || is_callable($definition, true)) {
175
            // an object, a class name, or a PHP callable
176
            $this->_definitions[$id] = $definition;
177
        } elseif (is_array($definition)) {
178
            // a configuration array
179
            if (isset($definition['class'])) {
180
                $this->_definitions[$id] = $definition;
181
            } else {
182
                throw new InvalidConfigException("The configuration for the \"$id\" filesystem must contain a \"class\" element.");
183
            }
184
        } else {
185
            throw new InvalidConfigException("Unexpected configuration type for the \"$id\" filesystem: " . gettype($definition));
186
        }
187
    }
188
189
    /**
190
     * Removes the filesystem from the locator.
191
     * @param string $id the filesystem ID
192
     */
193
    public function clear($id)
194
    {
195
        unset($this->_definitions[$id], $this->_filesystems[$id]);
196
    }
197
198
    /**
199
     * 获取磁盘
200
     * @param string|null $filesystem
201
     * @return \League\Flysystem\Filesystem
202
     * @throws InvalidConfigException
203
     */
204
    public function disk($filesystem = null)
205
    {
206
        $filesystem = !is_null($filesystem) ? $filesystem : $this->default;
207
        return $this->get($filesystem);
208
    }
209
210
    /**
211
     * Returns the list of the filesystem definitions or the loaded filesystem instances.
212
     * @param bool $returnDefinitions whether to return filesystem definitions instead of the loaded filesystem instances.
213
     * @return array the list of the filesystem definitions or the loaded filesystem instances (ID => definition or instance).
214
     */
215
    public function getFilesystems($returnDefinitions = true)
216
    {
217
        return $returnDefinitions ? $this->_definitions : $this->_filesystems;
218
    }
219
220
    /**
221
     * Registers a set of filesystem definitions in this locator.
222
     *
223
     * This is the bulk version of [[set()]]. The parameter should be an array
224
     * whose keys are filesystem IDs and values the corresponding filesystem definitions.
225
     *
226
     * For more details on how to specify filesystem IDs and definitions, please refer to [[set()]].
227
     *
228
     * If a filesystem definition with the same ID already exists, it will be overwritten.
229
     *
230
     * The following is an example for registering two filesystem definitions:
231
     *
232
     * ```php
233
     * [
234
     *     'local' => [
235
     *         'class' => 'yuncms\filesystem\adapters\LocalAdapter',
236
     *         'path' => '@root/storage',
237
     *     ],
238
     * ]
239
     * ```
240
     *
241
     * @param array $filesystems filesystem definitions or instances
242
     * @throws InvalidConfigException
243
     */
244
    public function setFilesystems($filesystems)
245
    {
246
        foreach ($filesystems as $id => $filesystem) {
247
            $this->set($id, $filesystem);
248
        }
249
    }
250
}