Completed
Push — master ( a9a1c2...6d64f3 )
by Frank
04:02 waited 01:24
created

MountManager::getPrefixAndPath()   A

↳ Parent: MountManager

Complexity

Conditions 2
Paths 2

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 8
Code Lines 4

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace League\Flysystem;
4
5
use InvalidArgumentException;
6
use League\Flysystem\FilesystemNotFoundException;
7
use League\Flysystem\Plugin\PluggableTrait;
8
use League\Flysystem\Plugin\PluginNotFoundException;
9
10
/**
11
 * Class MountManager.
12
 *
13
 * Proxies methods to Filesystem (@see __call):
14
 *
15
 * @method AdapterInterface getAdapter($prefix)
16
 * @method Config getConfig($prefix)
17
 * @method bool has($path)
18
 * @method bool write($path, $contents, array $config = [])
19
 * @method bool writeStream($path, $resource, array $config = [])
20
 * @method bool put($path, $contents, $config = [])
21
 * @method bool putStream($path, $contents, $config = [])
22
 * @method string readAndDelete($path)
23
 * @method bool update($path, $contents, $config = [])
24
 * @method bool updateStream($path, $resource, $config = [])
25
 * @method string|false read($path)
26
 * @method resource|false readStream($path)
27
 * @method bool rename($path, $newpath)
28
 * @method bool delete($path)
29
 * @method bool deleteDir($dirname)
30
 * @method bool createDir($dirname, $config = [])
31
 * @method array listFiles($directory = '', $recursive = false)
32
 * @method array listPaths($directory = '', $recursive = false)
33
 * @method array getWithMetadata($path, array $metadata)
34
 * @method string|false getMimetype($path)
35
 * @method string|false getTimestamp($path)
36
 * @method string|false getVisibility($path)
37
 * @method int|false getSize($path);
38
 * @method bool setVisibility($path, $visibility)
39
 * @method array|false getMetadata($path)
40
 * @method Handler get($path, Handler $handler = null)
41
 * @method Filesystem flushCache()
42
 * @method void assertPresent($path)
43
 * @method void assertAbsent($path)
44
 * @method Filesystem addPlugin(PluginInterface $plugin)
45
 */
46
class MountManager
47
{
48
    use PluggableTrait;
49
50
    /**
51
     * @var FilesystemInterface[]
52
     */
53
    protected $filesystems = [];
54
55
    /**
56
     * Constructor.
57
     *
58
     * @param FilesystemInterface[] $filesystems [:prefix => Filesystem,]
59
     *
60
     * @throws InvalidArgumentException
61
     */
62 45
    public function __construct(array $filesystems = [])
63
    {
64 45
        $this->mountFilesystems($filesystems);
65 45
    }
66
67
    /**
68
     * Mount filesystems.
69
     *
70
     * @param FilesystemInterface[] $filesystems [:prefix => Filesystem,]
71
     *
72
     * @throws InvalidArgumentException
73
     *
74
     * @return $this
75
     */
76 45
    public function mountFilesystems(array $filesystems)
77
    {
78 45
        foreach ($filesystems as $prefix => $filesystem) {
79 3
            $this->mountFilesystem($prefix, $filesystem);
80 45
        }
81
82 45
        return $this;
83
    }
84
85
    /**
86
     * Mount filesystems.
87
     *
88
     * @param string              $prefix
89
     * @param FilesystemInterface $filesystem
90
     *
91
     * @throws InvalidArgumentException
92
     *
93
     * @return $this
94
     */
95 33
    public function mountFilesystem($prefix, FilesystemInterface $filesystem)
96
    {
97 33
        if ( ! is_string($prefix)) {
98 3
            throw new InvalidArgumentException(__METHOD__ . ' expects argument #1 to be a string.');
99
        }
100
101 30
        $this->filesystems[$prefix] = $filesystem;
102
103 30
        return $this;
104
    }
105
106
    /**
107
     * Get the filesystem with the corresponding prefix.
108
     *
109
     * @param string $prefix
110
     *
111
     * @throws FilesystemNotFoundException
112
     *
113
     * @return FilesystemInterface
114
     */
115 33
    public function getFilesystem($prefix)
116
    {
117 33
        if ( ! isset($this->filesystems[$prefix])) {
118 3
            throw new FilesystemNotFoundException('No filesystem mounted with prefix ' . $prefix);
119
        }
120
121 30
        return $this->filesystems[$prefix];
122
    }
123
124
    /**
125
     * Retrieve the prefix from an arguments array.
126
     *
127
     * @param array $arguments
128
     *
129
     * @throws InvalidArgumentException
130
     *
131
     * @return array [:prefix, :arguments]
132
     */
133 24
    public function filterPrefix(array $arguments)
134
    {
135 24
        if (empty($arguments)) {
136 3
            throw new InvalidArgumentException('At least one argument needed');
137
        }
138
139 21
        $path = array_shift($arguments);
140
141 21
        if ( ! is_string($path)) {
142 3
            throw new InvalidArgumentException('First argument should be a string');
143
        }
144
145 18
        list($prefix, $path) = $this->getPrefixAndPath($path);
146 15
        array_unshift($arguments, $path);
147
148 15
        return [$prefix, $arguments];
149
    }
150
151
    /**
152
     * @param string $directory
153
     * @param bool   $recursive
154
     *
155
     * @throws InvalidArgumentException
156
     * @throws FilesystemNotFoundException
157
     *
158
     * @return array
159
     */
160 3
    public function listContents($directory = '', $recursive = false)
161
    {
162 3
        list($prefix, $directory) = $this->getPrefixAndPath($directory);
163 3
        $filesystem = $this->getFilesystem($prefix);
164 3
        $result = $filesystem->listContents($directory, $recursive);
165
166 3
        foreach ($result as &$file) {
167 3
            $file['filesystem'] = $prefix;
168 3
        }
169
170 3
        return $result;
171
    }
172
173
    /**
174
     * Call forwarder.
175
     *
176
     * @param string $method
177
     * @param array  $arguments
178
     *
179
     * @throws InvalidArgumentException
180
     * @throws FilesystemNotFoundException
181
     *
182
     * @return mixed
183
     */
184 15
    public function __call($method, $arguments)
185
    {
186 15
        list($prefix, $arguments) = $this->filterPrefix($arguments);
187
188 15
        return $this->invokePluginOnFilesystem($method, $arguments, $prefix);
189
    }
190
191
    /**
192
     * @param string $from
193
     * @param string $to
194
     * @param array  $config
195
     *
196
     * @throws InvalidArgumentException
197
     * @throws FilesystemNotFoundException
198
     *
199
     * @return bool
200
     */
201 6
    public function copy($from, $to, array $config = [])
202
    {
203 6
        list($prefixFrom, $from) = $this->getPrefixAndPath($from);
204
205 6
        $buffer = $this->getFilesystem($prefixFrom)->readStream($from);
206
207 6
        if ($buffer === false) {
208 3
            return false;
209
        }
210
211 6
        list($prefixTo, $to) = $this->getPrefixAndPath($to);
212
213 6
        $result = $this->getFilesystem($prefixTo)->writeStream($to, $buffer, $config);
214
215 6
        if (is_resource($buffer)) {
216 6
            fclose($buffer);
217 6
        }
218
219 6
        return $result;
220
    }
221
222
    /**
223
     * List with plugin adapter.
224
     *
225
     * @param array  $keys
226
     * @param string $directory
227
     * @param bool   $recursive
228
     *
229
     * @throws InvalidArgumentException
230
     * @throws FilesystemNotFoundException
231
     *
232
     * @return array
233
     */
234 3
    public function listWith(array $keys = [], $directory = '', $recursive = false)
235
    {
236 3
        list($prefix, $directory) = $this->getPrefixAndPath($directory);
237 3
        $arguments = [$keys, $directory, $recursive];
238
239 3
        return $this->invokePluginOnFilesystem('listWith', $arguments, $prefix);
240
    }
241
242
    /**
243
     * Move a file.
244
     *
245
     * @param string $from
246
     * @param string $to
247
     * @param array  $config
248
     *
249
     * @throws InvalidArgumentException
250
     * @throws FilesystemNotFoundException
251
     *
252
     * @return bool
253
     */
254 3
    public function move($from, $to, array $config = [])
255
    {
256 3
        $copied = $this->copy($from, $to, $config);
257
258 3
        if ($copied) {
259 3
            return $this->delete($from);
260
        }
261
262 3
        return false;
263
    }
264
265
    /**
266
     * Invoke a plugin on a filesystem mounted on a given prefix.
267
     *
268
     * @param string $method
269
     * @param array  $arguments
270
     * @param string $prefix
271
     *
272
     * @throws FilesystemNotFoundException
273
     *
274
     * @return mixed
275
     */
276 18
    public function invokePluginOnFilesystem($method, $arguments, $prefix)
277
    {
278 18
        $filesystem = $this->getFilesystem($prefix);
279
280
        try {
281 18
            return $this->invokePlugin($method, $arguments, $filesystem);
282 18
        } catch (PluginNotFoundException $e) {
283
            // Let it pass, it's ok, don't panic.
284
        }
285
286 18
        $callback = [$filesystem, $method];
287
288 18
        return call_user_func_array($callback, $arguments);
289
    }
290
291
    /**
292
     * @param string $path
293
     *
294
     * @throws InvalidArgumentException
295
     *
296
     * @return string[] [:prefix, :path]
297
     */
298 30
    protected function getPrefixAndPath($path)
299
    {
300 30
        if (strpos($path, '://') < 1) {
301 3
            throw new InvalidArgumentException('No prefix detected in path: ' . $path);
302
        }
303
304 27
        return explode('://', $path, 2);
305
    }
306
}
307