PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Streaming-Safe-for-Kids/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php

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