PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

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