MountManager.php 17 KB

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