PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

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