PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/mdabutaleb/sentinel
PHP | 464 lines | 197 code | 56 blank | 211 comment | 18 complexity | 2a7c67e9ce8c395b6ab2130800e092aa 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 or directory 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 trailing name component from a file path.
  153. *
  154. * @param string $path
  155. * @return string
  156. */
  157. public function basename($path)
  158. {
  159. return pathinfo($path, PATHINFO_BASENAME);
  160. }
  161. /**
  162. * Extract the parent directory from a file path.
  163. *
  164. * @param string $path
  165. * @return string
  166. */
  167. public function dirname($path)
  168. {
  169. return pathinfo($path, PATHINFO_DIRNAME);
  170. }
  171. /**
  172. * Extract the file extension from a file path.
  173. *
  174. * @param string $path
  175. * @return string
  176. */
  177. public function extension($path)
  178. {
  179. return pathinfo($path, PATHINFO_EXTENSION);
  180. }
  181. /**
  182. * Get the file type of a given file.
  183. *
  184. * @param string $path
  185. * @return string
  186. */
  187. public function type($path)
  188. {
  189. return filetype($path);
  190. }
  191. /**
  192. * Get the mime-type of a given file.
  193. *
  194. * @param string $path
  195. * @return string|false
  196. */
  197. public function mimeType($path)
  198. {
  199. return finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
  200. }
  201. /**
  202. * Get the file size of a given file.
  203. *
  204. * @param string $path
  205. * @return int
  206. */
  207. public function size($path)
  208. {
  209. return filesize($path);
  210. }
  211. /**
  212. * Get the file's last modification time.
  213. *
  214. * @param string $path
  215. * @return int
  216. */
  217. public function lastModified($path)
  218. {
  219. return filemtime($path);
  220. }
  221. /**
  222. * Determine if the given path is a directory.
  223. *
  224. * @param string $directory
  225. * @return bool
  226. */
  227. public function isDirectory($directory)
  228. {
  229. return is_dir($directory);
  230. }
  231. /**
  232. * Determine if the given path is writable.
  233. *
  234. * @param string $path
  235. * @return bool
  236. */
  237. public function isWritable($path)
  238. {
  239. return is_writable($path);
  240. }
  241. /**
  242. * Determine if the given path is a file.
  243. *
  244. * @param string $file
  245. * @return bool
  246. */
  247. public function isFile($file)
  248. {
  249. return is_file($file);
  250. }
  251. /**
  252. * Find path names matching a given pattern.
  253. *
  254. * @param string $pattern
  255. * @param int $flags
  256. * @return array
  257. */
  258. public function glob($pattern, $flags = 0)
  259. {
  260. return glob($pattern, $flags);
  261. }
  262. /**
  263. * Get an array of all files in a directory.
  264. *
  265. * @param string $directory
  266. * @return array
  267. */
  268. public function files($directory)
  269. {
  270. $glob = glob($directory.'/*');
  271. if ($glob === false) {
  272. return [];
  273. }
  274. // To get the appropriate files, we'll simply glob the directory and filter
  275. // out any "files" that are not truly files so we do not end up with any
  276. // directories in our list, but only true files within the directory.
  277. return array_filter($glob, function ($file) {
  278. return filetype($file) == 'file';
  279. });
  280. }
  281. /**
  282. * Get all of the files from the given directory (recursive).
  283. *
  284. * @param string $directory
  285. * @return array
  286. */
  287. public function allFiles($directory)
  288. {
  289. return iterator_to_array(Finder::create()->files()->in($directory), false);
  290. }
  291. /**
  292. * Get all of the directories within a given directory.
  293. *
  294. * @param string $directory
  295. * @return array
  296. */
  297. public function directories($directory)
  298. {
  299. $directories = [];
  300. foreach (Finder::create()->in($directory)->directories()->depth(0) as $dir) {
  301. $directories[] = $dir->getPathname();
  302. }
  303. return $directories;
  304. }
  305. /**
  306. * Create a directory.
  307. *
  308. * @param string $path
  309. * @param int $mode
  310. * @param bool $recursive
  311. * @param bool $force
  312. * @return bool
  313. */
  314. public function makeDirectory($path, $mode = 0755, $recursive = false, $force = false)
  315. {
  316. if ($force) {
  317. return @mkdir($path, $mode, $recursive);
  318. }
  319. return mkdir($path, $mode, $recursive);
  320. }
  321. /**
  322. * Copy a directory from one location to another.
  323. *
  324. * @param string $directory
  325. * @param string $destination
  326. * @param int $options
  327. * @return bool
  328. */
  329. public function copyDirectory($directory, $destination, $options = null)
  330. {
  331. if (! $this->isDirectory($directory)) {
  332. return false;
  333. }
  334. $options = $options ?: FilesystemIterator::SKIP_DOTS;
  335. // If the destination directory does not actually exist, we will go ahead and
  336. // create it recursively, which just gets the destination prepared to copy
  337. // the files over. Once we make the directory we'll proceed the copying.
  338. if (! $this->isDirectory($destination)) {
  339. $this->makeDirectory($destination, 0777, true);
  340. }
  341. $items = new FilesystemIterator($directory, $options);
  342. foreach ($items as $item) {
  343. // As we spin through items, we will check to see if the current file is actually
  344. // a directory or a file. When it is actually a directory we will need to call
  345. // back into this function recursively to keep copying these nested folders.
  346. $target = $destination.'/'.$item->getBasename();
  347. if ($item->isDir()) {
  348. $path = $item->getPathname();
  349. if (! $this->copyDirectory($path, $target, $options)) {
  350. return false;
  351. }
  352. }
  353. // If the current items is just a regular file, we will just copy this to the new
  354. // location and keep looping. If for some reason the copy fails we'll bail out
  355. // and return false, so the developer is aware that the copy process failed.
  356. else {
  357. if (! $this->copy($item->getPathname(), $target)) {
  358. return false;
  359. }
  360. }
  361. }
  362. return true;
  363. }
  364. /**
  365. * Recursively delete a directory.
  366. *
  367. * The directory itself may be optionally preserved.
  368. *
  369. * @param string $directory
  370. * @param bool $preserve
  371. * @return bool
  372. */
  373. public function deleteDirectory($directory, $preserve = false)
  374. {
  375. if (! $this->isDirectory($directory)) {
  376. return false;
  377. }
  378. $items = new FilesystemIterator($directory);
  379. foreach ($items as $item) {
  380. // If the item is a directory, we can just recurse into the function and
  381. // delete that sub-directory otherwise we'll just delete the file and
  382. // keep iterating through each file until the directory is cleaned.
  383. if ($item->isDir() && ! $item->isLink()) {
  384. $this->deleteDirectory($item->getPathname());
  385. }
  386. // If the item is just a file, we can go ahead and delete it since we're
  387. // just looping through and waxing all of the files in this directory
  388. // and calling directories recursively, so we delete the real path.
  389. else {
  390. $this->delete($item->getPathname());
  391. }
  392. }
  393. if (! $preserve) {
  394. @rmdir($directory);
  395. }
  396. return true;
  397. }
  398. /**
  399. * Empty the specified directory of all files and folders.
  400. *
  401. * @param string $directory
  402. * @return bool
  403. */
  404. public function cleanDirectory($directory)
  405. {
  406. return $this->deleteDirectory($directory, true);
  407. }
  408. }