PageRenderTime 45ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/myCore/lib/laravel/illuminate/filesystem/Illuminate/Filesystem/Filesystem.php

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