PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/georgeasd/Laravel-4-simple-blog-starter
PHP | 407 lines | 170 code | 49 blank | 188 comment | 14 complexity | 88b15ee83225e63eca665d8ded45df67 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, LGPL-3.0, LGPL-2.1
  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. * Prepend to a file.
  71. *
  72. * @param string $path
  73. * @param string $data
  74. * @return int
  75. */
  76. public function prepend($path, $data)
  77. {
  78. if ($this->exists($path))
  79. {
  80. return $this->put($path, $data.$this->get($path));
  81. }
  82. else
  83. {
  84. return $this->put($path, $data);
  85. }
  86. }
  87. /**
  88. * Append to a file.
  89. *
  90. * @param string $path
  91. * @param string $data
  92. * @return int
  93. */
  94. public function append($path, $data)
  95. {
  96. return file_put_contents($path, $data, FILE_APPEND);
  97. }
  98. /**
  99. * Delete the file at a given path.
  100. *
  101. * @param string $path
  102. * @return bool
  103. */
  104. public function delete($path)
  105. {
  106. return @unlink($path);
  107. }
  108. /**
  109. * Move a file to a new location.
  110. *
  111. * @param string $path
  112. * @param string $target
  113. * @return bool
  114. */
  115. public function move($path, $target)
  116. {
  117. return rename($path, $target);
  118. }
  119. /**
  120. * Copy a file to a new location.
  121. *
  122. * @param string $path
  123. * @param string $target
  124. * @return bool
  125. */
  126. public function copy($path, $target)
  127. {
  128. return copy($path, $target);
  129. }
  130. /**
  131. * Extract the file extension from a file path.
  132. *
  133. * @param string $path
  134. * @return string
  135. */
  136. public function extension($path)
  137. {
  138. return pathinfo($path, PATHINFO_EXTENSION);
  139. }
  140. /**
  141. * Get the file type of a given file.
  142. *
  143. * @param string $path
  144. * @return string
  145. */
  146. public function type($path)
  147. {
  148. return filetype($path);
  149. }
  150. /**
  151. * Get the file size of a given file.
  152. *
  153. * @param string $path
  154. * @return int
  155. */
  156. public function size($path)
  157. {
  158. return filesize($path);
  159. }
  160. /**
  161. * Get the file's last modification time.
  162. *
  163. * @param string $path
  164. * @return int
  165. */
  166. public function lastModified($path)
  167. {
  168. return filemtime($path);
  169. }
  170. /**
  171. * Determine if the given path is a directory.
  172. *
  173. * @param string $directory
  174. * @return bool
  175. */
  176. public function isDirectory($directory)
  177. {
  178. return is_dir($directory);
  179. }
  180. /**
  181. * Determine if the given path is writable.
  182. *
  183. * @param string $path
  184. * @return bool
  185. */
  186. public function isWritable($path)
  187. {
  188. return is_writable($path);
  189. }
  190. /**
  191. * Determine if the given path is a file.
  192. *
  193. * @param string $file
  194. * @return bool
  195. */
  196. public function isFile($file)
  197. {
  198. return is_file($file);
  199. }
  200. /**
  201. * Find path names matching a given pattern.
  202. *
  203. * @param string $pattern
  204. * @param int $flags
  205. * @return array
  206. */
  207. public function glob($pattern, $flags = 0)
  208. {
  209. return glob($pattern, $flags);
  210. }
  211. /**
  212. * Get an array of all files in a directory.
  213. *
  214. * @param string $directory
  215. * @return array
  216. */
  217. public function files($directory)
  218. {
  219. $glob = glob($directory.'/*');
  220. if ($glob === false) return array();
  221. // To get the appropriate files, we'll simply glob the directory and filter
  222. // out any "files" that are not truly files so we do not end up with any
  223. // directories in our list, but only true files within the directory.
  224. return array_filter($glob, function($file)
  225. {
  226. return filetype($file) == 'file';
  227. });
  228. }
  229. /**
  230. * Get all of the files from the given directory (recursive).
  231. *
  232. * @param string $directory
  233. * @return array
  234. */
  235. public function allFiles($directory)
  236. {
  237. return iterator_to_array(Finder::create()->files()->in($directory), false);
  238. }
  239. /**
  240. * Get all of the directories within a given directory.
  241. *
  242. * @param string $directory
  243. * @return array
  244. */
  245. public function directories($directory)
  246. {
  247. $directories = array();
  248. foreach (Finder::create()->in($directory)->directories()->depth(0) as $dir)
  249. {
  250. $directories[] = $dir->getPathname();
  251. }
  252. return $directories;
  253. }
  254. /**
  255. * Create a directory.
  256. *
  257. * @param string $path
  258. * @param int $mode
  259. * @param bool $recursive
  260. * @param bool $force
  261. * @return bool
  262. */
  263. public function makeDirectory($path, $mode = 0777, $recursive = false, $force = false)
  264. {
  265. if ($force)
  266. {
  267. return @mkdir($path, $mode, $recursive);
  268. }
  269. else
  270. {
  271. return mkdir($path, $mode, $recursive);
  272. }
  273. }
  274. /**
  275. * Copy a directory from one location to another.
  276. *
  277. * @param string $directory
  278. * @param string $destination
  279. * @param int $options
  280. * @return bool
  281. */
  282. public function copyDirectory($directory, $destination, $options = null)
  283. {
  284. if ( ! $this->isDirectory($directory)) return false;
  285. $options = $options ?: FilesystemIterator::SKIP_DOTS;
  286. // If the destination directory does not actually exist, we will go ahead and
  287. // create it recursively, which just gets the destination prepared to copy
  288. // the files over. Once we make the directory we'll proceed the copying.
  289. if ( ! $this->isDirectory($destination))
  290. {
  291. $this->makeDirectory($destination, 0777, true);
  292. }
  293. $items = new FilesystemIterator($directory, $options);
  294. foreach ($items as $item)
  295. {
  296. // As we spin through items, we will check to see if the current file is actually
  297. // a directory or a file. When it is actually a directory we will need to call
  298. // back into this function recursively to keep copying these nested folders.
  299. $target = $destination.'/'.$item->getBasename();
  300. if ($item->isDir())
  301. {
  302. $path = $item->getPathname();
  303. if ( ! $this->copyDirectory($path, $target, $options)) return false;
  304. }
  305. // If the current items is just a regular file, we will just copy this to the new
  306. // location and keep looping. If for some reason the copy fails we'll bail out
  307. // and return false, so the developer is aware that the copy process failed.
  308. else
  309. {
  310. if ( ! $this->copy($item->getPathname(), $target)) return false;
  311. }
  312. }
  313. return true;
  314. }
  315. /**
  316. * Recursively delete a directory.
  317. *
  318. * The directory itself may be optionally preserved.
  319. *
  320. * @param string $directory
  321. * @param bool $preserve
  322. * @return bool
  323. */
  324. public function deleteDirectory($directory, $preserve = false)
  325. {
  326. if ( ! $this->isDirectory($directory)) return false;
  327. $items = new FilesystemIterator($directory);
  328. foreach ($items as $item)
  329. {
  330. // If the item is a directory, we can just recurse into the function and
  331. // delete that sub-director, otherwise we'll just delete the file and
  332. // keep iterating through each file until the directory is cleaned.
  333. if ($item->isDir())
  334. {
  335. $this->deleteDirectory($item->getPathname());
  336. }
  337. // If the item is just a file, we can go ahead and delete it since we're
  338. // just looping through and waxing all of the files in this directory
  339. // and calling directories recursively, so we delete the real path.
  340. else
  341. {
  342. $this->delete($item->getPathname());
  343. }
  344. }
  345. if ( ! $preserve) @rmdir($directory);
  346. return true;
  347. }
  348. /**
  349. * Empty the specified directory of all files and folders.
  350. *
  351. * @param string $directory
  352. * @return bool
  353. */
  354. public function cleanDirectory($directory)
  355. {
  356. return $this->deleteDirectory($directory, true);
  357. }
  358. }