PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/league/flysystem/src/Filesystem.php

https://gitlab.com/pthapa81/MeroSaaman-1.0
PHP | 573 lines | 250 code | 79 blank | 244 comment | 20 complexity | 9330f4e72a1054e22ed8d4a7ac890042 MD5 | raw file
  1. <?php
  2. namespace League\Flysystem;
  3. use BadMethodCallException;
  4. use InvalidArgumentException;
  5. use League\Flysystem\Plugin\PluggableTrait;
  6. use League\Flysystem\Plugin\PluginNotFoundException;
  7. /**
  8. * @method array getWithMetadata(string $path, array $metadata)
  9. * @method array listFiles(string $path = '', boolean $recursive = false)
  10. * @method array listPaths(string $path = '', boolean $recursive = false)
  11. * @method array listWith(array $keys = [], $directory = '', $recursive = false)
  12. */
  13. class Filesystem implements FilesystemInterface
  14. {
  15. use PluggableTrait;
  16. /**
  17. * @var AdapterInterface
  18. */
  19. protected $adapter;
  20. /**
  21. * @var Config
  22. */
  23. protected $config;
  24. /**
  25. * Constructor.
  26. *
  27. * @param AdapterInterface $adapter
  28. * @param Config|array $config
  29. */
  30. public function __construct(AdapterInterface $adapter, $config = null)
  31. {
  32. $this->adapter = $adapter;
  33. $this->config = Util::ensureConfig($config);
  34. }
  35. /**
  36. * Get the Adapter.
  37. *
  38. * @return AdapterInterface adapter
  39. */
  40. public function getAdapter()
  41. {
  42. return $this->adapter;
  43. }
  44. /**
  45. * Get the Config.
  46. *
  47. * @return Config config object
  48. */
  49. public function getConfig()
  50. {
  51. return $this->config;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function has($path)
  57. {
  58. $path = Util::normalizePath($path);
  59. return (bool) $this->adapter->has($path);
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function write($path, $contents, array $config = [])
  65. {
  66. $path = Util::normalizePath($path);
  67. $this->assertAbsent($path);
  68. $config = $this->prepareConfig($config);
  69. return (bool) $this->adapter->write($path, $contents, $config);
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function writeStream($path, $resource, array $config = [])
  75. {
  76. if (! is_resource($resource)) {
  77. throw new InvalidArgumentException(__METHOD__.' expects argument #2 to be a valid resource.');
  78. }
  79. $path = Util::normalizePath($path);
  80. $this->assertAbsent($path);
  81. $config = $this->prepareConfig($config);
  82. Util::rewindStream($resource);
  83. return (bool) $this->adapter->writeStream($path, $resource, $config);
  84. }
  85. /**
  86. * Create a file or update if exists.
  87. *
  88. * @param string $path path to file
  89. * @param string $contents file contents
  90. * @param mixed $config
  91. *
  92. * @throws FileExistsException
  93. *
  94. * @return bool success boolean
  95. */
  96. public function put($path, $contents, array $config = [])
  97. {
  98. $path = Util::normalizePath($path);
  99. if ($this->has($path)) {
  100. return $this->update($path, $contents, $config);
  101. }
  102. return $this->write($path, $contents, $config);
  103. }
  104. /**
  105. * Create a file or update if exists using a stream.
  106. *
  107. * @param string $path
  108. * @param resource $resource
  109. * @param mixed $config
  110. *
  111. * @return bool success boolean
  112. */
  113. public function putStream($path, $resource, array $config = [])
  114. {
  115. $path = Util::normalizePath($path);
  116. if ($this->has($path)) {
  117. return $this->updateStream($path, $resource, $config);
  118. }
  119. return $this->writeStream($path, $resource, $config);
  120. }
  121. /**
  122. * Read and delete a file.
  123. *
  124. * @param string $path
  125. *
  126. * @throws FileNotFoundException
  127. *
  128. * @return string file contents
  129. */
  130. public function readAndDelete($path)
  131. {
  132. $path = Util::normalizePath($path);
  133. $this->assertPresent($path);
  134. $contents = $this->read($path);
  135. if ($contents === false) {
  136. return false;
  137. }
  138. $this->delete($path);
  139. return $contents;
  140. }
  141. /**
  142. * Update a file.
  143. *
  144. * @param string $path path to file
  145. * @param string $contents file contents
  146. * @param mixed $config Config object or visibility setting
  147. *
  148. * @throws FileNotFoundException
  149. *
  150. * @return bool success boolean
  151. */
  152. public function update($path, $contents, array $config = [])
  153. {
  154. $path = Util::normalizePath($path);
  155. $config = $this->prepareConfig($config);
  156. $this->assertPresent($path);
  157. return (bool) $this->adapter->update($path, $contents, $config);
  158. }
  159. /**
  160. * Update a file with the contents of a stream.
  161. *
  162. * @param string $path
  163. * @param resource $resource
  164. * @param mixed $config Config object or visibility setting
  165. *
  166. * @throws InvalidArgumentException
  167. *
  168. * @return bool success boolean
  169. */
  170. public function updateStream($path, $resource, array $config = [])
  171. {
  172. if (! is_resource($resource)) {
  173. throw new InvalidArgumentException(__METHOD__.' expects argument #2 to be a valid resource.');
  174. }
  175. $path = Util::normalizePath($path);
  176. $config = $this->prepareConfig($config);
  177. $this->assertPresent($path);
  178. Util::rewindStream($resource);
  179. return (bool) $this->adapter->updateStream($path, $resource, $config);
  180. }
  181. /**
  182. * Read a file.
  183. *
  184. * @param string $path path to file
  185. *
  186. * @throws FileNotFoundException
  187. *
  188. * @return string|false file contents or FALSE when fails
  189. * to read existing file
  190. */
  191. public function read($path)
  192. {
  193. $path = Util::normalizePath($path);
  194. $this->assertPresent($path);
  195. if (! ($object = $this->adapter->read($path))) {
  196. return false;
  197. }
  198. return $object['contents'];
  199. }
  200. /**
  201. * Retrieves a read-stream for a path.
  202. *
  203. * @param string $path
  204. *
  205. * @return resource|false path resource or false when on failure
  206. */
  207. public function readStream($path)
  208. {
  209. $path = Util::normalizePath($path);
  210. $this->assertPresent($path);
  211. if (! $object = $this->adapter->readStream($path)) {
  212. return false;
  213. }
  214. return $object['stream'];
  215. }
  216. /**
  217. * Rename a file.
  218. *
  219. * @param string $path path to file
  220. * @param string $newpath new path
  221. *
  222. * @throws FileExistsException
  223. * @throws FileNotFoundException
  224. *
  225. * @return bool success boolean
  226. */
  227. public function rename($path, $newpath)
  228. {
  229. $path = Util::normalizePath($path);
  230. $newpath = Util::normalizePath($newpath);
  231. $this->assertPresent($path);
  232. $this->assertAbsent($newpath);
  233. return (bool) $this->adapter->rename($path, $newpath);
  234. }
  235. /**
  236. * Copy a file.
  237. *
  238. * @param string $path
  239. * @param string $newpath
  240. *
  241. * @return bool
  242. */
  243. public function copy($path, $newpath)
  244. {
  245. $path = Util::normalizePath($path);
  246. $newpath = Util::normalizePath($newpath);
  247. $this->assertPresent($path);
  248. $this->assertAbsent($newpath);
  249. return $this->adapter->copy($path, $newpath);
  250. }
  251. /**
  252. * Delete a file.
  253. *
  254. * @param string $path path to file
  255. *
  256. * @throws FileNotFoundException
  257. *
  258. * @return bool success boolean
  259. */
  260. public function delete($path)
  261. {
  262. $path = Util::normalizePath($path);
  263. $this->assertPresent($path);
  264. return $this->adapter->delete($path);
  265. }
  266. /**
  267. * Delete a directory.
  268. *
  269. * @param string $dirname path to directory
  270. *
  271. * @return bool success boolean
  272. */
  273. public function deleteDir($dirname)
  274. {
  275. $dirname = Util::normalizePath($dirname);
  276. if ($dirname === '') {
  277. throw new RootViolationException('Root directories can not be deleted.');
  278. }
  279. return (bool) $this->adapter->deleteDir($dirname);
  280. }
  281. /**
  282. * {@inheritdoc}
  283. */
  284. public function createDir($dirname, array $config = [])
  285. {
  286. $dirname = Util::normalizePath($dirname);
  287. $config = $this->prepareConfig($config);
  288. return (bool) $this->adapter->createDir($dirname, $config);
  289. }
  290. /**
  291. * List the filesystem contents.
  292. *
  293. * @param string $directory
  294. * @param bool $recursive
  295. *
  296. * @return array contents
  297. */
  298. public function listContents($directory = '', $recursive = false)
  299. {
  300. $directory = Util::normalizePath($directory);
  301. $contents = $this->adapter->listContents($directory, $recursive);
  302. $mapper = function ($entry) use ($directory, $recursive) {
  303. $entry = $entry + Util::pathinfo($entry['path']);
  304. if (! empty($directory) && strpos($entry['path'], $directory) === false) {
  305. return false;
  306. }
  307. if ($recursive === false && Util::dirname($entry['path']) !== $directory) {
  308. return false;
  309. }
  310. return $entry;
  311. };
  312. return array_values(array_filter(array_map($mapper, $contents)));
  313. }
  314. /**
  315. * Get a file's mime-type.
  316. *
  317. * @param string $path path to file
  318. *
  319. * @throws FileNotFoundException
  320. *
  321. * @return string|false file mime-type or FALSE when fails
  322. * to fetch mime-type from existing file
  323. */
  324. public function getMimetype($path)
  325. {
  326. $path = Util::normalizePath($path);
  327. $this->assertPresent($path);
  328. if (! $object = $this->adapter->getMimetype($path)) {
  329. return false;
  330. }
  331. return $object['mimetype'];
  332. }
  333. /**
  334. * Get a file's timestamp.
  335. *
  336. * @param string $path path to file
  337. *
  338. * @throws FileNotFoundException
  339. *
  340. * @return string|false timestamp or FALSE when fails
  341. * to fetch timestamp from existing file
  342. */
  343. public function getTimestamp($path)
  344. {
  345. $path = Util::normalizePath($path);
  346. $this->assertPresent($path);
  347. if (! $object = $this->adapter->getTimestamp($path)) {
  348. return false;
  349. }
  350. return $object['timestamp'];
  351. }
  352. /**
  353. * Get a file's visibility.
  354. *
  355. * @param string $path path to file
  356. *
  357. * @return string|false visibility (public|private) or FALSE
  358. * when fails to check it in existing file
  359. */
  360. public function getVisibility($path)
  361. {
  362. $path = Util::normalizePath($path);
  363. $this->assertPresent($path);
  364. if (($object = $this->adapter->getVisibility($path)) === false) {
  365. return false;
  366. }
  367. return $object['visibility'];
  368. }
  369. /**
  370. * Get a file's size.
  371. *
  372. * @param string $path path to file
  373. *
  374. * @return int|false file size or FALSE when fails
  375. * to check size of existing file
  376. */
  377. public function getSize($path)
  378. {
  379. $path = Util::normalizePath($path);
  380. if (($object = $this->adapter->getSize($path)) === false || !isset($object['size'])) {
  381. return false;
  382. }
  383. return (int) $object['size'];
  384. }
  385. /**
  386. * Get a file's size.
  387. *
  388. * @param string $path path to file
  389. * @param string $visibility visibility
  390. *
  391. * @return bool success boolean
  392. */
  393. public function setVisibility($path, $visibility)
  394. {
  395. $path = Util::normalizePath($path);
  396. return (bool) $this->adapter->setVisibility($path, $visibility);
  397. }
  398. /**
  399. * Get a file's metadata.
  400. *
  401. * @param string $path path to file
  402. *
  403. * @throws FileNotFoundException
  404. *
  405. * @return array|false file metadata or FALSE when fails
  406. * to fetch it from existing file
  407. */
  408. public function getMetadata($path)
  409. {
  410. $path = Util::normalizePath($path);
  411. $this->assertPresent($path);
  412. return $this->adapter->getMetadata($path);
  413. }
  414. /**
  415. * Get a file/directory handler.
  416. *
  417. * @param string $path
  418. * @param Handler $handler
  419. *
  420. * @return Handler file or directory handler
  421. */
  422. public function get($path, Handler $handler = null)
  423. {
  424. $path = Util::normalizePath($path);
  425. if (! $handler) {
  426. $metadata = $this->getMetadata($path);
  427. $handler = $metadata['type'] === 'file' ? new File($this, $path) : new Directory($this, $path);
  428. }
  429. $handler->setPath($path);
  430. $handler->setFilesystem($this);
  431. return $handler;
  432. }
  433. /**
  434. * Convert a config array to a Config object with the correct fallback.
  435. *
  436. * @param array $config
  437. *
  438. * @return Config
  439. */
  440. protected function prepareConfig(array $config)
  441. {
  442. $config = new Config($config);
  443. $config->setFallback($this->config);
  444. return $config;
  445. }
  446. /**
  447. * Assert a file is present.
  448. *
  449. * @param string $path path to file
  450. *
  451. * @throws FileNotFoundException
  452. */
  453. public function assertPresent($path)
  454. {
  455. if (! $this->has($path)) {
  456. throw new FileNotFoundException($path);
  457. }
  458. }
  459. /**
  460. * Assert a file is absent.
  461. *
  462. * @param string $path path to file
  463. *
  464. * @throws FileExistsException
  465. */
  466. public function assertAbsent($path)
  467. {
  468. if ($this->has($path)) {
  469. throw new FileExistsException($path);
  470. }
  471. }
  472. /**
  473. * Plugins pass-through.
  474. *
  475. * @param string $method
  476. * @param array $arguments
  477. *
  478. * @throws BadMethodCallException
  479. *
  480. * @return mixed
  481. */
  482. public function __call($method, array $arguments)
  483. {
  484. try {
  485. return $this->invokePlugin($method, $arguments, $this);
  486. } catch (PluginNotFoundException $e) {
  487. throw new BadMethodCallException(
  488. 'Call to undefined method '
  489. .__CLASS__
  490. .'::'.$method
  491. );
  492. }
  493. }
  494. }