/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php

https://gitlab.com/Pasantias/pasantiasASLG · PHP · 353 lines · 151 code · 38 blank · 164 comment · 10 complexity · 600fce796784d093d26ea406211f1fcc MD5 · raw file

  1. <?php
  2. namespace Illuminate\Filesystem;
  3. use InvalidArgumentException;
  4. use Illuminate\Support\Collection;
  5. use League\Flysystem\AdapterInterface;
  6. use League\Flysystem\FilesystemInterface;
  7. use League\Flysystem\FileNotFoundException;
  8. use Illuminate\Contracts\Filesystem\Filesystem as FilesystemContract;
  9. use Illuminate\Contracts\Filesystem\Cloud as CloudFilesystemContract;
  10. use Illuminate\Contracts\Filesystem\FileNotFoundException as ContractFileNotFoundException;
  11. class FilesystemAdapter implements FilesystemContract, CloudFilesystemContract
  12. {
  13. /**
  14. * The Flysystem filesystem implementation.
  15. *
  16. * @var \League\Flysystem\FilesystemInterface
  17. */
  18. protected $driver;
  19. /**
  20. * Create a new filesystem adapter instance.
  21. *
  22. * @param \League\Flysystem\FilesystemInterface $driver
  23. * @return void
  24. */
  25. public function __construct(FilesystemInterface $driver)
  26. {
  27. $this->driver = $driver;
  28. }
  29. /**
  30. * Determine if a file exists.
  31. *
  32. * @param string $path
  33. * @return bool
  34. */
  35. public function exists($path)
  36. {
  37. return $this->driver->has($path);
  38. }
  39. /**
  40. * Get the contents of a file.
  41. *
  42. * @param string $path
  43. * @return string
  44. *
  45. * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
  46. */
  47. public function get($path)
  48. {
  49. try {
  50. return $this->driver->read($path);
  51. } catch (FileNotFoundException $e) {
  52. throw new ContractFileNotFoundException($path, $e->getCode(), $e);
  53. }
  54. }
  55. /**
  56. * Write the contents of a file.
  57. *
  58. * @param string $path
  59. * @param string|resource $contents
  60. * @param string $visibility
  61. * @return bool
  62. */
  63. public function put($path, $contents, $visibility = null)
  64. {
  65. if ($visibility = $this->parseVisibility($visibility)) {
  66. $config = ['visibility' => $visibility];
  67. } else {
  68. $config = [];
  69. }
  70. if (is_resource($contents)) {
  71. return $this->driver->putStream($path, $contents, $config);
  72. } else {
  73. return $this->driver->put($path, $contents, $config);
  74. }
  75. }
  76. /**
  77. * Get the visibility for the given path.
  78. *
  79. * @param string $path
  80. * @return string
  81. */
  82. public function getVisibility($path)
  83. {
  84. if ($this->driver->getVisibility($path) == AdapterInterface::VISIBILITY_PUBLIC) {
  85. return FilesystemContract::VISIBILITY_PUBLIC;
  86. }
  87. return FilesystemContract::VISIBILITY_PRIVATE;
  88. }
  89. /**
  90. * Set the visibility for the given path.
  91. *
  92. * @param string $path
  93. * @param string $visibility
  94. * @return void
  95. */
  96. public function setVisibility($path, $visibility)
  97. {
  98. return $this->driver->setVisibility($path, $this->parseVisibility($visibility));
  99. }
  100. /**
  101. * Prepend to a file.
  102. *
  103. * @param string $path
  104. * @param string $data
  105. * @return int
  106. */
  107. public function prepend($path, $data)
  108. {
  109. if ($this->exists($path)) {
  110. return $this->put($path, $data.PHP_EOL.$this->get($path));
  111. }
  112. return $this->put($path, $data);
  113. }
  114. /**
  115. * Append to a file.
  116. *
  117. * @param string $path
  118. * @param string $data
  119. * @return int
  120. */
  121. public function append($path, $data)
  122. {
  123. if ($this->exists($path)) {
  124. return $this->put($path, $this->get($path).PHP_EOL.$data);
  125. }
  126. return $this->put($path, $data);
  127. }
  128. /**
  129. * Delete the file at a given path.
  130. *
  131. * @param string|array $paths
  132. * @return bool
  133. */
  134. public function delete($paths)
  135. {
  136. $paths = is_array($paths) ? $paths : func_get_args();
  137. foreach ($paths as $path) {
  138. $this->driver->delete($path);
  139. }
  140. return true;
  141. }
  142. /**
  143. * Copy a file to a new location.
  144. *
  145. * @param string $from
  146. * @param string $to
  147. * @return bool
  148. */
  149. public function copy($from, $to)
  150. {
  151. return $this->driver->copy($from, $to);
  152. }
  153. /**
  154. * Move a file to a new location.
  155. *
  156. * @param string $from
  157. * @param string $to
  158. * @return bool
  159. */
  160. public function move($from, $to)
  161. {
  162. return $this->driver->rename($from, $to);
  163. }
  164. /**
  165. * Get the file size of a given file.
  166. *
  167. * @param string $path
  168. * @return int
  169. */
  170. public function size($path)
  171. {
  172. return $this->driver->getSize($path);
  173. }
  174. /**
  175. * Get the mime-type of a given file.
  176. *
  177. * @param string $path
  178. * @return string|false
  179. */
  180. public function mimeType($path)
  181. {
  182. return $this->driver->getMimetype($path);
  183. }
  184. /**
  185. * Get the file's last modification time.
  186. *
  187. * @param string $path
  188. * @return int
  189. */
  190. public function lastModified($path)
  191. {
  192. return $this->driver->getTimestamp($path);
  193. }
  194. /**
  195. * Get an array of all files in a directory.
  196. *
  197. * @param string|null $directory
  198. * @param bool $recursive
  199. * @return array
  200. */
  201. public function files($directory = null, $recursive = false)
  202. {
  203. $contents = $this->driver->listContents($directory, $recursive);
  204. return $this->filterContentsByType($contents, 'file');
  205. }
  206. /**
  207. * Get all of the files from the given directory (recursive).
  208. *
  209. * @param string|null $directory
  210. * @return array
  211. */
  212. public function allFiles($directory = null)
  213. {
  214. return $this->files($directory, true);
  215. }
  216. /**
  217. * Get all of the directories within a given directory.
  218. *
  219. * @param string|null $directory
  220. * @param bool $recursive
  221. * @return array
  222. */
  223. public function directories($directory = null, $recursive = false)
  224. {
  225. $contents = $this->driver->listContents($directory, $recursive);
  226. return $this->filterContentsByType($contents, 'dir');
  227. }
  228. /**
  229. * Get all (recursive) of the directories within a given directory.
  230. *
  231. * @param string|null $directory
  232. * @return array
  233. */
  234. public function allDirectories($directory = null)
  235. {
  236. return $this->directories($directory, true);
  237. }
  238. /**
  239. * Create a directory.
  240. *
  241. * @param string $path
  242. * @return bool
  243. */
  244. public function makeDirectory($path)
  245. {
  246. return $this->driver->createDir($path);
  247. }
  248. /**
  249. * Recursively delete a directory.
  250. *
  251. * @param string $directory
  252. * @return bool
  253. */
  254. public function deleteDirectory($directory)
  255. {
  256. return $this->driver->deleteDir($directory);
  257. }
  258. /**
  259. * Get the Flysystem driver.
  260. *
  261. * @return \League\Flysystem\FilesystemInterface
  262. */
  263. public function getDriver()
  264. {
  265. return $this->driver;
  266. }
  267. /**
  268. * Filter directory contents by type.
  269. *
  270. * @param array $contents
  271. * @param string $type
  272. * @return array
  273. */
  274. protected function filterContentsByType($contents, $type)
  275. {
  276. return Collection::make($contents)
  277. ->where('type', $type)
  278. ->pluck('path')
  279. ->values()
  280. ->all();
  281. }
  282. /**
  283. * Parse the given visibility value.
  284. *
  285. * @param string|null $visibility
  286. * @return string|null
  287. * @throws \InvalidArgumentException
  288. */
  289. protected function parseVisibility($visibility)
  290. {
  291. if (is_null($visibility)) {
  292. return;
  293. }
  294. switch ($visibility) {
  295. case FilesystemContract::VISIBILITY_PUBLIC:
  296. return AdapterInterface::VISIBILITY_PUBLIC;
  297. case FilesystemContract::VISIBILITY_PRIVATE:
  298. return AdapterInterface::VISIBILITY_PRIVATE;
  299. }
  300. throw new InvalidArgumentException('Unknown visibility: '.$visibility);
  301. }
  302. /**
  303. * Pass dynamic methods call onto Flysystem.
  304. *
  305. * @param string $method
  306. * @param array $parameters
  307. * @return mixed
  308. *
  309. * @throws \BadMethodCallException
  310. */
  311. public function __call($method, array $parameters)
  312. {
  313. return call_user_func_array([$this->driver, $method], $parameters);
  314. }
  315. }