PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

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

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