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

https://gitlab.com/xolotsoft/pumasruiz · PHP · 412 lines · 166 code · 53 blank · 193 comment · 15 complexity · c09413220b1630f374979febb752e49c MD5 · raw file

  1. <?php namespace Illuminate\Filesystem;
  2. use FilesystemIterator;
  3. use Symfony\Component\Finder\Finder;
  4. class Filesystem {
  5. /**
  6. * Determine if a file exists.
  7. *
  8. * @param string $path
  9. * @return bool
  10. */
  11. public function exists($path)
  12. {
  13. return file_exists($path);
  14. }
  15. /**
  16. * Get the contents of a file.
  17. *
  18. * @param string $path
  19. * @return string
  20. *
  21. * @throws FileNotFoundException
  22. */
  23. public function get($path)
  24. {
  25. if ($this->isFile($path)) return file_get_contents($path);
  26. throw new FileNotFoundException("File does not exist at path {$path}");
  27. }
  28. /**
  29. * Get the returned value of a file.
  30. *
  31. * @param string $path
  32. * @return mixed
  33. *
  34. * @throws FileNotFoundException
  35. */
  36. public function getRequire($path)
  37. {
  38. if ($this->isFile($path)) return require $path;
  39. throw new FileNotFoundException("File does not exist at path {$path}");
  40. }
  41. /**
  42. * Require the given file once.
  43. *
  44. * @param string $file
  45. * @return mixed
  46. */
  47. public function requireOnce($file)
  48. {
  49. require_once $file;
  50. }
  51. /**
  52. * Write the contents of a file.
  53. *
  54. * @param string $path
  55. * @param string $contents
  56. * @param bool $lock
  57. * @return int
  58. */
  59. public function put($path, $contents, $lock = false)
  60. {
  61. return file_put_contents($path, $contents, $lock ? LOCK_EX : 0);
  62. }
  63. /**
  64. * Prepend to a file.
  65. *
  66. * @param string $path
  67. * @param string $data
  68. * @return int
  69. */
  70. public function prepend($path, $data)
  71. {
  72. if ($this->exists($path))
  73. {
  74. return $this->put($path, $data.$this->get($path));
  75. }
  76. return $this->put($path, $data);
  77. }
  78. /**
  79. * Append to a file.
  80. *
  81. * @param string $path
  82. * @param string $data
  83. * @return int
  84. */
  85. public function append($path, $data)
  86. {
  87. return file_put_contents($path, $data, FILE_APPEND);
  88. }
  89. /**
  90. * Delete the file at a given path.
  91. *
  92. * @param string|array $paths
  93. * @return bool
  94. */
  95. public function delete($paths)
  96. {
  97. $paths = is_array($paths) ? $paths : func_get_args();
  98. $success = true;
  99. foreach ($paths as $path) { if ( ! @unlink($path)) $success = false; }
  100. return $success;
  101. }
  102. /**
  103. * Move a file to a new location.
  104. *
  105. * @param string $path
  106. * @param string $target
  107. * @return bool
  108. */
  109. public function move($path, $target)
  110. {
  111. return rename($path, $target);
  112. }
  113. /**
  114. * Copy a file to a new location.
  115. *
  116. * @param string $path
  117. * @param string $target
  118. * @return bool
  119. */
  120. public function copy($path, $target)
  121. {
  122. return copy($path, $target);
  123. }
  124. /**
  125. * Extract the file name from a file path.
  126. *
  127. * @param string $path
  128. * @return string
  129. */
  130. public function name($path)
  131. {
  132. return pathinfo($path, PATHINFO_FILENAME);
  133. }
  134. /**
  135. * Extract the file extension from a file path.
  136. *
  137. * @param string $path
  138. * @return string
  139. */
  140. public function extension($path)
  141. {
  142. return pathinfo($path, PATHINFO_EXTENSION);
  143. }
  144. /**
  145. * Get the file type of a given file.
  146. *
  147. * @param string $path
  148. * @return string
  149. */
  150. public function type($path)
  151. {
  152. return filetype($path);
  153. }
  154. /**
  155. * Get the file size of a given file.
  156. *
  157. * @param string $path
  158. * @return int
  159. */
  160. public function size($path)
  161. {
  162. return filesize($path);
  163. }
  164. /**
  165. * Get the file's last modification time.
  166. *
  167. * @param string $path
  168. * @return int
  169. */
  170. public function lastModified($path)
  171. {
  172. return filemtime($path);
  173. }
  174. /**
  175. * Determine if the given path is a directory.
  176. *
  177. * @param string $directory
  178. * @return bool
  179. */
  180. public function isDirectory($directory)
  181. {
  182. return is_dir($directory);
  183. }
  184. /**
  185. * Determine if the given path is writable.
  186. *
  187. * @param string $path
  188. * @return bool
  189. */
  190. public function isWritable($path)
  191. {
  192. return is_writable($path);
  193. }
  194. /**
  195. * Determine if the given path is a file.
  196. *
  197. * @param string $file
  198. * @return bool
  199. */
  200. public function isFile($file)
  201. {
  202. return is_file($file);
  203. }
  204. /**
  205. * Find path names matching a given pattern.
  206. *
  207. * @param string $pattern
  208. * @param int $flags
  209. * @return array
  210. */
  211. public function glob($pattern, $flags = 0)
  212. {
  213. return glob($pattern, $flags);
  214. }
  215. /**
  216. * Get an array of all files in a directory.
  217. *
  218. * @param string $directory
  219. * @return array
  220. */
  221. public function files($directory)
  222. {
  223. $glob = glob($directory.'/*');
  224. if ($glob === false) return array();
  225. // To get the appropriate files, we'll simply glob the directory and filter
  226. // out any "files" that are not truly files so we do not end up with any
  227. // directories in our list, but only true files within the directory.
  228. return array_filter($glob, function($file)
  229. {
  230. return filetype($file) == 'file';
  231. });
  232. }
  233. /**
  234. * Get all of the files from the given directory (recursive).
  235. *
  236. * @param string $directory
  237. * @return array
  238. */
  239. public function allFiles($directory)
  240. {
  241. return iterator_to_array(Finder::create()->files()->in($directory), false);
  242. }
  243. /**
  244. * Get all of the directories within a given directory.
  245. *
  246. * @param string $directory
  247. * @return array
  248. */
  249. public function directories($directory)
  250. {
  251. $directories = array();
  252. foreach (Finder::create()->in($directory)->directories()->depth(0) as $dir)
  253. {
  254. $directories[] = $dir->getPathname();
  255. }
  256. return $directories;
  257. }
  258. /**
  259. * Create a directory.
  260. *
  261. * @param string $path
  262. * @param int $mode
  263. * @param bool $recursive
  264. * @param bool $force
  265. * @return bool
  266. */
  267. public function makeDirectory($path, $mode = 0755, $recursive = false, $force = false)
  268. {
  269. if ($force)
  270. {
  271. return @mkdir($path, $mode, $recursive);
  272. }
  273. return mkdir($path, $mode, $recursive);
  274. }
  275. /**
  276. * Copy a directory from one location to another.
  277. *
  278. * @param string $directory
  279. * @param string $destination
  280. * @param int $options
  281. * @return bool
  282. */
  283. public function copyDirectory($directory, $destination, $options = null)
  284. {
  285. if ( ! $this->isDirectory($directory)) return false;
  286. $options = $options ?: FilesystemIterator::SKIP_DOTS;
  287. // If the destination directory does not actually exist, we will go ahead and
  288. // create it recursively, which just gets the destination prepared to copy
  289. // the files over. Once we make the directory we'll proceed the copying.
  290. if ( ! $this->isDirectory($destination))
  291. {
  292. $this->makeDirectory($destination, 0777, true);
  293. }
  294. $items = new FilesystemIterator($directory, $options);
  295. foreach ($items as $item)
  296. {
  297. // As we spin through items, we will check to see if the current file is actually
  298. // a directory or a file. When it is actually a directory we will need to call
  299. // back into this function recursively to keep copying these nested folders.
  300. $target = $destination.'/'.$item->getBasename();
  301. if ($item->isDir())
  302. {
  303. $path = $item->getPathname();
  304. if ( ! $this->copyDirectory($path, $target, $options)) return false;
  305. }
  306. // If the current items is just a regular file, we will just copy this to the new
  307. // location and keep looping. If for some reason the copy fails we'll bail out
  308. // and return false, so the developer is aware that the copy process failed.
  309. else
  310. {
  311. if ( ! $this->copy($item->getPathname(), $target)) return false;
  312. }
  313. }
  314. return true;
  315. }
  316. /**
  317. * Recursively delete a directory.
  318. *
  319. * The directory itself may be optionally preserved.
  320. *
  321. * @param string $directory
  322. * @param bool $preserve
  323. * @return bool
  324. */
  325. public function deleteDirectory($directory, $preserve = false)
  326. {
  327. if ( ! $this->isDirectory($directory)) return false;
  328. $items = new FilesystemIterator($directory);
  329. foreach ($items as $item)
  330. {
  331. // If the item is a directory, we can just recurse into the function and
  332. // delete that sub-directory otherwise we'll just delete the file and
  333. // keep iterating through each file until the directory is cleaned.
  334. if ($item->isDir())
  335. {
  336. $this->deleteDirectory($item->getPathname());
  337. }
  338. // If the item is just a file, we can go ahead and delete it since we're
  339. // just looping through and waxing all of the files in this directory
  340. // and calling directories recursively, so we delete the real path.
  341. else
  342. {
  343. $this->delete($item->getPathname());
  344. }
  345. }
  346. if ( ! $preserve) @rmdir($directory);
  347. return true;
  348. }
  349. /**
  350. * Empty the specified directory of all files and folders.
  351. *
  352. * @param string $directory
  353. * @return bool
  354. */
  355. public function cleanDirectory($directory)
  356. {
  357. return $this->deleteDirectory($directory, true);
  358. }
  359. }