PageRenderTime 49ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/puleeno/fiona-fashion
PHP | 400 lines | 223 code | 72 blank | 105 comment | 17 complexity | 3cf71e70d7b375daced900a0262638ec MD5 | raw file
  1. <?php
  2. namespace League\Flysystem;
  3. use InvalidArgumentException;
  4. use League\Flysystem\Plugin\PluggableTrait;
  5. use League\Flysystem\Util\ContentListingFormatter;
  6. /**
  7. * @method array getWithMetadata(string $path, array $metadata)
  8. * @method bool forceCopy(string $path, string $newpath)
  9. * @method bool forceRename(string $path, string $newpath)
  10. * @method array listFiles(string $path = '', boolean $recursive = false)
  11. * @method array listPaths(string $path = '', boolean $recursive = false)
  12. * @method array listWith(array $keys = [], $directory = '', $recursive = false)
  13. */
  14. class Filesystem implements FilesystemInterface
  15. {
  16. use PluggableTrait;
  17. use ConfigAwareTrait;
  18. /**
  19. * @var AdapterInterface
  20. */
  21. protected $adapter;
  22. /**
  23. * Constructor.
  24. *
  25. * @param AdapterInterface $adapter
  26. * @param Config|array $config
  27. */
  28. public function __construct(AdapterInterface $adapter, $config = null)
  29. {
  30. $this->adapter = $adapter;
  31. $this->setConfig($config);
  32. }
  33. /**
  34. * Get the Adapter.
  35. *
  36. * @return AdapterInterface adapter
  37. */
  38. public function getAdapter()
  39. {
  40. return $this->adapter;
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. public function has($path)
  46. {
  47. $path = Util::normalizePath($path);
  48. return strlen($path) === 0 ? false : (bool) $this->getAdapter()->has($path);
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. public function write($path, $contents, array $config = [])
  54. {
  55. $path = Util::normalizePath($path);
  56. $this->assertAbsent($path);
  57. $config = $this->prepareConfig($config);
  58. return (bool) $this->getAdapter()->write($path, $contents, $config);
  59. }
  60. /**
  61. * @inheritdoc
  62. */
  63. public function writeStream($path, $resource, array $config = [])
  64. {
  65. if ( ! is_resource($resource)) {
  66. throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.');
  67. }
  68. $path = Util::normalizePath($path);
  69. $this->assertAbsent($path);
  70. $config = $this->prepareConfig($config);
  71. Util::rewindStream($resource);
  72. return (bool) $this->getAdapter()->writeStream($path, $resource, $config);
  73. }
  74. /**
  75. * @inheritdoc
  76. */
  77. public function put($path, $contents, array $config = [])
  78. {
  79. $path = Util::normalizePath($path);
  80. $config = $this->prepareConfig($config);
  81. if ($this->has($path)) {
  82. return (bool) $this->getAdapter()->update($path, $contents, $config);
  83. }
  84. return (bool) $this->getAdapter()->write($path, $contents, $config);
  85. }
  86. /**
  87. * @inheritdoc
  88. */
  89. public function putStream($path, $resource, array $config = [])
  90. {
  91. if ( ! is_resource($resource)) {
  92. throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.');
  93. }
  94. $path = Util::normalizePath($path);
  95. $config = $this->prepareConfig($config);
  96. Util::rewindStream($resource);
  97. if ($this->has($path)) {
  98. return (bool) $this->getAdapter()->updateStream($path, $resource, $config);
  99. }
  100. return (bool) $this->getAdapter()->writeStream($path, $resource, $config);
  101. }
  102. /**
  103. * @inheritdoc
  104. */
  105. public function readAndDelete($path)
  106. {
  107. $path = Util::normalizePath($path);
  108. $this->assertPresent($path);
  109. $contents = $this->read($path);
  110. if ($contents === false) {
  111. return false;
  112. }
  113. $this->delete($path);
  114. return $contents;
  115. }
  116. /**
  117. * @inheritdoc
  118. */
  119. public function update($path, $contents, array $config = [])
  120. {
  121. $path = Util::normalizePath($path);
  122. $config = $this->prepareConfig($config);
  123. $this->assertPresent($path);
  124. return (bool) $this->getAdapter()->update($path, $contents, $config);
  125. }
  126. /**
  127. * @inheritdoc
  128. */
  129. public function updateStream($path, $resource, array $config = [])
  130. {
  131. if ( ! is_resource($resource)) {
  132. throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.');
  133. }
  134. $path = Util::normalizePath($path);
  135. $config = $this->prepareConfig($config);
  136. $this->assertPresent($path);
  137. Util::rewindStream($resource);
  138. return (bool) $this->getAdapter()->updateStream($path, $resource, $config);
  139. }
  140. /**
  141. * @inheritdoc
  142. */
  143. public function read($path)
  144. {
  145. $path = Util::normalizePath($path);
  146. $this->assertPresent($path);
  147. if ( ! ($object = $this->getAdapter()->read($path))) {
  148. return false;
  149. }
  150. return $object['contents'];
  151. }
  152. /**
  153. * @inheritdoc
  154. */
  155. public function readStream($path)
  156. {
  157. $path = Util::normalizePath($path);
  158. $this->assertPresent($path);
  159. if ( ! $object = $this->getAdapter()->readStream($path)) {
  160. return false;
  161. }
  162. return $object['stream'];
  163. }
  164. /**
  165. * @inheritdoc
  166. */
  167. public function rename($path, $newpath)
  168. {
  169. $path = Util::normalizePath($path);
  170. $newpath = Util::normalizePath($newpath);
  171. $this->assertPresent($path);
  172. $this->assertAbsent($newpath);
  173. return (bool) $this->getAdapter()->rename($path, $newpath);
  174. }
  175. /**
  176. * @inheritdoc
  177. */
  178. public function copy($path, $newpath)
  179. {
  180. $path = Util::normalizePath($path);
  181. $newpath = Util::normalizePath($newpath);
  182. $this->assertPresent($path);
  183. $this->assertAbsent($newpath);
  184. return $this->getAdapter()->copy($path, $newpath);
  185. }
  186. /**
  187. * @inheritdoc
  188. */
  189. public function delete($path)
  190. {
  191. $path = Util::normalizePath($path);
  192. $this->assertPresent($path);
  193. return $this->getAdapter()->delete($path);
  194. }
  195. /**
  196. * @inheritdoc
  197. */
  198. public function deleteDir($dirname)
  199. {
  200. $dirname = Util::normalizePath($dirname);
  201. if ($dirname === '') {
  202. throw new RootViolationException('Root directories can not be deleted.');
  203. }
  204. return (bool) $this->getAdapter()->deleteDir($dirname);
  205. }
  206. /**
  207. * @inheritdoc
  208. */
  209. public function createDir($dirname, array $config = [])
  210. {
  211. $dirname = Util::normalizePath($dirname);
  212. $config = $this->prepareConfig($config);
  213. return (bool) $this->getAdapter()->createDir($dirname, $config);
  214. }
  215. /**
  216. * @inheritdoc
  217. */
  218. public function listContents($directory = '', $recursive = false)
  219. {
  220. $directory = Util::normalizePath($directory);
  221. $contents = $this->getAdapter()->listContents($directory, $recursive);
  222. return (new ContentListingFormatter($directory, $recursive))->formatListing($contents);
  223. }
  224. /**
  225. * @inheritdoc
  226. */
  227. public function getMimetype($path)
  228. {
  229. $path = Util::normalizePath($path);
  230. $this->assertPresent($path);
  231. if ( ! $object = $this->getAdapter()->getMimetype($path)) {
  232. return false;
  233. }
  234. return $object['mimetype'];
  235. }
  236. /**
  237. * @inheritdoc
  238. */
  239. public function getTimestamp($path)
  240. {
  241. $path = Util::normalizePath($path);
  242. $this->assertPresent($path);
  243. if ( ! $object = $this->getAdapter()->getTimestamp($path)) {
  244. return false;
  245. }
  246. return $object['timestamp'];
  247. }
  248. /**
  249. * @inheritdoc
  250. */
  251. public function getVisibility($path)
  252. {
  253. $path = Util::normalizePath($path);
  254. $this->assertPresent($path);
  255. if (($object = $this->getAdapter()->getVisibility($path)) === false) {
  256. return false;
  257. }
  258. return $object['visibility'];
  259. }
  260. /**
  261. * @inheritdoc
  262. */
  263. public function getSize($path)
  264. {
  265. $path = Util::normalizePath($path);
  266. if (($object = $this->getAdapter()->getSize($path)) === false || ! isset($object['size'])) {
  267. return false;
  268. }
  269. return (int) $object['size'];
  270. }
  271. /**
  272. * @inheritdoc
  273. */
  274. public function setVisibility($path, $visibility)
  275. {
  276. $path = Util::normalizePath($path);
  277. return (bool) $this->getAdapter()->setVisibility($path, $visibility);
  278. }
  279. /**
  280. * @inheritdoc
  281. */
  282. public function getMetadata($path)
  283. {
  284. $path = Util::normalizePath($path);
  285. $this->assertPresent($path);
  286. return $this->getAdapter()->getMetadata($path);
  287. }
  288. /**
  289. * @inheritdoc
  290. */
  291. public function get($path, Handler $handler = null)
  292. {
  293. $path = Util::normalizePath($path);
  294. if ( ! $handler) {
  295. $metadata = $this->getMetadata($path);
  296. $handler = $metadata['type'] === 'file' ? new File($this, $path) : new Directory($this, $path);
  297. }
  298. $handler->setPath($path);
  299. $handler->setFilesystem($this);
  300. return $handler;
  301. }
  302. /**
  303. * Assert a file is present.
  304. *
  305. * @param string $path path to file
  306. *
  307. * @throws FileNotFoundException
  308. */
  309. public function assertPresent($path)
  310. {
  311. if ( ! $this->has($path)) {
  312. throw new FileNotFoundException($path);
  313. }
  314. }
  315. /**
  316. * Assert a file is absent.
  317. *
  318. * @param string $path path to file
  319. *
  320. * @throws FileExistsException
  321. */
  322. public function assertAbsent($path)
  323. {
  324. if ($this->has($path)) {
  325. throw new FileExistsException($path);
  326. }
  327. }
  328. }