PageRenderTime 144ms CodeModel.GetById 42ms RepoModel.GetById 8ms app.codeStats 0ms

/src/Illuminate/Filesystem/Filesystem.php

https://gitlab.com/ezeql/framework
PHP | 442 lines | 189 code | 54 blank | 199 comment | 18 complexity | d8ae520ef2d992e51545fe69cbe126e6 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Filesystem;
  3. use ErrorException;
  4. use FilesystemIterator;
  5. use Symfony\Component\Finder\Finder;
  6. use Illuminate\Support\Traits\Macroable;
  7. use Illuminate\Contracts\Filesystem\FileNotFoundException;
  8. class Filesystem
  9. {
  10. use Macroable;
  11. /**
  12. * Determine if a file exists.
  13. *
  14. * @param string $path
  15. * @return bool
  16. */
  17. public function exists($path)
  18. {
  19. return file_exists($path);
  20. }
  21. /**
  22. * Get the contents of a file.
  23. *
  24. * @param string $path
  25. * @return string
  26. *
  27. * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
  28. */
  29. public function get($path)
  30. {
  31. if ($this->isFile($path)) {
  32. return file_get_contents($path);
  33. }
  34. throw new FileNotFoundException("File does not exist at path {$path}");
  35. }
  36. /**
  37. * Get the returned value of a file.
  38. *
  39. * @param string $path
  40. * @return mixed
  41. *
  42. * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
  43. */
  44. public function getRequire($path)
  45. {
  46. if ($this->isFile($path)) {
  47. return require $path;
  48. }
  49. throw new FileNotFoundException("File does not exist at path {$path}");
  50. }
  51. /**
  52. * Require the given file once.
  53. *
  54. * @param string $file
  55. * @return mixed
  56. */
  57. public function requireOnce($file)
  58. {
  59. require_once $file;
  60. }
  61. /**
  62. * Write the contents of a file.
  63. *
  64. * @param string $path
  65. * @param string $contents
  66. * @param bool $lock
  67. * @return int
  68. */
  69. public function put($path, $contents, $lock = false)
  70. {
  71. return file_put_contents($path, $contents, $lock ? LOCK_EX : 0);
  72. }
  73. /**
  74. * Prepend to a file.
  75. *
  76. * @param string $path
  77. * @param string $data
  78. * @return int
  79. */
  80. public function prepend($path, $data)
  81. {
  82. if ($this->exists($path)) {
  83. return $this->put($path, $data.$this->get($path));
  84. }
  85. return $this->put($path, $data);
  86. }
  87. /**
  88. * Append to a file.
  89. *
  90. * @param string $path
  91. * @param string $data
  92. * @return int
  93. */
  94. public function append($path, $data)
  95. {
  96. return file_put_contents($path, $data, FILE_APPEND);
  97. }
  98. /**
  99. * Delete the file at a given path.
  100. *
  101. * @param string|array $paths
  102. * @return bool
  103. */
  104. public function delete($paths)
  105. {
  106. $paths = is_array($paths) ? $paths : func_get_args();
  107. $success = true;
  108. foreach ($paths as $path) {
  109. try {
  110. if (! @unlink($path)) {
  111. $success = false;
  112. }
  113. } catch (ErrorException $e) {
  114. $success = false;
  115. }
  116. }
  117. return $success;
  118. }
  119. /**
  120. * Move a file to a new location.
  121. *
  122. * @param string $path
  123. * @param string $target
  124. * @return bool
  125. */
  126. public function move($path, $target)
  127. {
  128. return rename($path, $target);
  129. }
  130. /**
  131. * Copy a file to a new location.
  132. *
  133. * @param string $path
  134. * @param string $target
  135. * @return bool
  136. */
  137. public function copy($path, $target)
  138. {
  139. return copy($path, $target);
  140. }
  141. /**
  142. * Extract the file name from a file path.
  143. *
  144. * @param string $path
  145. * @return string
  146. */
  147. public function name($path)
  148. {
  149. return pathinfo($path, PATHINFO_FILENAME);
  150. }
  151. /**
  152. * Extract the file extension from a file path.
  153. *
  154. * @param string $path
  155. * @return string
  156. */
  157. public function extension($path)
  158. {
  159. return pathinfo($path, PATHINFO_EXTENSION);
  160. }
  161. /**
  162. * Get the file type of a given file.
  163. *
  164. * @param string $path
  165. * @return string
  166. */
  167. public function type($path)
  168. {
  169. return filetype($path);
  170. }
  171. /**
  172. * Get the mime-type of a given file.
  173. *
  174. * @param string $path
  175. * @return string|false
  176. */
  177. public function mimeType($path)
  178. {
  179. return finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
  180. }
  181. /**
  182. * Get the file size of a given file.
  183. *
  184. * @param string $path
  185. * @return int
  186. */
  187. public function size($path)
  188. {
  189. return filesize($path);
  190. }
  191. /**
  192. * Get the file's last modification time.
  193. *
  194. * @param string $path
  195. * @return int
  196. */
  197. public function lastModified($path)
  198. {
  199. return filemtime($path);
  200. }
  201. /**
  202. * Determine if the given path is a directory.
  203. *
  204. * @param string $directory
  205. * @return bool
  206. */
  207. public function isDirectory($directory)
  208. {
  209. return is_dir($directory);
  210. }
  211. /**
  212. * Determine if the given path is writable.
  213. *
  214. * @param string $path
  215. * @return bool
  216. */
  217. public function isWritable($path)
  218. {
  219. return is_writable($path);
  220. }
  221. /**
  222. * Determine if the given path is a file.
  223. *
  224. * @param string $file
  225. * @return bool
  226. */
  227. public function isFile($file)
  228. {
  229. return is_file($file);
  230. }
  231. /**
  232. * Find path names matching a given pattern.
  233. *
  234. * @param string $pattern
  235. * @param int $flags
  236. * @return array
  237. */
  238. public function glob($pattern, $flags = 0)
  239. {
  240. return glob($pattern, $flags);
  241. }
  242. /**
  243. * Get an array of all files in a directory.
  244. *
  245. * @param string $directory
  246. * @return array
  247. */
  248. public function files($directory)
  249. {
  250. $glob = glob($directory.'/*');
  251. if ($glob === false) {
  252. return [];
  253. }
  254. // To get the appropriate files, we'll simply glob the directory and filter
  255. // out any "files" that are not truly files so we do not end up with any
  256. // directories in our list, but only true files within the directory.
  257. return array_filter($glob, function ($file) {
  258. return filetype($file) == 'file';
  259. });
  260. }
  261. /**
  262. * Get all of the files from the given directory (recursive).
  263. *
  264. * @param string $directory
  265. * @return array
  266. */
  267. public function allFiles($directory)
  268. {
  269. return iterator_to_array(Finder::create()->files()->in($directory), false);
  270. }
  271. /**
  272. * Get all of the directories within a given directory.
  273. *
  274. * @param string $directory
  275. * @return array
  276. */
  277. public function directories($directory)
  278. {
  279. $directories = [];
  280. foreach (Finder::create()->in($directory)->directories()->depth(0) as $dir) {
  281. $directories[] = $dir->getPathname();
  282. }
  283. return $directories;
  284. }
  285. /**
  286. * Create a directory.
  287. *
  288. * @param string $path
  289. * @param int $mode
  290. * @param bool $recursive
  291. * @param bool $force
  292. * @return bool
  293. */
  294. public function makeDirectory($path, $mode = 0755, $recursive = false, $force = false)
  295. {
  296. if ($force) {
  297. return @mkdir($path, $mode, $recursive);
  298. }
  299. return mkdir($path, $mode, $recursive);
  300. }
  301. /**
  302. * Copy a directory from one location to another.
  303. *
  304. * @param string $directory
  305. * @param string $destination
  306. * @param int $options
  307. * @return bool
  308. */
  309. public function copyDirectory($directory, $destination, $options = null)
  310. {
  311. if (! $this->isDirectory($directory)) {
  312. return false;
  313. }
  314. $options = $options ?: FilesystemIterator::SKIP_DOTS;
  315. // If the destination directory does not actually exist, we will go ahead and
  316. // create it recursively, which just gets the destination prepared to copy
  317. // the files over. Once we make the directory we'll proceed the copying.
  318. if (! $this->isDirectory($destination)) {
  319. $this->makeDirectory($destination, 0777, true);
  320. }
  321. $items = new FilesystemIterator($directory, $options);
  322. foreach ($items as $item) {
  323. // As we spin through items, we will check to see if the current file is actually
  324. // a directory or a file. When it is actually a directory we will need to call
  325. // back into this function recursively to keep copying these nested folders.
  326. $target = $destination.'/'.$item->getBasename();
  327. if ($item->isDir()) {
  328. $path = $item->getPathname();
  329. if (! $this->copyDirectory($path, $target, $options)) {
  330. return false;
  331. }
  332. }
  333. // If the current items is just a regular file, we will just copy this to the new
  334. // location and keep looping. If for some reason the copy fails we'll bail out
  335. // and return false, so the developer is aware that the copy process failed.
  336. else {
  337. if (! $this->copy($item->getPathname(), $target)) {
  338. return false;
  339. }
  340. }
  341. }
  342. return true;
  343. }
  344. /**
  345. * Recursively delete a directory.
  346. *
  347. * The directory itself may be optionally preserved.
  348. *
  349. * @param string $directory
  350. * @param bool $preserve
  351. * @return bool
  352. */
  353. public function deleteDirectory($directory, $preserve = false)
  354. {
  355. if (! $this->isDirectory($directory)) {
  356. return false;
  357. }
  358. $items = new FilesystemIterator($directory);
  359. foreach ($items as $item) {
  360. // If the item is a directory, we can just recurse into the function and
  361. // delete that sub-directory otherwise we'll just delete the file and
  362. // keep iterating through each file until the directory is cleaned.
  363. if ($item->isDir() && ! $item->isLink()) {
  364. $this->deleteDirectory($item->getPathname());
  365. }
  366. // If the item is just a file, we can go ahead and delete it since we're
  367. // just looping through and waxing all of the files in this directory
  368. // and calling directories recursively, so we delete the real path.
  369. else {
  370. $this->delete($item->getPathname());
  371. }
  372. }
  373. if (! $preserve) {
  374. @rmdir($directory);
  375. }
  376. return true;
  377. }
  378. /**
  379. * Empty the specified directory of all files and folders.
  380. *
  381. * @param string $directory
  382. * @return bool
  383. */
  384. public function cleanDirectory($directory)
  385. {
  386. return $this->deleteDirectory($directory, true);
  387. }
  388. }