PluginProbe
WPIDE – File Manager & Code Editor / 3.5.9
WPIDE – File Manager & Code Editor v3.5.9
3.5.9 3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 All 55 releases
wpide / vendor / league / flysystem / src / MountManager.php

MountManager.php in WPIDE – File Manager & Code Editor 3.5.9, at vendor/league/flysystem/src/MountManager.php

649 lines 17.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace League\Flysystem;
4
5 use InvalidArgumentException;
6 use League\Flysystem\Plugin\PluggableTrait;
7 use League\Flysystem\Plugin\PluginNotFoundException;
8
9 /**
10 * Class MountManager.
11 *
12 * Proxies methods to Filesystem (@see __call):
13 *
14 * @method AdapterInterface getAdapter($prefix)
15 * @method Config getConfig($prefix)
16 * @method array listFiles($directory = '', $recursive = false)
17 * @method array listPaths($directory = '', $recursive = false)
18 * @method array getWithMetadata($path, array $metadata)
19 * @method Filesystem flushCache()
20 * @method void assertPresent($path)
21 * @method void assertAbsent($path)
22 * @method Filesystem addPlugin(PluginInterface $plugin)
23 */
24 class MountManager implements FilesystemInterface
25 {
26 use PluggableTrait;
27
28 /**
29 * @var FilesystemInterface[]
30 */
31 protected $filesystems = [];
32
33 /**
34 * Constructor.
35 *
36 * @param FilesystemInterface[] $filesystems [:prefix => Filesystem,]
37 *
38 * @throws InvalidArgumentException
39 */
40 public function __construct(array $filesystems = [])
41 {
42 $this->mountFilesystems($filesystems);
43 }
44
45 /**
46 * Mount filesystems.
47 *
48 * @param FilesystemInterface[] $filesystems [:prefix => Filesystem,]
49 *
50 * @throws InvalidArgumentException
51 *
52 * @return $this
53 */
54 public function mountFilesystems(array $filesystems)
55 {
56 foreach ($filesystems as $prefix => $filesystem) {
57 $this->mountFilesystem($prefix, $filesystem);
58 }
59
60 return $this;
61 }
62
63 /**
64 * Mount filesystems.
65 *
66 * @param string $prefix
67 * @param FilesystemInterface $filesystem
68 *
69 * @throws InvalidArgumentException
70 *
71 * @return $this
72 */
73 public function mountFilesystem($prefix, FilesystemInterface $filesystem)
74 {
75 if ( ! is_string($prefix)) {
76 throw new InvalidArgumentException(__METHOD__ . ' expects argument #1 to be a string.');
77 }
78
79 $this->filesystems[$prefix] = $filesystem;
80
81 return $this;
82 }
83
84 /**
85 * Get the filesystem with the corresponding prefix.
86 *
87 * @param string $prefix
88 *
89 * @throws FilesystemNotFoundException
90 *
91 * @return FilesystemInterface
92 */
93 public function getFilesystem($prefix)
94 {
95 if ( ! isset($this->filesystems[$prefix])) {
96 throw new FilesystemNotFoundException('No filesystem mounted with prefix ' . $prefix);
97 }
98
99 return $this->filesystems[$prefix];
100 }
101
102 /**
103 * Retrieve the prefix from an arguments array.
104 *
105 * @param array $arguments
106 *
107 * @throws InvalidArgumentException
108 *
109 * @return array [:prefix, :arguments]
110 */
111 public function filterPrefix(array $arguments)
112 {
113 if (empty($arguments)) {
114 throw new InvalidArgumentException('At least one argument needed');
115 }
116
117 $path = array_shift($arguments);
118
119 if ( ! is_string($path)) {
120 throw new InvalidArgumentException('First argument should be a string');
121 }
122
123 list($prefix, $path) = $this->getPrefixAndPath($path);
124 array_unshift($arguments, $path);
125
126 return [$prefix, $arguments];
127 }
128
129 /**
130 * @param string $directory
131 * @param bool $recursive
132 *
133 * @throws InvalidArgumentException
134 * @throws FilesystemNotFoundException
135 *
136 * @return array
137 */
138 public function listContents($directory = '', $recursive = false)
139 {
140 list($prefix, $directory) = $this->getPrefixAndPath($directory);
141 $filesystem = $this->getFilesystem($prefix);
142 $result = $filesystem->listContents($directory, $recursive);
143
144 foreach ($result as &$file) {
145 $file['filesystem'] = $prefix;
146 }
147
148 return $result;
149 }
150
151 /**
152 * Call forwarder.
153 *
154 * @param string $method
155 * @param array $arguments
156 *
157 * @throws InvalidArgumentException
158 * @throws FilesystemNotFoundException
159 *
160 * @return mixed
161 */
162 public function __call($method, $arguments)
163 {
164 list($prefix, $arguments) = $this->filterPrefix($arguments);
165
166 return $this->invokePluginOnFilesystem($method, $arguments, $prefix);
167 }
168
169 /**
170 * @param string $from
171 * @param string $to
172 * @param array $config
173 *
174 * @throws InvalidArgumentException
175 * @throws FilesystemNotFoundException
176 * @throws FileExistsException
177 *
178 * @return bool
179 */
180 public function copy($from, $to, array $config = [])
181 {
182 list($prefixFrom, $from) = $this->getPrefixAndPath($from);
183
184 $buffer = $this->getFilesystem($prefixFrom)->readStream($from);
185
186 if ($buffer === false) {
187 return false;
188 }
189
190 list($prefixTo, $to) = $this->getPrefixAndPath($to);
191
192 $result = $this->getFilesystem($prefixTo)->writeStream($to, $buffer, $config);
193
194 if (is_resource($buffer)) {
195 fclose($buffer);
196 }
197
198 return $result;
199 }
200
201 /**
202 * List with plugin adapter.
203 *
204 * @param array $keys
205 * @param string $directory
206 * @param bool $recursive
207 *
208 * @throws InvalidArgumentException
209 * @throws FilesystemNotFoundException
210 *
211 * @return array
212 */
213 public function listWith(array $keys = [], $directory = '', $recursive = false)
214 {
215 list($prefix, $directory) = $this->getPrefixAndPath($directory);
216 $arguments = [$keys, $directory, $recursive];
217
218 return $this->invokePluginOnFilesystem('listWith', $arguments, $prefix);
219 }
220
221 /**
222 * Move a file.
223 *
224 * @param string $from
225 * @param string $to
226 * @param array $config
227 *
228 * @throws InvalidArgumentException
229 * @throws FilesystemNotFoundException
230 *
231 * @return bool
232 */
233 public function move($from, $to, array $config = [])
234 {
235 list($prefixFrom, $pathFrom) = $this->getPrefixAndPath($from);
236 list($prefixTo, $pathTo) = $this->getPrefixAndPath($to);
237
238 if ($prefixFrom === $prefixTo) {
239 $filesystem = $this->getFilesystem($prefixFrom);
240 $renamed = $filesystem->rename($pathFrom, $pathTo);
241
242 if ($renamed && isset($config['visibility'])) {
243 return $filesystem->setVisibility($pathTo, $config['visibility']);
244 }
245
246 return $renamed;
247 }
248
249 $copied = $this->copy($from, $to, $config);
250
251 if ($copied) {
252 return $this->delete($from);
253 }
254
255 return false;
256 }
257
258 /**
259 * Invoke a plugin on a filesystem mounted on a given prefix.
260 *
261 * @param string $method
262 * @param array $arguments
263 * @param string $prefix
264 *
265 * @throws FilesystemNotFoundException
266 *
267 * @return mixed
268 */
269 public function invokePluginOnFilesystem($method, $arguments, $prefix)
270 {
271 $filesystem = $this->getFilesystem($prefix);
272
273 try {
274 return $this->invokePlugin($method, $arguments, $filesystem);
275 } catch (PluginNotFoundException $e) {
276 // Let it pass, it's ok, don't panic.
277 }
278
279 $callback = [$filesystem, $method];
280
281 return call_user_func_array($callback, $arguments);
282 }
283
284 /**
285 * @param string $path
286 *
287 * @throws InvalidArgumentException
288 *
289 * @return string[] [:prefix, :path]
290 */
291 protected function getPrefixAndPath($path)
292 {
293 if (strpos($path, '://') < 1) {
294 throw new InvalidArgumentException('No prefix detected in path: ' . $path);
295 }
296
297 return explode('://', $path, 2);
298 }
299
300 /**
301 * Check whether a file exists.
302 *
303 * @param string $path
304 *
305 * @return bool
306 */
307 public function has($path)
308 {
309 list($prefix, $path) = $this->getPrefixAndPath($path);
310
311 return $this->getFilesystem($prefix)->has($path);
312 }
313
314 /**
315 * Read a file.
316 *
317 * @param string $path The path to the file.
318 *
319 * @throws FileNotFoundException
320 *
321 * @return string|false The file contents or false on failure.
322 */
323 public function read($path)
324 {
325 list($prefix, $path) = $this->getPrefixAndPath($path);
326
327 return $this->getFilesystem($prefix)->read($path);
328 }
329
330 /**
331 * Retrieves a read-stream for a path.
332 *
333 * @param string $path The path to the file.
334 *
335 * @throws FileNotFoundException
336 *
337 * @return resource|false The path resource or false on failure.
338 */
339 public function readStream($path)
340 {
341 list($prefix, $path) = $this->getPrefixAndPath($path);
342
343 return $this->getFilesystem($prefix)->readStream($path);
344 }
345
346 /**
347 * Get a file's metadata.
348 *
349 * @param string $path The path to the file.
350 *
351 * @throws FileNotFoundException
352 *
353 * @return array|false The file metadata or false on failure.
354 */
355 public function getMetadata($path)
356 {
357 list($prefix, $path) = $this->getPrefixAndPath($path);
358
359 return $this->getFilesystem($prefix)->getMetadata($path);
360 }
361
362 /**
363 * Get a file's size.
364 *
365 * @param string $path The path to the file.
366 *
367 * @throws FileNotFoundException
368 *
369 * @return int|false The file size or false on failure.
370 */
371 public function getSize($path)
372 {
373 list($prefix, $path) = $this->getPrefixAndPath($path);
374
375 return $this->getFilesystem($prefix)->getSize($path);
376 }
377
378 /**
379 * Get a file's mime-type.
380 *
381 * @param string $path The path to the file.
382 *
383 * @throws FileNotFoundException
384 *
385 * @return string|false The file mime-type or false on failure.
386 */
387 public function getMimetype($path)
388 {
389 list($prefix, $path) = $this->getPrefixAndPath($path);
390
391 return $this->getFilesystem($prefix)->getMimetype($path);
392 }
393
394 /**
395 * Get a file's timestamp.
396 *
397 * @param string $path The path to the file.
398 *
399 * @throws FileNotFoundException
400 *
401 * @return string|false The timestamp or false on failure.
402 */
403 public function getTimestamp($path)
404 {
405 list($prefix, $path) = $this->getPrefixAndPath($path);
406
407 return $this->getFilesystem($prefix)->getTimestamp($path);
408 }
409
410 /**
411 * Get a file's visibility.
412 *
413 * @param string $path The path to the file.
414 *
415 * @throws FileNotFoundException
416 *
417 * @return string|false The visibility (public|private) or false on failure.
418 */
419 public function getVisibility($path)
420 {
421 list($prefix, $path) = $this->getPrefixAndPath($path);
422
423 return $this->getFilesystem($prefix)->getVisibility($path);
424 }
425
426 /**
427 * Write a new file.
428 *
429 * @param string $path The path of the new file.
430 * @param string $contents The file contents.
431 * @param array $config An optional configuration array.
432 *
433 * @throws FileExistsException
434 *
435 * @return bool True on success, false on failure.
436 */
437 public function write($path, $contents, array $config = [])
438 {
439 list($prefix, $path) = $this->getPrefixAndPath($path);
440
441 return $this->getFilesystem($prefix)->write($path, $contents, $config);
442 }
443
444 /**
445 * Write a new file using a stream.
446 *
447 * @param string $path The path of the new file.
448 * @param resource $resource The file handle.
449 * @param array $config An optional configuration array.
450 *
451 * @throws InvalidArgumentException If $resource is not a file handle.
452 * @throws FileExistsException
453 *
454 * @return bool True on success, false on failure.
455 */
456 public function writeStream($path, $resource, array $config = [])
457 {
458 list($prefix, $path) = $this->getPrefixAndPath($path);
459
460 return $this->getFilesystem($prefix)->writeStream($path, $resource, $config);
461 }
462
463 /**
464 * Update an existing file.
465 *
466 * @param string $path The path of the existing file.
467 * @param string $contents The file contents.
468 * @param array $config An optional configuration array.
469 *
470 * @throws FileNotFoundException
471 *
472 * @return bool True on success, false on failure.
473 */
474 public function update($path, $contents, array $config = [])
475 {
476 list($prefix, $path) = $this->getPrefixAndPath($path);
477
478 return $this->getFilesystem($prefix)->update($path, $contents, $config);
479 }
480
481 /**
482 * Update an existing file using a stream.
483 *
484 * @param string $path The path of the existing file.
485 * @param resource $resource The file handle.
486 * @param array $config An optional configuration array.
487 *
488 * @throws InvalidArgumentException If $resource is not a file handle.
489 * @throws FileNotFoundException
490 *
491 * @return bool True on success, false on failure.
492 */
493 public function updateStream($path, $resource, array $config = [])
494 {
495 list($prefix, $path) = $this->getPrefixAndPath($path);
496
497 return $this->getFilesystem($prefix)->updateStream($path, $resource, $config);
498 }
499
500 /**
501 * Rename a file.
502 *
503 * @param string $path Path to the existing file.
504 * @param string $newpath The new path of the file.
505 *
506 * @throws FileExistsException Thrown if $newpath exists.
507 * @throws FileNotFoundException Thrown if $path does not exist.
508 *
509 * @return bool True on success, false on failure.
510 */
511 public function rename($path, $newpath)
512 {
513 list($prefix, $path) = $this->getPrefixAndPath($path);
514
515 return $this->getFilesystem($prefix)->rename($path, $newpath);
516 }
517
518 /**
519 * Delete a file.
520 *
521 * @param string $path
522 *
523 * @throws FileNotFoundException
524 *
525 * @return bool True on success, false on failure.
526 */
527 public function delete($path)
528 {
529 list($prefix, $path) = $this->getPrefixAndPath($path);
530
531 return $this->getFilesystem($prefix)->delete($path);
532 }
533
534 /**
535 * Delete a directory.
536 *
537 * @param string $dirname
538 *
539 * @throws RootViolationException Thrown if $dirname is empty.
540 *
541 * @return bool True on success, false on failure.
542 */
543 public function deleteDir($dirname)
544 {
545 list($prefix, $dirname) = $this->getPrefixAndPath($dirname);
546
547 return $this->getFilesystem($prefix)->deleteDir($dirname);
548 }
549
550 /**
551 * Create a directory.
552 *
553 * @param string $dirname The name of the new directory.
554 * @param array $config An optional configuration array.
555 *
556 * @return bool True on success, false on failure.
557 */
558 public function createDir($dirname, array $config = [])
559 {
560 list($prefix, $dirname) = $this->getPrefixAndPath($dirname);
561
562 return $this->getFilesystem($prefix)->createDir($dirname);
563 }
564
565 /**
566 * Set the visibility for a file.
567 *
568 * @param string $path The path to the file.
569 * @param string $visibility One of 'public' or 'private'.
570 *
571 * @throws FileNotFoundException
572 *
573 * @return bool True on success, false on failure.
574 */
575 public function setVisibility($path, $visibility)
576 {
577 list($prefix, $path) = $this->getPrefixAndPath($path);
578
579 return $this->getFilesystem($prefix)->setVisibility($path, $visibility);
580 }
581
582 /**
583 * Create a file or update if exists.
584 *
585 * @param string $path The path to the file.
586 * @param string $contents The file contents.
587 * @param array $config An optional configuration array.
588 *
589 * @return bool True on success, false on failure.
590 */
591 public function put($path, $contents, array $config = [])
592 {
593 list($prefix, $path) = $this->getPrefixAndPath($path);
594
595 return $this->getFilesystem($prefix)->put($path, $contents, $config);
596 }
597
598 /**
599 * Create a file or update if exists.
600 *
601 * @param string $path The path to the file.
602 * @param resource $resource The file handle.
603 * @param array $config An optional configuration array.
604 *
605 * @throws InvalidArgumentException Thrown if $resource is not a resource.
606 *
607 * @return bool True on success, false on failure.
608 */
609 public function putStream($path, $resource, array $config = [])
610 {
611 list($prefix, $path) = $this->getPrefixAndPath($path);
612
613 return $this->getFilesystem($prefix)->putStream($path, $resource, $config);
614 }
615
616 /**
617 * Read and delete a file.
618 *
619 * @param string $path The path to the file.
620 *
621 * @throws FileNotFoundException
622 *
623 * @return string|false The file contents, or false on failure.
624 */
625 public function readAndDelete($path)
626 {
627 list($prefix, $path) = $this->getPrefixAndPath($path);
628
629 return $this->getFilesystem($prefix)->readAndDelete($path);
630 }
631
632 /**
633 * Get a file/directory handler.
634 *
635 * @deprecated
636 *
637 * @param string $path The path to the file.
638 * @param Handler $handler An optional existing handler to populate.
639 *
640 * @return Handler Either a file or directory handler.
641 */
642 public function get($path, Handler $handler = null)
643 {
644 list($prefix, $path) = $this->getPrefixAndPath($path);
645
646 return $this->getFilesystem($prefix)->get($path);
647 }
648 }
649