/vendor/league/flysystem/src/MountManager.php

https://gitlab.com/ealexis.t/trends · PHP · 271 lines · 110 code · 39 blank · 122 comment · 8 complexity · 265f52ef613a9ace1ca8a920f6276128 MD5 · raw file

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