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

https://bitbucket.org/larryg/powerhut · PHP · 378 lines · 151 code · 47 blank · 180 comment · 12 complexity · 3343390757351a34a702058e92238de2 MD5 · raw file

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