1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,609 +0,0 @@ |
1 |
-<?php |
|
2 |
- |
|
3 |
-namespace Illuminate\Filesystem; |
|
4 |
- |
|
5 |
-use ErrorException; |
|
6 |
-use FilesystemIterator; |
|
7 |
-use Symfony\Component\Finder\Finder; |
|
8 |
-use Illuminate\Support\Traits\Macroable; |
|
9 |
-use Illuminate\Contracts\Filesystem\FileNotFoundException; |
|
10 |
- |
|
11 |
-class Filesystem |
|
12 |
-{ |
|
13 |
- use Macroable; |
|
14 |
- |
|
15 |
- /** |
|
16 |
- * Determine if a file or directory exists. |
|
17 |
- * |
|
18 |
- * @param string $path |
|
19 |
- * @return bool |
|
20 |
- */ |
|
21 |
- public function exists($path) |
|
22 |
- { |
|
23 |
- return file_exists($path); |
|
24 |
- } |
|
25 |
- |
|
26 |
- /** |
|
27 |
- * Get the contents of a file. |
|
28 |
- * |
|
29 |
- * @param string $path |
|
30 |
- * @param bool $lock |
|
31 |
- * @return string |
|
32 |
- * |
|
33 |
- * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
|
34 |
- */ |
|
35 |
- public function get($path, $lock = false) |
|
36 |
- { |
|
37 |
- if ($this->isFile($path)) { |
|
38 |
- return $lock ? $this->sharedGet($path) : file_get_contents($path); |
|
39 |
- } |
|
40 |
- |
|
41 |
- throw new FileNotFoundException("File does not exist at path {$path}"); |
|
42 |
- } |
|
43 |
- |
|
44 |
- /** |
|
45 |
- * Get contents of a file with shared access. |
|
46 |
- * |
|
47 |
- * @param string $path |
|
48 |
- * @return string |
|
49 |
- */ |
|
50 |
- public function sharedGet($path) |
|
51 |
- { |
|
52 |
- $contents = ''; |
|
53 |
- |
|
54 |
- $handle = fopen($path, 'rb'); |
|
55 |
- |
|
56 |
- if ($handle) { |
|
57 |
- try { |
|
58 |
- if (flock($handle, LOCK_SH)) { |
|
59 |
- clearstatcache(true, $path); |
|
60 |
- |
|
61 |
- $contents = fread($handle, $this->size($path) ?: 1); |
|
62 |
- |
|
63 |
- flock($handle, LOCK_UN); |
|
64 |
- } |
|
65 |
- } finally { |
|
66 |
- fclose($handle); |
|
67 |
- } |
|
68 |
- } |
|
69 |
- |
|
70 |
- return $contents; |
|
71 |
- } |
|
72 |
- |
|
73 |
- /** |
|
74 |
- * Get the returned value of a file. |
|
75 |
- * |
|
76 |
- * @param string $path |
|
77 |
- * @return mixed |
|
78 |
- * |
|
79 |
- * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
|
80 |
- */ |
|
81 |
- public function getRequire($path) |
|
82 |
- { |
|
83 |
- if ($this->isFile($path)) { |
|
84 |
- return require $path; |
|
85 |
- } |
|
86 |
- |
|
87 |
- throw new FileNotFoundException("File does not exist at path {$path}"); |
|
88 |
- } |
|
89 |
- |
|
90 |
- /** |
|
91 |
- * Require the given file once. |
|
92 |
- * |
|
93 |
- * @param string $file |
|
94 |
- * @return mixed |
|
95 |
- */ |
|
96 |
- public function requireOnce($file) |
|
97 |
- { |
|
98 |
- require_once $file; |
|
99 |
- } |
|
100 |
- |
|
101 |
- /** |
|
102 |
- * Get the MD5 hash of the file at the given path. |
|
103 |
- * |
|
104 |
- * @param string $path |
|
105 |
- * @return string |
|
106 |
- */ |
|
107 |
- public function hash($path) |
|
108 |
- { |
|
109 |
- return md5_file($path); |
|
110 |
- } |
|
111 |
- |
|
112 |
- /** |
|
113 |
- * Write the contents of a file. |
|
114 |
- * |
|
115 |
- * @param string $path |
|
116 |
- * @param string $contents |
|
117 |
- * @param bool $lock |
|
118 |
- * @return int|bool |
|
119 |
- */ |
|
120 |
- public function put($path, $contents, $lock = false) |
|
121 |
- { |
|
122 |
- return file_put_contents($path, $contents, $lock ? LOCK_EX : 0); |
|
123 |
- } |
|
124 |
- |
|
125 |
- /** |
|
126 |
- * Write the contents of a file, replacing it atomically if it already exists. |
|
127 |
- * |
|
128 |
- * @param string $path |
|
129 |
- * @param string $content |
|
130 |
- * @return void |
|
131 |
- */ |
|
132 |
- public function replace($path, $content) |
|
133 |
- { |
|
134 |
- // If the path already exists and is a symlink, get the real path... |
|
135 |
- clearstatcache(true, $path); |
|
136 |
- |
|
137 |
- $path = realpath($path) ?: $path; |
|
138 |
- |
|
139 |
- $tempPath = tempnam(dirname($path), basename($path)); |
|
140 |
- |
|
141 |
- // Fix permissions of tempPath because `tempnam()` creates it with permissions set to 0600... |
|
142 |
- chmod($tempPath, 0777 - umask()); |
|
143 |
- |
|
144 |
- file_put_contents($tempPath, $content); |
|
145 |
- |
|
146 |
- rename($tempPath, $path); |
|
147 |
- } |
|
148 |
- |
|
149 |
- /** |
|
150 |
- * Prepend to a file. |
|
151 |
- * |
|
152 |
- * @param string $path |
|
153 |
- * @param string $data |
|
154 |
- * @return int |
|
155 |
- */ |
|
156 |
- public function prepend($path, $data) |
|
157 |
- { |
|
158 |
- if ($this->exists($path)) { |
|
159 |
- return $this->put($path, $data.$this->get($path)); |
|
160 |
- } |
|
161 |
- |
|
162 |
- return $this->put($path, $data); |
|
163 |
- } |
|
164 |
- |
|
165 |
- /** |
|
166 |
- * Append to a file. |
|
167 |
- * |
|
168 |
- * @param string $path |
|
169 |
- * @param string $data |
|
170 |
- * @return int |
|
171 |
- */ |
|
172 |
- public function append($path, $data) |
|
173 |
- { |
|
174 |
- return file_put_contents($path, $data, FILE_APPEND); |
|
175 |
- } |
|
176 |
- |
|
177 |
- /** |
|
178 |
- * Get or set UNIX mode of a file or directory. |
|
179 |
- * |
|
180 |
- * @param string $path |
|
181 |
- * @param int|null $mode |
|
182 |
- * @return mixed |
|
183 |
- */ |
|
184 |
- public function chmod($path, $mode = null) |
|
185 |
- { |
|
186 |
- if ($mode) { |
|
187 |
- return chmod($path, $mode); |
|
188 |
- } |
|
189 |
- |
|
190 |
- return substr(sprintf('%o', fileperms($path)), -4); |
|
191 |
- } |
|
192 |
- |
|
193 |
- /** |
|
194 |
- * Delete the file at a given path. |
|
195 |
- * |
|
196 |
- * @param string|array $paths |
|
197 |
- * @return bool |
|
198 |
- */ |
|
199 |
- public function delete($paths) |
|
200 |
- { |
|
201 |
- $paths = is_array($paths) ? $paths : func_get_args(); |
|
202 |
- |
|
203 |
- $success = true; |
|
204 |
- |
|
205 |
- foreach ($paths as $path) { |
|
206 |
- try { |
|
207 |
- if (! @unlink($path)) { |
|
208 |
- $success = false; |
|
209 |
- } |
|
210 |
- } catch (ErrorException $e) { |
|
211 |
- $success = false; |
|
212 |
- } |
|
213 |
- } |
|
214 |
- |
|
215 |
- return $success; |
|
216 |
- } |
|
217 |
- |
|
218 |
- /** |
|
219 |
- * Move a file to a new location. |
|
220 |
- * |
|
221 |
- * @param string $path |
|
222 |
- * @param string $target |
|
223 |
- * @return bool |
|
224 |
- */ |
|
225 |
- public function move($path, $target) |
|
226 |
- { |
|
227 |
- return rename($path, $target); |
|
228 |
- } |
|
229 |
- |
|
230 |
- /** |
|
231 |
- * Copy a file to a new location. |
|
232 |
- * |
|
233 |
- * @param string $path |
|
234 |
- * @param string $target |
|
235 |
- * @return bool |
|
236 |
- */ |
|
237 |
- public function copy($path, $target) |
|
238 |
- { |
|
239 |
- return copy($path, $target); |
|
240 |
- } |
|
241 |
- |
|
242 |
- /** |
|
243 |
- * Create a hard link to the target file or directory. |
|
244 |
- * |
|
245 |
- * @param string $target |
|
246 |
- * @param string $link |
|
247 |
- * @return void |
|
248 |
- */ |
|
249 |
- public function link($target, $link) |
|
250 |
- { |
|
251 |
- if (! windows_os()) { |
|
252 |
- return symlink($target, $link); |
|
253 |
- } |
|
254 |
- |
|
255 |
- $mode = $this->isDirectory($target) ? 'J' : 'H'; |
|
256 |
- |
|
257 |
- exec("mklink /{$mode} ".escapeshellarg($link).' '.escapeshellarg($target)); |
|
258 |
- } |
|
259 |
- |
|
260 |
- /** |
|
261 |
- * Extract the file name from a file path. |
|
262 |
- * |
|
263 |
- * @param string $path |
|
264 |
- * @return string |
|
265 |
- */ |
|
266 |
- public function name($path) |
|
267 |
- { |
|
268 |
- return pathinfo($path, PATHINFO_FILENAME); |
|
269 |
- } |
|
270 |
- |
|
271 |
- /** |
|
272 |
- * Extract the trailing name component from a file path. |
|
273 |
- * |
|
274 |
- * @param string $path |
|
275 |
- * @return string |
|
276 |
- */ |
|
277 |
- public function basename($path) |
|
278 |
- { |
|
279 |
- return pathinfo($path, PATHINFO_BASENAME); |
|
280 |
- } |
|
281 |
- |
|
282 |
- /** |
|
283 |
- * Extract the parent directory from a file path. |
|
284 |
- * |
|
285 |
- * @param string $path |
|
286 |
- * @return string |
|
287 |
- */ |
|
288 |
- public function dirname($path) |
|
289 |
- { |
|
290 |
- return pathinfo($path, PATHINFO_DIRNAME); |
|
291 |
- } |
|
292 |
- |
|
293 |
- /** |
|
294 |
- * Extract the file extension from a file path. |
|
295 |
- * |
|
296 |
- * @param string $path |
|
297 |
- * @return string |
|
298 |
- */ |
|
299 |
- public function extension($path) |
|
300 |
- { |
|
301 |
- return pathinfo($path, PATHINFO_EXTENSION); |
|
302 |
- } |
|
303 |
- |
|
304 |
- /** |
|
305 |
- * Get the file type of a given file. |
|
306 |
- * |
|
307 |
- * @param string $path |
|
308 |
- * @return string |
|
309 |
- */ |
|
310 |
- public function type($path) |
|
311 |
- { |
|
312 |
- return filetype($path); |
|
313 |
- } |
|
314 |
- |
|
315 |
- /** |
|
316 |
- * Get the mime-type of a given file. |
|
317 |
- * |
|
318 |
- * @param string $path |
|
319 |
- * @return string|false |
|
320 |
- */ |
|
321 |
- public function mimeType($path) |
|
322 |
- { |
|
323 |
- return finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path); |
|
324 |
- } |
|
325 |
- |
|
326 |
- /** |
|
327 |
- * Get the file size of a given file. |
|
328 |
- * |
|
329 |
- * @param string $path |
|
330 |
- * @return int |
|
331 |
- */ |
|
332 |
- public function size($path) |
|
333 |
- { |
|
334 |
- return filesize($path); |
|
335 |
- } |
|
336 |
- |
|
337 |
- /** |
|
338 |
- * Get the file's last modification time. |
|
339 |
- * |
|
340 |
- * @param string $path |
|
341 |
- * @return int |
|
342 |
- */ |
|
343 |
- public function lastModified($path) |
|
344 |
- { |
|
345 |
- return filemtime($path); |
|
346 |
- } |
|
347 |
- |
|
348 |
- /** |
|
349 |
- * Determine if the given path is a directory. |
|
350 |
- * |
|
351 |
- * @param string $directory |
|
352 |
- * @return bool |
|
353 |
- */ |
|
354 |
- public function isDirectory($directory) |
|
355 |
- { |
|
356 |
- return is_dir($directory); |
|
357 |
- } |
|
358 |
- |
|
359 |
- /** |
|
360 |
- * Determine if the given path is readable. |
|
361 |
- * |
|
362 |
- * @param string $path |
|
363 |
- * @return bool |
|
364 |
- */ |
|
365 |
- public function isReadable($path) |
|
366 |
- { |
|
367 |
- return is_readable($path); |
|
368 |
- } |
|
369 |
- |
|
370 |
- /** |
|
371 |
- * Determine if the given path is writable. |
|
372 |
- * |
|
373 |
- * @param string $path |
|
374 |
- * @return bool |
|
375 |
- */ |
|
376 |
- public function isWritable($path) |
|
377 |
- { |
|
378 |
- return is_writable($path); |
|
379 |
- } |
|
380 |
- |
|
381 |
- /** |
|
382 |
- * Determine if the given path is a file. |
|
383 |
- * |
|
384 |
- * @param string $file |
|
385 |
- * @return bool |
|
386 |
- */ |
|
387 |
- public function isFile($file) |
|
388 |
- { |
|
389 |
- return is_file($file); |
|
390 |
- } |
|
391 |
- |
|
392 |
- /** |
|
393 |
- * Find path names matching a given pattern. |
|
394 |
- * |
|
395 |
- * @param string $pattern |
|
396 |
- * @param int $flags |
|
397 |
- * @return array |
|
398 |
- */ |
|
399 |
- public function glob($pattern, $flags = 0) |
|
400 |
- { |
|
401 |
- return glob($pattern, $flags); |
|
402 |
- } |
|
403 |
- |
|
404 |
- /** |
|
405 |
- * Get an array of all files in a directory. |
|
406 |
- * |
|
407 |
- * @param string $directory |
|
408 |
- * @param bool $hidden |
|
409 |
- * @return \Symfony\Component\Finder\SplFileInfo[] |
|
410 |
- */ |
|
411 |
- public function files($directory, $hidden = false) |
|
412 |
- { |
|
413 |
- return iterator_to_array( |
|
414 |
- Finder::create()->files()->ignoreDotFiles(! $hidden)->in($directory)->depth(0)->sortByName(), |
|
415 |
- false |
|
416 |
- ); |
|
417 |
- } |
|
418 |
- |
|
419 |
- /** |
|
420 |
- * Get all of the files from the given directory (recursive). |
|
421 |
- * |
|
422 |
- * @param string $directory |
|
423 |
- * @param bool $hidden |
|
424 |
- * @return \Symfony\Component\Finder\SplFileInfo[] |
|
425 |
- */ |
|
426 |
- public function allFiles($directory, $hidden = false) |
|
427 |
- { |
|
428 |
- return iterator_to_array( |
|
429 |
- Finder::create()->files()->ignoreDotFiles(! $hidden)->in($directory)->sortByName(), |
|
430 |
- false |
|
431 |
- ); |
|
432 |
- } |
|
433 |
- |
|
434 |
- /** |
|
435 |
- * Get all of the directories within a given directory. |
|
436 |
- * |
|
437 |
- * @param string $directory |
|
438 |
- * @return array |
|
439 |
- */ |
|
440 |
- public function directories($directory) |
|
441 |
- { |
|
442 |
- $directories = []; |
|
443 |
- |
|
444 |
- foreach (Finder::create()->in($directory)->directories()->depth(0)->sortByName() as $dir) { |
|
445 |
- $directories[] = $dir->getPathname(); |
|
446 |
- } |
|
447 |
- |
|
448 |
- return $directories; |
|
449 |
- } |
|
450 |
- |
|
451 |
- /** |
|
452 |
- * Create a directory. |
|
453 |
- * |
|
454 |
- * @param string $path |
|
455 |
- * @param int $mode |
|
456 |
- * @param bool $recursive |
|
457 |
- * @param bool $force |
|
458 |
- * @return bool |
|
459 |
- */ |
|
460 |
- public function makeDirectory($path, $mode = 0755, $recursive = false, $force = false) |
|
461 |
- { |
|
462 |
- if ($force) { |
|
463 |
- return @mkdir($path, $mode, $recursive); |
|
464 |
- } |
|
465 |
- |
|
466 |
- return mkdir($path, $mode, $recursive); |
|
467 |
- } |
|
468 |
- |
|
469 |
- /** |
|
470 |
- * Move a directory. |
|
471 |
- * |
|
472 |
- * @param string $from |
|
473 |
- * @param string $to |
|
474 |
- * @param bool $overwrite |
|
475 |
- * @return bool |
|
476 |
- */ |
|
477 |
- public function moveDirectory($from, $to, $overwrite = false) |
|
478 |
- { |
|
479 |
- if ($overwrite && $this->isDirectory($to) && ! $this->deleteDirectory($to)) { |
|
480 |
- return false; |
|
481 |
- } |
|
482 |
- |
|
483 |
- return @rename($from, $to) === true; |
|
484 |
- } |
|
485 |
- |
|
486 |
- /** |
|
487 |
- * Copy a directory from one location to another. |
|
488 |
- * |
|
489 |
- * @param string $directory |
|
490 |
- * @param string $destination |
|
491 |
- * @param int|null $options |
|
492 |
- * @return bool |
|
493 |
- */ |
|
494 |
- public function copyDirectory($directory, $destination, $options = null) |
|
495 |
- { |
|
496 |
- if (! $this->isDirectory($directory)) { |
|
497 |
- return false; |
|
498 |
- } |
|
499 |
- |
|
500 |
- $options = $options ?: FilesystemIterator::SKIP_DOTS; |
|
501 |
- |
|
502 |
- // If the destination directory does not actually exist, we will go ahead and |
|
503 |
- // create it recursively, which just gets the destination prepared to copy |
|
504 |
- // the files over. Once we make the directory we'll proceed the copying. |
|
505 |
- if (! $this->isDirectory($destination)) { |
|
506 |
- $this->makeDirectory($destination, 0777, true); |
|
507 |
- } |
|
508 |
- |
|
509 |
- $items = new FilesystemIterator($directory, $options); |
|
510 |
- |
|
511 |
- foreach ($items as $item) { |
|
512 |
- // As we spin through items, we will check to see if the current file is actually |
|
513 |
- // a directory or a file. When it is actually a directory we will need to call |
|
514 |
- // back into this function recursively to keep copying these nested folders. |
|
515 |
- $target = $destination.'/'.$item->getBasename(); |
|
516 |
- |
|
517 |
- if ($item->isDir()) { |
|
518 |
- $path = $item->getPathname(); |
|
519 |
- |
|
520 |
- if (! $this->copyDirectory($path, $target, $options)) { |
|
521 |
- return false; |
|
522 |
- } |
|
523 |
- } |
|
524 |
- |
|
525 |
- // If the current items is just a regular file, we will just copy this to the new |
|
526 |
- // location and keep looping. If for some reason the copy fails we'll bail out |
|
527 |
- // and return false, so the developer is aware that the copy process failed. |
|
528 |
- else { |
|
529 |
- if (! $this->copy($item->getPathname(), $target)) { |
|
530 |
- return false; |
|
531 |
- } |
|
532 |
- } |
|
533 |
- } |
|
534 |
- |
|
535 |
- return true; |
|
536 |
- } |
|
537 |
- |
|
538 |
- /** |
|
539 |
- * Recursively delete a directory. |
|
540 |
- * |
|
541 |
- * The directory itself may be optionally preserved. |
|
542 |
- * |
|
543 |
- * @param string $directory |
|
544 |
- * @param bool $preserve |
|
545 |
- * @return bool |
|
546 |
- */ |
|
547 |
- public function deleteDirectory($directory, $preserve = false) |
|
548 |
- { |
|
549 |
- if (! $this->isDirectory($directory)) { |
|
550 |
- return false; |
|
551 |
- } |
|
552 |
- |
|
553 |
- $items = new FilesystemIterator($directory); |
|
554 |
- |
|
555 |
- foreach ($items as $item) { |
|
556 |
- // If the item is a directory, we can just recurse into the function and |
|
557 |
- // delete that sub-directory otherwise we'll just delete the file and |
|
558 |
- // keep iterating through each file until the directory is cleaned. |
|
559 |
- if ($item->isDir() && ! $item->isLink()) { |
|
560 |
- $this->deleteDirectory($item->getPathname()); |
|
561 |
- } |
|
562 |
- |
|
563 |
- // If the item is just a file, we can go ahead and delete it since we're |
|
564 |
- // just looping through and waxing all of the files in this directory |
|
565 |
- // and calling directories recursively, so we delete the real path. |
|
566 |
- else { |
|
567 |
- $this->delete($item->getPathname()); |
|
568 |
- } |
|
569 |
- } |
|
570 |
- |
|
571 |
- if (! $preserve) { |
|
572 |
- @rmdir($directory); |
|
573 |
- } |
|
574 |
- |
|
575 |
- return true; |
|
576 |
- } |
|
577 |
- |
|
578 |
- /** |
|
579 |
- * Remove all of the directories within a given directory. |
|
580 |
- * |
|
581 |
- * @param string $directory |
|
582 |
- * @return bool |
|
583 |
- */ |
|
584 |
- public function deleteDirectories($directory) |
|
585 |
- { |
|
586 |
- $allDirectories = $this->directories($directory); |
|
587 |
- |
|
588 |
- if (! empty($allDirectories)) { |
|
589 |
- foreach ($allDirectories as $directoryName) { |
|
590 |
- $this->deleteDirectory($directoryName); |
|
591 |
- } |
|
592 |
- |
|
593 |
- return true; |
|
594 |
- } |
|
595 |
- |
|
596 |
- return false; |
|
597 |
- } |
|
598 |
- |
|
599 |
- /** |
|
600 |
- * Empty the specified directory of all files and folders. |
|
601 |
- * |
|
602 |
- * @param string $directory |
|
603 |
- * @return bool |
|
604 |
- */ |
|
605 |
- public function cleanDirectory($directory) |
|
606 |
- { |
|
607 |
- return $this->deleteDirectory($directory, true); |
|
608 |
- } |
|
609 |
-} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,609 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+namespace Illuminate\Filesystem; |
|
4 |
+ |
|
5 |
+use ErrorException; |
|
6 |
+use FilesystemIterator; |
|
7 |
+use Symfony\Component\Finder\Finder; |
|
8 |
+use Illuminate\Support\Traits\Macroable; |
|
9 |
+use Illuminate\Contracts\Filesystem\FileNotFoundException; |
|
10 |
+ |
|
11 |
+class Filesystem |
|
12 |
+{ |
|
13 |
+ use Macroable; |
|
14 |
+ |
|
15 |
+ /** |
|
16 |
+ * Determine if a file or directory exists. |
|
17 |
+ * |
|
18 |
+ * @param string $path |
|
19 |
+ * @return bool |
|
20 |
+ */ |
|
21 |
+ public function exists($path) |
|
22 |
+ { |
|
23 |
+ return file_exists($path); |
|
24 |
+ } |
|
25 |
+ |
|
26 |
+ /** |
|
27 |
+ * Get the contents of a file. |
|
28 |
+ * |
|
29 |
+ * @param string $path |
|
30 |
+ * @param bool $lock |
|
31 |
+ * @return string |
|
32 |
+ * |
|
33 |
+ * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
|
34 |
+ */ |
|
35 |
+ public function get($path, $lock = false) |
|
36 |
+ { |
|
37 |
+ if ($this->isFile($path)) { |
|
38 |
+ return $lock ? $this->sharedGet($path) : file_get_contents($path); |
|
39 |
+ } |
|
40 |
+ |
|
41 |
+ throw new FileNotFoundException("File does not exist at path {$path}"); |
|
42 |
+ } |
|
43 |
+ |
|
44 |
+ /** |
|
45 |
+ * Get contents of a file with shared access. |
|
46 |
+ * |
|
47 |
+ * @param string $path |
|
48 |
+ * @return string |
|
49 |
+ */ |
|
50 |
+ public function sharedGet($path) |
|
51 |
+ { |
|
52 |
+ $contents = ''; |
|
53 |
+ |
|
54 |
+ $handle = fopen($path, 'rb'); |
|
55 |
+ |
|
56 |
+ if ($handle) { |
|
57 |
+ try { |
|
58 |
+ if (flock($handle, LOCK_SH)) { |
|
59 |
+ clearstatcache(true, $path); |
|
60 |
+ |
|
61 |
+ $contents = fread($handle, $this->size($path) ?: 1); |
|
62 |
+ |
|
63 |
+ flock($handle, LOCK_UN); |
|
64 |
+ } |
|
65 |
+ } finally { |
|
66 |
+ fclose($handle); |
|
67 |
+ } |
|
68 |
+ } |
|
69 |
+ |
|
70 |
+ return $contents; |
|
71 |
+ } |
|
72 |
+ |
|
73 |
+ /** |
|
74 |
+ * Get the returned value of a file. |
|
75 |
+ * |
|
76 |
+ * @param string $path |
|
77 |
+ * @return mixed |
|
78 |
+ * |
|
79 |
+ * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
|
80 |
+ */ |
|
81 |
+ public function getRequire($path) |
|
82 |
+ { |
|
83 |
+ if ($this->isFile($path)) { |
|
84 |
+ return require $path; |
|
85 |
+ } |
|
86 |
+ |
|
87 |
+ throw new FileNotFoundException("File does not exist at path {$path}"); |
|
88 |
+ } |
|
89 |
+ |
|
90 |
+ /** |
|
91 |
+ * Require the given file once. |
|
92 |
+ * |
|
93 |
+ * @param string $file |
|
94 |
+ * @return mixed |
|
95 |
+ */ |
|
96 |
+ public function requireOnce($file) |
|
97 |
+ { |
|
98 |
+ require_once $file; |
|
99 |
+ } |
|
100 |
+ |
|
101 |
+ /** |
|
102 |
+ * Get the MD5 hash of the file at the given path. |
|
103 |
+ * |
|
104 |
+ * @param string $path |
|
105 |
+ * @return string |
|
106 |
+ */ |
|
107 |
+ public function hash($path) |
|
108 |
+ { |
|
109 |
+ return md5_file($path); |
|
110 |
+ } |
|
111 |
+ |
|
112 |
+ /** |
|
113 |
+ * Write the contents of a file. |
|
114 |
+ * |
|
115 |
+ * @param string $path |
|
116 |
+ * @param string $contents |
|
117 |
+ * @param bool $lock |
|
118 |
+ * @return int|bool |
|
119 |
+ */ |
|
120 |
+ public function put($path, $contents, $lock = false) |
|
121 |
+ { |
|
122 |
+ return file_put_contents($path, $contents, $lock ? LOCK_EX : 0); |
|
123 |
+ } |
|
124 |
+ |
|
125 |
+ /** |
|
126 |
+ * Write the contents of a file, replacing it atomically if it already exists. |
|
127 |
+ * |
|
128 |
+ * @param string $path |
|
129 |
+ * @param string $content |
|
130 |
+ * @return void |
|
131 |
+ */ |
|
132 |
+ public function replace($path, $content) |
|
133 |
+ { |
|
134 |
+ // If the path already exists and is a symlink, get the real path... |
|
135 |
+ clearstatcache(true, $path); |
|
136 |
+ |
|
137 |
+ $path = realpath($path) ?: $path; |
|
138 |
+ |
|
139 |
+ $tempPath = tempnam(dirname($path), basename($path)); |
|
140 |
+ |
|
141 |
+ // Fix permissions of tempPath because `tempnam()` creates it with permissions set to 0600... |
|
142 |
+ chmod($tempPath, 0777 - umask()); |
|
143 |
+ |
|
144 |
+ file_put_contents($tempPath, $content); |
|
145 |
+ |
|
146 |
+ rename($tempPath, $path); |
|
147 |
+ } |
|
148 |
+ |
|
149 |
+ /** |
|
150 |
+ * Prepend to a file. |
|
151 |
+ * |
|
152 |
+ * @param string $path |
|
153 |
+ * @param string $data |
|
154 |
+ * @return int |
|
155 |
+ */ |
|
156 |
+ public function prepend($path, $data) |
|
157 |
+ { |
|
158 |
+ if ($this->exists($path)) { |
|
159 |
+ return $this->put($path, $data.$this->get($path)); |
|
160 |
+ } |
|
161 |
+ |
|
162 |
+ return $this->put($path, $data); |
|
163 |
+ } |
|
164 |
+ |
|
165 |
+ /** |
|
166 |
+ * Append to a file. |
|
167 |
+ * |
|
168 |
+ * @param string $path |
|
169 |
+ * @param string $data |
|
170 |
+ * @return int |
|
171 |
+ */ |
|
172 |
+ public function append($path, $data) |
|
173 |
+ { |
|
174 |
+ return file_put_contents($path, $data, FILE_APPEND); |
|
175 |
+ } |
|
176 |
+ |
|
177 |
+ /** |
|
178 |
+ * Get or set UNIX mode of a file or directory. |
|
179 |
+ * |
|
180 |
+ * @param string $path |
|
181 |
+ * @param int|null $mode |
|
182 |
+ * @return mixed |
|
183 |
+ */ |
|
184 |
+ public function chmod($path, $mode = null) |
|
185 |
+ { |
|
186 |
+ if ($mode) { |
|
187 |
+ return chmod($path, $mode); |
|
188 |
+ } |
|
189 |
+ |
|
190 |
+ return substr(sprintf('%o', fileperms($path)), -4); |
|
191 |
+ } |
|
192 |
+ |
|
193 |
+ /** |
|
194 |
+ * Delete the file at a given path. |
|
195 |
+ * |
|
196 |
+ * @param string|array $paths |
|
197 |
+ * @return bool |
|
198 |
+ */ |
|
199 |
+ public function delete($paths) |
|
200 |
+ { |
|
201 |
+ $paths = is_array($paths) ? $paths : func_get_args(); |
|
202 |
+ |
|
203 |
+ $success = true; |
|
204 |
+ |
|
205 |
+ foreach ($paths as $path) { |
|
206 |
+ try { |
|
207 |
+ if (! @unlink($path)) { |
|
208 |
+ $success = false; |
|
209 |
+ } |
|
210 |
+ } catch (ErrorException $e) { |
|
211 |
+ $success = false; |
|
212 |
+ } |
|
213 |
+ } |
|
214 |
+ |
|
215 |
+ return $success; |
|
216 |
+ } |
|
217 |
+ |
|
218 |
+ /** |
|
219 |
+ * Move a file to a new location. |
|
220 |
+ * |
|
221 |
+ * @param string $path |
|
222 |
+ * @param string $target |
|
223 |
+ * @return bool |
|
224 |
+ */ |
|
225 |
+ public function move($path, $target) |
|
226 |
+ { |
|
227 |
+ return rename($path, $target); |
|
228 |
+ } |
|
229 |
+ |
|
230 |
+ /** |
|
231 |
+ * Copy a file to a new location. |
|
232 |
+ * |
|
233 |
+ * @param string $path |
|
234 |
+ * @param string $target |
|
235 |
+ * @return bool |
|
236 |
+ */ |
|
237 |
+ public function copy($path, $target) |
|
238 |
+ { |
|
239 |
+ return copy($path, $target); |
|
240 |
+ } |
|
241 |
+ |
|
242 |
+ /** |
|
243 |
+ * Create a hard link to the target file or directory. |
|
244 |
+ * |
|
245 |
+ * @param string $target |
|
246 |
+ * @param string $link |
|
247 |
+ * @return void |
|
248 |
+ */ |
|
249 |
+ public function link($target, $link) |
|
250 |
+ { |
|
251 |
+ if (! windows_os()) { |
|
252 |
+ return symlink($target, $link); |
|
253 |
+ } |
|
254 |
+ |
|
255 |
+ $mode = $this->isDirectory($target) ? 'J' : 'H'; |
|
256 |
+ |
|
257 |
+ exec("mklink /{$mode} ".escapeshellarg($link).' '.escapeshellarg($target)); |
|
258 |
+ } |
|
259 |
+ |
|
260 |
+ /** |
|
261 |
+ * Extract the file name from a file path. |
|
262 |
+ * |
|
263 |
+ * @param string $path |
|
264 |
+ * @return string |
|
265 |
+ */ |
|
266 |
+ public function name($path) |
|
267 |
+ { |
|
268 |
+ return pathinfo($path, PATHINFO_FILENAME); |
|
269 |
+ } |
|
270 |
+ |
|
271 |
+ /** |
|
272 |
+ * Extract the trailing name component from a file path. |
|
273 |
+ * |
|
274 |
+ * @param string $path |
|
275 |
+ * @return string |
|
276 |
+ */ |
|
277 |
+ public function basename($path) |
|
278 |
+ { |
|
279 |
+ return pathinfo($path, PATHINFO_BASENAME); |
|
280 |
+ } |
|
281 |
+ |
|
282 |
+ /** |
|
283 |
+ * Extract the parent directory from a file path. |
|
284 |
+ * |
|
285 |
+ * @param string $path |
|
286 |
+ * @return string |
|
287 |
+ */ |
|
288 |
+ public function dirname($path) |
|
289 |
+ { |
|
290 |
+ return pathinfo($path, PATHINFO_DIRNAME); |
|
291 |
+ } |
|
292 |
+ |
|
293 |
+ /** |
|
294 |
+ * Extract the file extension from a file path. |
|
295 |
+ * |
|
296 |
+ * @param string $path |
|
297 |
+ * @return string |
|
298 |
+ */ |
|
299 |
+ public function extension($path) |
|
300 |
+ { |
|
301 |
+ return pathinfo($path, PATHINFO_EXTENSION); |
|
302 |
+ } |
|
303 |
+ |
|
304 |
+ /** |
|
305 |
+ * Get the file type of a given file. |
|
306 |
+ * |
|
307 |
+ * @param string $path |
|
308 |
+ * @return string |
|
309 |
+ */ |
|
310 |
+ public function type($path) |
|
311 |
+ { |
|
312 |
+ return filetype($path); |
|
313 |
+ } |
|
314 |
+ |
|
315 |
+ /** |
|
316 |
+ * Get the mime-type of a given file. |
|
317 |
+ * |
|
318 |
+ * @param string $path |
|
319 |
+ * @return string|false |
|
320 |
+ */ |
|
321 |
+ public function mimeType($path) |
|
322 |
+ { |
|
323 |
+ return finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path); |
|
324 |
+ } |
|
325 |
+ |
|
326 |
+ /** |
|
327 |
+ * Get the file size of a given file. |
|
328 |
+ * |
|
329 |
+ * @param string $path |
|
330 |
+ * @return int |
|
331 |
+ */ |
|
332 |
+ public function size($path) |
|
333 |
+ { |
|
334 |
+ return filesize($path); |
|
335 |
+ } |
|
336 |
+ |
|
337 |
+ /** |
|
338 |
+ * Get the file's last modification time. |
|
339 |
+ * |
|
340 |
+ * @param string $path |
|
341 |
+ * @return int |
|
342 |
+ */ |
|
343 |
+ public function lastModified($path) |
|
344 |
+ { |
|
345 |
+ return filemtime($path); |
|
346 |
+ } |
|
347 |
+ |
|
348 |
+ /** |
|
349 |
+ * Determine if the given path is a directory. |
|
350 |
+ * |
|
351 |
+ * @param string $directory |
|
352 |
+ * @return bool |
|
353 |
+ */ |
|
354 |
+ public function isDirectory($directory) |
|
355 |
+ { |
|
356 |
+ return is_dir($directory); |
|
357 |
+ } |
|
358 |
+ |
|
359 |
+ /** |
|
360 |
+ * Determine if the given path is readable. |
|
361 |
+ * |
|
362 |
+ * @param string $path |
|
363 |
+ * @return bool |
|
364 |
+ */ |
|
365 |
+ public function isReadable($path) |
|
366 |
+ { |
|
367 |
+ return is_readable($path); |
|
368 |
+ } |
|
369 |
+ |
|
370 |
+ /** |
|
371 |
+ * Determine if the given path is writable. |
|
372 |
+ * |
|
373 |
+ * @param string $path |
|
374 |
+ * @return bool |
|
375 |
+ */ |
|
376 |
+ public function isWritable($path) |
|
377 |
+ { |
|
378 |
+ return is_writable($path); |
|
379 |
+ } |
|
380 |
+ |
|
381 |
+ /** |
|
382 |
+ * Determine if the given path is a file. |
|
383 |
+ * |
|
384 |
+ * @param string $file |
|
385 |
+ * @return bool |
|
386 |
+ */ |
|
387 |
+ public function isFile($file) |
|
388 |
+ { |
|
389 |
+ return is_file($file); |
|
390 |
+ } |
|
391 |
+ |
|
392 |
+ /** |
|
393 |
+ * Find path names matching a given pattern. |
|
394 |
+ * |
|
395 |
+ * @param string $pattern |
|
396 |
+ * @param int $flags |
|
397 |
+ * @return array |
|
398 |
+ */ |
|
399 |
+ public function glob($pattern, $flags = 0) |
|
400 |
+ { |
|
401 |
+ return glob($pattern, $flags); |
|
402 |
+ } |
|
403 |
+ |
|
404 |
+ /** |
|
405 |
+ * Get an array of all files in a directory. |
|
406 |
+ * |
|
407 |
+ * @param string $directory |
|
408 |
+ * @param bool $hidden |
|
409 |
+ * @return \Symfony\Component\Finder\SplFileInfo[] |
|
410 |
+ */ |
|
411 |
+ public function files($directory, $hidden = false) |
|
412 |
+ { |
|
413 |
+ return iterator_to_array( |
|
414 |
+ Finder::create()->files()->ignoreDotFiles(! $hidden)->in($directory)->depth(0)->sortByName(), |
|
415 |
+ false |
|
416 |
+ ); |
|
417 |
+ } |
|
418 |
+ |
|
419 |
+ /** |
|
420 |
+ * Get all of the files from the given directory (recursive). |
|
421 |
+ * |
|
422 |
+ * @param string $directory |
|
423 |
+ * @param bool $hidden |
|
424 |
+ * @return \Symfony\Component\Finder\SplFileInfo[] |
|
425 |
+ */ |
|
426 |
+ public function allFiles($directory, $hidden = false) |
|
427 |
+ { |
|
428 |
+ return iterator_to_array( |
|
429 |
+ Finder::create()->files()->ignoreDotFiles(! $hidden)->in($directory)->sortByName(), |
|
430 |
+ false |
|
431 |
+ ); |
|
432 |
+ } |
|
433 |
+ |
|
434 |
+ /** |
|
435 |
+ * Get all of the directories within a given directory. |
|
436 |
+ * |
|
437 |
+ * @param string $directory |
|
438 |
+ * @return array |
|
439 |
+ */ |
|
440 |
+ public function directories($directory) |
|
441 |
+ { |
|
442 |
+ $directories = []; |
|
443 |
+ |
|
444 |
+ foreach (Finder::create()->in($directory)->directories()->depth(0)->sortByName() as $dir) { |
|
445 |
+ $directories[] = $dir->getPathname(); |
|
446 |
+ } |
|
447 |
+ |
|
448 |
+ return $directories; |
|
449 |
+ } |
|
450 |
+ |
|
451 |
+ /** |
|
452 |
+ * Create a directory. |
|
453 |
+ * |
|
454 |
+ * @param string $path |
|
455 |
+ * @param int $mode |
|
456 |
+ * @param bool $recursive |
|
457 |
+ * @param bool $force |
|
458 |
+ * @return bool |
|
459 |
+ */ |
|
460 |
+ public function makeDirectory($path, $mode = 0755, $recursive = false, $force = false) |
|
461 |
+ { |
|
462 |
+ if ($force) { |
|
463 |
+ return @mkdir($path, $mode, $recursive); |
|
464 |
+ } |
|
465 |
+ |
|
466 |
+ return mkdir($path, $mode, $recursive); |
|
467 |
+ } |
|
468 |
+ |
|
469 |
+ /** |
|
470 |
+ * Move a directory. |
|
471 |
+ * |
|
472 |
+ * @param string $from |
|
473 |
+ * @param string $to |
|
474 |
+ * @param bool $overwrite |
|
475 |
+ * @return bool |
|
476 |
+ */ |
|
477 |
+ public function moveDirectory($from, $to, $overwrite = false) |
|
478 |
+ { |
|
479 |
+ if ($overwrite && $this->isDirectory($to) && ! $this->deleteDirectory($to)) { |
|
480 |
+ return false; |
|
481 |
+ } |
|
482 |
+ |
|
483 |
+ return @rename($from, $to) === true; |
|
484 |
+ } |
|
485 |
+ |
|
486 |
+ /** |
|
487 |
+ * Copy a directory from one location to another. |
|
488 |
+ * |
|
489 |
+ * @param string $directory |
|
490 |
+ * @param string $destination |
|
491 |
+ * @param int|null $options |
|
492 |
+ * @return bool |
|
493 |
+ */ |
|
494 |
+ public function copyDirectory($directory, $destination, $options = null) |
|
495 |
+ { |
|
496 |
+ if (! $this->isDirectory($directory)) { |
|
497 |
+ return false; |
|
498 |
+ } |
|
499 |
+ |
|
500 |
+ $options = $options ?: FilesystemIterator::SKIP_DOTS; |
|
501 |
+ |
|
502 |
+ // If the destination directory does not actually exist, we will go ahead and |
|
503 |
+ // create it recursively, which just gets the destination prepared to copy |
|
504 |
+ // the files over. Once we make the directory we'll proceed the copying. |
|
505 |
+ if (! $this->isDirectory($destination)) { |
|
506 |
+ $this->makeDirectory($destination, 0777, true); |
|
507 |
+ } |
|
508 |
+ |
|
509 |
+ $items = new FilesystemIterator($directory, $options); |
|
510 |
+ |
|
511 |
+ foreach ($items as $item) { |
|
512 |
+ // As we spin through items, we will check to see if the current file is actually |
|
513 |
+ // a directory or a file. When it is actually a directory we will need to call |
|
514 |
+ // back into this function recursively to keep copying these nested folders. |
|
515 |
+ $target = $destination.'/'.$item->getBasename(); |
|
516 |
+ |
|
517 |
+ if ($item->isDir()) { |
|
518 |
+ $path = $item->getPathname(); |
|
519 |
+ |
|
520 |
+ if (! $this->copyDirectory($path, $target, $options)) { |
|
521 |
+ return false; |
|
522 |
+ } |
|
523 |
+ } |
|
524 |
+ |
|
525 |
+ // If the current items is just a regular file, we will just copy this to the new |
|
526 |
+ // location and keep looping. If for some reason the copy fails we'll bail out |
|
527 |
+ // and return false, so the developer is aware that the copy process failed. |
|
528 |
+ else { |
|
529 |
+ if (! $this->copy($item->getPathname(), $target)) { |
|
530 |
+ return false; |
|
531 |
+ } |
|
532 |
+ } |
|
533 |
+ } |
|
534 |
+ |
|
535 |
+ return true; |
|
536 |
+ } |
|
537 |
+ |
|
538 |
+ /** |
|
539 |
+ * Recursively delete a directory. |
|
540 |
+ * |
|
541 |
+ * The directory itself may be optionally preserved. |
|
542 |
+ * |
|
543 |
+ * @param string $directory |
|
544 |
+ * @param bool $preserve |
|
545 |
+ * @return bool |
|
546 |
+ */ |
|
547 |
+ public function deleteDirectory($directory, $preserve = false) |
|
548 |
+ { |
|
549 |
+ if (! $this->isDirectory($directory)) { |
|
550 |
+ return false; |
|
551 |
+ } |
|
552 |
+ |
|
553 |
+ $items = new FilesystemIterator($directory); |
|
554 |
+ |
|
555 |
+ foreach ($items as $item) { |
|
556 |
+ // If the item is a directory, we can just recurse into the function and |
|
557 |
+ // delete that sub-directory otherwise we'll just delete the file and |
|
558 |
+ // keep iterating through each file until the directory is cleaned. |
|
559 |
+ if ($item->isDir() && ! $item->isLink()) { |
|
560 |
+ $this->deleteDirectory($item->getPathname()); |
|
561 |
+ } |
|
562 |
+ |
|
563 |
+ // If the item is just a file, we can go ahead and delete it since we're |
|
564 |
+ // just looping through and waxing all of the files in this directory |
|
565 |
+ // and calling directories recursively, so we delete the real path. |
|
566 |
+ else { |
|
567 |
+ $this->delete($item->getPathname()); |
|
568 |
+ } |
|
569 |
+ } |
|
570 |
+ |
|
571 |
+ if (! $preserve) { |
|
572 |
+ @rmdir($directory); |
|
573 |
+ } |
|
574 |
+ |
|
575 |
+ return true; |
|
576 |
+ } |
|
577 |
+ |
|
578 |
+ /** |
|
579 |
+ * Remove all of the directories within a given directory. |
|
580 |
+ * |
|
581 |
+ * @param string $directory |
|
582 |
+ * @return bool |
|
583 |
+ */ |
|
584 |
+ public function deleteDirectories($directory) |
|
585 |
+ { |
|
586 |
+ $allDirectories = $this->directories($directory); |
|
587 |
+ |
|
588 |
+ if (! empty($allDirectories)) { |
|
589 |
+ foreach ($allDirectories as $directoryName) { |
|
590 |
+ $this->deleteDirectory($directoryName); |
|
591 |
+ } |
|
592 |
+ |
|
593 |
+ return true; |
|
594 |
+ } |
|
595 |
+ |
|
596 |
+ return false; |
|
597 |
+ } |
|
598 |
+ |
|
599 |
+ /** |
|
600 |
+ * Empty the specified directory of all files and folders. |
|
601 |
+ * |
|
602 |
+ * @param string $directory |
|
603 |
+ * @return bool |
|
604 |
+ */ |
|
605 |
+ public function cleanDirectory($directory) |
|
606 |
+ { |
|
607 |
+ return $this->deleteDirectory($directory, true); |
|
608 |
+ } |
|
609 |
+} |