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

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

https://gitlab.com/kimting254/wbms
PHP | 441 lines | 187 code | 55 blank | 199 comment | 16 complexity | caa246524352be6bd2c54489ae718007 MD5 | raw file
  1. <?php namespace Illuminate\Filesystem;
  2. use ErrorException;
  3. use FilesystemIterator;
  4. use Symfony\Component\Finder\Finder;
  5. use Illuminate\Support\Traits\Macroable;
  6. use Illuminate\Contracts\Filesystem\FileNotFoundException;
  7. class Filesystem {
  8. use Macroable;
  9. /**
  10. * Determine if a file exists.
  11. *
  12. * @param string $path
  13. * @return bool
  14. */
  15. public function exists($path)
  16. {
  17. return file_exists($path);
  18. }
  19. /**
  20. * Get the contents of a file.
  21. *
  22. * @param string $path
  23. * @return string
  24. *
  25. * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
  26. */
  27. public function get($path)
  28. {
  29. if ($this->isFile($path)) return file_get_contents($path);
  30. throw new FileNotFoundException("File does not exist at path {$path}");
  31. }
  32. /**
  33. * Get the returned value of a file.
  34. *
  35. * @param string $path
  36. * @return mixed
  37. *
  38. * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
  39. */
  40. public function getRequire($path)
  41. {
  42. if ($this->isFile($path)) return require $path;
  43. throw new FileNotFoundException("File does not exist at path {$path}");
  44. }
  45. /**
  46. * Require the given file once.
  47. *
  48. * @param string $file
  49. * @return mixed
  50. */
  51. public function requireOnce($file)
  52. {
  53. require_once $file;
  54. }
  55. /**
  56. * Write the contents of a file.
  57. *
  58. * @param string $path
  59. * @param string $contents
  60. * @param bool $lock
  61. * @return int
  62. */
  63. public function put($path, $contents, $lock = false)
  64. {
  65. return file_put_contents($path, $contents, $lock ? LOCK_EX : 0);
  66. }
  67. /**
  68. * Prepend to a file.
  69. *
  70. * @param string $path
  71. * @param string $data
  72. * @return int
  73. */
  74. public function prepend($path, $data)
  75. {
  76. if ($this->exists($path))
  77. {
  78. return $this->put($path, $data.$this->get($path));
  79. }
  80. return $this->put($path, $data);
  81. }
  82. /**
  83. * Append to a file.
  84. *
  85. * @param string $path
  86. * @param string $data
  87. * @return int
  88. */
  89. public function append($path, $data)
  90. {
  91. return file_put_contents($path, $data, FILE_APPEND);
  92. }
  93. /**
  94. * Delete the file at a given path.
  95. *
  96. * @param string|array $paths
  97. * @return bool
  98. */
  99. public function delete($paths)
  100. {
  101. $paths = is_array($paths) ? $paths : func_get_args();
  102. $success = true;
  103. foreach ($paths as $path)
  104. {
  105. try
  106. {
  107. if ( ! @unlink($path))
  108. {
  109. $success = false;
  110. }
  111. }
  112. catch (ErrorException $e)
  113. {
  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) return array();
  252. // To get the appropriate files, we'll simply glob the directory and filter
  253. // out any "files" that are not truly files so we do not end up with any
  254. // directories in our list, but only true files within the directory.
  255. return array_filter($glob, function($file)
  256. {
  257. return filetype($file) == 'file';
  258. });
  259. }
  260. /**
  261. * Get all of the files from the given directory (recursive).
  262. *
  263. * @param string $directory
  264. * @return array
  265. */
  266. public function allFiles($directory)
  267. {
  268. return iterator_to_array(Finder::create()->files()->in($directory), false);
  269. }
  270. /**
  271. * Get all of the directories within a given directory.
  272. *
  273. * @param string $directory
  274. * @return array
  275. */
  276. public function directories($directory)
  277. {
  278. $directories = array();
  279. foreach (Finder::create()->in($directory)->directories()->depth(0) as $dir)
  280. {
  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. {
  298. return @mkdir($path, $mode, $recursive);
  299. }
  300. return mkdir($path, $mode, $recursive);
  301. }
  302. /**
  303. * Copy a directory from one location to another.
  304. *
  305. * @param string $directory
  306. * @param string $destination
  307. * @param int $options
  308. * @return bool
  309. */
  310. public function copyDirectory($directory, $destination, $options = null)
  311. {
  312. if ( ! $this->isDirectory($directory)) return false;
  313. $options = $options ?: FilesystemIterator::SKIP_DOTS;
  314. // If the destination directory does not actually exist, we will go ahead and
  315. // create it recursively, which just gets the destination prepared to copy
  316. // the files over. Once we make the directory we'll proceed the copying.
  317. if ( ! $this->isDirectory($destination))
  318. {
  319. $this->makeDirectory($destination, 0777, true);
  320. }
  321. $items = new FilesystemIterator($directory, $options);
  322. foreach ($items as $item)
  323. {
  324. // As we spin through items, we will check to see if the current file is actually
  325. // a directory or a file. When it is actually a directory we will need to call
  326. // back into this function recursively to keep copying these nested folders.
  327. $target = $destination.'/'.$item->getBasename();
  328. if ($item->isDir())
  329. {
  330. $path = $item->getPathname();
  331. if ( ! $this->copyDirectory($path, $target, $options)) return false;
  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. {
  338. if ( ! $this->copy($item->getPathname(), $target)) return false;
  339. }
  340. }
  341. return true;
  342. }
  343. /**
  344. * Recursively delete a directory.
  345. *
  346. * The directory itself may be optionally preserved.
  347. *
  348. * @param string $directory
  349. * @param bool $preserve
  350. * @return bool
  351. */
  352. public function deleteDirectory($directory, $preserve = false)
  353. {
  354. if ( ! $this->isDirectory($directory)) return false;
  355. $items = new FilesystemIterator($directory);
  356. foreach ($items as $item)
  357. {
  358. // If the item is a directory, we can just recurse into the function and
  359. // delete that sub-directory otherwise we'll just delete the file and
  360. // keep iterating through each file until the directory is cleaned.
  361. if ($item->isDir() && ! $item->isLink())
  362. {
  363. $this->deleteDirectory($item->getPathname());
  364. }
  365. // If the item is just a file, we can go ahead and delete it since we're
  366. // just looping through and waxing all of the files in this directory
  367. // and calling directories recursively, so we delete the real path.
  368. else
  369. {
  370. $this->delete($item->getPathname());
  371. }
  372. }
  373. if ( ! $preserve) @rmdir($directory);
  374. return true;
  375. }
  376. /**
  377. * Empty the specified directory of all files and folders.
  378. *
  379. * @param string $directory
  380. * @return bool
  381. */
  382. public function cleanDirectory($directory)
  383. {
  384. return $this->deleteDirectory($directory, true);
  385. }
  386. }