PageRenderTime 32ms CodeModel.GetById 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Illuminate/Filesystem/Filesystem.php

https://github.com/Fale/framework
PHP | 326 lines | 129 code | 41 blank | 156 comment | 12 complexity | 92c39b45c26cdf49b2329b55230aebd8 MD5 | raw file
  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->exists($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->exists($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, LOCK_EX);
  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, LOCK_EX | 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. return 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. * Find path names matching a given pattern.
  163. *
  164. * @param string $pattern
  165. * @param int $flags
  166. * @return array
  167. */
  168. public function glob($pattern, $flags = 0)
  169. {
  170. return glob($pattern, $flags);
  171. }
  172. /**
  173. * Get an array of all files in a directory.
  174. *
  175. * @param string $directory
  176. * @return array
  177. */
  178. public function files($directory)
  179. {
  180. $glob = glob($directory.'/*');
  181. if ($glob === false) return array();
  182. // To get the appropriate files, we'll simply glob the directory and filter
  183. // out any "files" that are not truly files so we do not end up with any
  184. // directories in our list, but only true files within the directory.
  185. return array_filter($glob, function($file)
  186. {
  187. return filetype($file) == 'file';
  188. });
  189. }
  190. /**
  191. * Create a directory.
  192. *
  193. * @param string $path
  194. * @param int $mode
  195. * @param bool $recursive
  196. * @return bool
  197. */
  198. public function makeDirectory($path, $mode = 0777, $recursive = false)
  199. {
  200. return mkdir($path, $mode, $recursive);
  201. }
  202. /**
  203. * Copy a directory from one location to another.
  204. *
  205. * @param string $directory
  206. * @param string $destination
  207. * @param int $options
  208. * @return void
  209. */
  210. public function copyDirectory($directory, $destination, $options = null)
  211. {
  212. if ( ! $this->isDirectory($directory)) return false;
  213. $options = $options ?: FilesystemIterator::SKIP_DOTS;
  214. // If the destination directory does not actually exist, we will go ahead and
  215. // create it recursively, which just gets the destination prepared to copy
  216. // the files over. Once we make the directory we'll proceed the copying.
  217. if ( ! $this->isDirectory($destination))
  218. {
  219. $this->makeDirectory($destination, 0777, true);
  220. }
  221. $items = new FilesystemIterator($directory, $options);
  222. foreach ($items as $item)
  223. {
  224. // As we spin through items, we will check to see if the current file is actually
  225. // a directory or a file. When it is actually a directory we will need to call
  226. // back into this function recursively to keep copying these nested folders.
  227. $target = $destination.'/'.$item->getBasename();
  228. if ($item->isDir())
  229. {
  230. $path = $item->getRealPath();
  231. if ( ! $this->copyDirectory($path, $target, $options)) return false;
  232. }
  233. // If the current items is just a regular file, we will just copy this to the new
  234. // location and keep looping. If for some reason the copy fails we'll bail out
  235. // and return false, so the developer is aware that the copy process failed.
  236. else
  237. {
  238. if ( ! $this->copy($item->getRealPath(), $target)) return false;
  239. }
  240. }
  241. return true;
  242. }
  243. /**
  244. * Recursively delete a directory.
  245. *
  246. * The directory itself may be optionally preserved.
  247. *
  248. * @param string $directory
  249. * @param bool $preserve
  250. * @return void
  251. */
  252. public function deleteDirectory($directory, $preserve = false)
  253. {
  254. if ( ! $this->isDirectory($directory)) return;
  255. $items = new FilesystemIterator($directory);
  256. foreach ($items as $item)
  257. {
  258. // If the item is a directory, we can just recurse into the function and
  259. // delete that sub-director, otherwise we'll just delete the file and
  260. // keep iterating through each file until the directory is cleaned.
  261. if ($item->isDir())
  262. {
  263. $this->deleteDirectory($item->getRealPath());
  264. }
  265. // If the item is just a file, we can go ahead and delete it since we're
  266. // just looping through and waxing all of the files in this directory
  267. // and calling directories recursively, so we delete the real path.
  268. else
  269. {
  270. $this->delete($item->getRealPath());
  271. }
  272. }
  273. if ( ! $preserve) @rmdir($directory);
  274. }
  275. /**
  276. * Empty the specified directory of all files and folders.
  277. *
  278. * @param string $directory
  279. * @return void
  280. */
  281. public function cleanDirectory($directory)
  282. {
  283. return $this->deleteDirectory($directory, true);
  284. }
  285. }