Filesystem.php 10 KB

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