PageRenderTime 35ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

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