1 |
cb15593b
|
Cajova-Houba
|
<?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 |
|
|
* Write the contents of a file.
|
103 |
|
|
*
|
104 |
|
|
* @param string $path
|
105 |
|
|
* @param string $contents
|
106 |
|
|
* @param bool $lock
|
107 |
|
|
* @return int
|
108 |
|
|
*/
|
109 |
|
|
public function put($path, $contents, $lock = false)
|
110 |
|
|
{
|
111 |
|
|
return file_put_contents($path, $contents, $lock ? LOCK_EX : 0);
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
/**
|
115 |
|
|
* Prepend to a file.
|
116 |
|
|
*
|
117 |
|
|
* @param string $path
|
118 |
|
|
* @param string $data
|
119 |
|
|
* @return int
|
120 |
|
|
*/
|
121 |
|
|
public function prepend($path, $data)
|
122 |
|
|
{
|
123 |
|
|
if ($this->exists($path)) {
|
124 |
|
|
return $this->put($path, $data.$this->get($path));
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
return $this->put($path, $data);
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
/**
|
131 |
|
|
* Append to a file.
|
132 |
|
|
*
|
133 |
|
|
* @param string $path
|
134 |
|
|
* @param string $data
|
135 |
|
|
* @return int
|
136 |
|
|
*/
|
137 |
|
|
public function append($path, $data)
|
138 |
|
|
{
|
139 |
|
|
return file_put_contents($path, $data, FILE_APPEND);
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
/**
|
143 |
|
|
* Delete the file at a given path.
|
144 |
|
|
*
|
145 |
|
|
* @param string|array $paths
|
146 |
|
|
* @return bool
|
147 |
|
|
*/
|
148 |
|
|
public function delete($paths)
|
149 |
|
|
{
|
150 |
|
|
$paths = is_array($paths) ? $paths : func_get_args();
|
151 |
|
|
|
152 |
|
|
$success = true;
|
153 |
|
|
|
154 |
|
|
foreach ($paths as $path) {
|
155 |
|
|
try {
|
156 |
|
|
if (! @unlink($path)) {
|
157 |
|
|
$success = false;
|
158 |
|
|
}
|
159 |
|
|
} catch (ErrorException $e) {
|
160 |
|
|
$success = false;
|
161 |
|
|
}
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
return $success;
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
/**
|
168 |
|
|
* Move a file to a new location.
|
169 |
|
|
*
|
170 |
|
|
* @param string $path
|
171 |
|
|
* @param string $target
|
172 |
|
|
* @return bool
|
173 |
|
|
*/
|
174 |
|
|
public function move($path, $target)
|
175 |
|
|
{
|
176 |
|
|
return rename($path, $target);
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
/**
|
180 |
|
|
* Copy a file to a new location.
|
181 |
|
|
*
|
182 |
|
|
* @param string $path
|
183 |
|
|
* @param string $target
|
184 |
|
|
* @return bool
|
185 |
|
|
*/
|
186 |
|
|
public function copy($path, $target)
|
187 |
|
|
{
|
188 |
|
|
return copy($path, $target);
|
189 |
|
|
}
|
190 |
|
|
|
191 |
|
|
/**
|
192 |
|
|
* Extract the file name from a file path.
|
193 |
|
|
*
|
194 |
|
|
* @param string $path
|
195 |
|
|
* @return string
|
196 |
|
|
*/
|
197 |
|
|
public function name($path)
|
198 |
|
|
{
|
199 |
|
|
return pathinfo($path, PATHINFO_FILENAME);
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
/**
|
203 |
|
|
* Extract the trailing name component from a file path.
|
204 |
|
|
*
|
205 |
|
|
* @param string $path
|
206 |
|
|
* @return string
|
207 |
|
|
*/
|
208 |
|
|
public function basename($path)
|
209 |
|
|
{
|
210 |
|
|
return pathinfo($path, PATHINFO_BASENAME);
|
211 |
|
|
}
|
212 |
|
|
|
213 |
|
|
/**
|
214 |
|
|
* Extract the parent directory from a file path.
|
215 |
|
|
*
|
216 |
|
|
* @param string $path
|
217 |
|
|
* @return string
|
218 |
|
|
*/
|
219 |
|
|
public function dirname($path)
|
220 |
|
|
{
|
221 |
|
|
return pathinfo($path, PATHINFO_DIRNAME);
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
/**
|
225 |
|
|
* Extract the file extension from a file path.
|
226 |
|
|
*
|
227 |
|
|
* @param string $path
|
228 |
|
|
* @return string
|
229 |
|
|
*/
|
230 |
|
|
public function extension($path)
|
231 |
|
|
{
|
232 |
|
|
return pathinfo($path, PATHINFO_EXTENSION);
|
233 |
|
|
}
|
234 |
|
|
|
235 |
|
|
/**
|
236 |
|
|
* Get the file type of a given file.
|
237 |
|
|
*
|
238 |
|
|
* @param string $path
|
239 |
|
|
* @return string
|
240 |
|
|
*/
|
241 |
|
|
public function type($path)
|
242 |
|
|
{
|
243 |
|
|
return filetype($path);
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
/**
|
247 |
|
|
* Get the mime-type of a given file.
|
248 |
|
|
*
|
249 |
|
|
* @param string $path
|
250 |
|
|
* @return string|false
|
251 |
|
|
*/
|
252 |
|
|
public function mimeType($path)
|
253 |
|
|
{
|
254 |
|
|
return finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
|
255 |
|
|
}
|
256 |
|
|
|
257 |
|
|
/**
|
258 |
|
|
* Get the file size of a given file.
|
259 |
|
|
*
|
260 |
|
|
* @param string $path
|
261 |
|
|
* @return int
|
262 |
|
|
*/
|
263 |
|
|
public function size($path)
|
264 |
|
|
{
|
265 |
|
|
return filesize($path);
|
266 |
|
|
}
|
267 |
|
|
|
268 |
|
|
/**
|
269 |
|
|
* Get the file's last modification time.
|
270 |
|
|
*
|
271 |
|
|
* @param string $path
|
272 |
|
|
* @return int
|
273 |
|
|
*/
|
274 |
|
|
public function lastModified($path)
|
275 |
|
|
{
|
276 |
|
|
return filemtime($path);
|
277 |
|
|
}
|
278 |
|
|
|
279 |
|
|
/**
|
280 |
|
|
* Determine if the given path is a directory.
|
281 |
|
|
*
|
282 |
|
|
* @param string $directory
|
283 |
|
|
* @return bool
|
284 |
|
|
*/
|
285 |
|
|
public function isDirectory($directory)
|
286 |
|
|
{
|
287 |
|
|
return is_dir($directory);
|
288 |
|
|
}
|
289 |
|
|
|
290 |
|
|
/**
|
291 |
|
|
* Determine if the given path is writable.
|
292 |
|
|
*
|
293 |
|
|
* @param string $path
|
294 |
|
|
* @return bool
|
295 |
|
|
*/
|
296 |
|
|
public function isWritable($path)
|
297 |
|
|
{
|
298 |
|
|
return is_writable($path);
|
299 |
|
|
}
|
300 |
|
|
|
301 |
|
|
/**
|
302 |
|
|
* Determine if the given path is a file.
|
303 |
|
|
*
|
304 |
|
|
* @param string $file
|
305 |
|
|
* @return bool
|
306 |
|
|
*/
|
307 |
|
|
public function isFile($file)
|
308 |
|
|
{
|
309 |
|
|
return is_file($file);
|
310 |
|
|
}
|
311 |
|
|
|
312 |
|
|
/**
|
313 |
|
|
* Find path names matching a given pattern.
|
314 |
|
|
*
|
315 |
|
|
* @param string $pattern
|
316 |
|
|
* @param int $flags
|
317 |
|
|
* @return array
|
318 |
|
|
*/
|
319 |
|
|
public function glob($pattern, $flags = 0)
|
320 |
|
|
{
|
321 |
|
|
return glob($pattern, $flags);
|
322 |
|
|
}
|
323 |
|
|
|
324 |
|
|
/**
|
325 |
|
|
* Get an array of all files in a directory.
|
326 |
|
|
*
|
327 |
|
|
* @param string $directory
|
328 |
|
|
* @return array
|
329 |
|
|
*/
|
330 |
|
|
public function files($directory)
|
331 |
|
|
{
|
332 |
|
|
$glob = glob($directory.'/*');
|
333 |
|
|
|
334 |
|
|
if ($glob === false) {
|
335 |
|
|
return [];
|
336 |
|
|
}
|
337 |
|
|
|
338 |
|
|
// To get the appropriate files, we'll simply glob the directory and filter
|
339 |
|
|
// out any "files" that are not truly files so we do not end up with any
|
340 |
|
|
// directories in our list, but only true files within the directory.
|
341 |
|
|
return array_filter($glob, function ($file) {
|
342 |
|
|
return filetype($file) == 'file';
|
343 |
|
|
});
|
344 |
|
|
}
|
345 |
|
|
|
346 |
|
|
/**
|
347 |
|
|
* Get all of the files from the given directory (recursive).
|
348 |
|
|
*
|
349 |
|
|
* @param string $directory
|
350 |
|
|
* @param bool $hidden
|
351 |
|
|
* @return array
|
352 |
|
|
*/
|
353 |
|
|
public function allFiles($directory, $hidden = false)
|
354 |
|
|
{
|
355 |
|
|
return iterator_to_array(Finder::create()->files()->ignoreDotFiles(! $hidden)->in($directory), false);
|
356 |
|
|
}
|
357 |
|
|
|
358 |
|
|
/**
|
359 |
|
|
* Get all of the directories within a given directory.
|
360 |
|
|
*
|
361 |
|
|
* @param string $directory
|
362 |
|
|
* @return array
|
363 |
|
|
*/
|
364 |
|
|
public function directories($directory)
|
365 |
|
|
{
|
366 |
|
|
$directories = [];
|
367 |
|
|
|
368 |
|
|
foreach (Finder::create()->in($directory)->directories()->depth(0) as $dir) {
|
369 |
|
|
$directories[] = $dir->getPathname();
|
370 |
|
|
}
|
371 |
|
|
|
372 |
|
|
return $directories;
|
373 |
|
|
}
|
374 |
|
|
|
375 |
|
|
/**
|
376 |
|
|
* Create a directory.
|
377 |
|
|
*
|
378 |
|
|
* @param string $path
|
379 |
|
|
* @param int $mode
|
380 |
|
|
* @param bool $recursive
|
381 |
|
|
* @param bool $force
|
382 |
|
|
* @return bool
|
383 |
|
|
*/
|
384 |
|
|
public function makeDirectory($path, $mode = 0755, $recursive = false, $force = false)
|
385 |
|
|
{
|
386 |
|
|
if ($force) {
|
387 |
|
|
return @mkdir($path, $mode, $recursive);
|
388 |
|
|
}
|
389 |
|
|
|
390 |
|
|
return mkdir($path, $mode, $recursive);
|
391 |
|
|
}
|
392 |
|
|
|
393 |
|
|
/**
|
394 |
|
|
* Move a directory.
|
395 |
|
|
*
|
396 |
|
|
* @param string $from
|
397 |
|
|
* @param string $to
|
398 |
|
|
* @param bool $overwrite
|
399 |
|
|
* @return bool
|
400 |
|
|
*/
|
401 |
|
|
public function moveDirectory($from, $to, $overwrite = false)
|
402 |
|
|
{
|
403 |
|
|
if ($overwrite && $this->isDirectory($to)) {
|
404 |
|
|
if (! $this->deleteDirectory($to)) {
|
405 |
|
|
return false;
|
406 |
|
|
}
|
407 |
|
|
}
|
408 |
|
|
|
409 |
|
|
return @rename($from, $to) === true;
|
410 |
|
|
}
|
411 |
|
|
|
412 |
|
|
/**
|
413 |
|
|
* Copy a directory from one location to another.
|
414 |
|
|
*
|
415 |
|
|
* @param string $directory
|
416 |
|
|
* @param string $destination
|
417 |
|
|
* @param int $options
|
418 |
|
|
* @return bool
|
419 |
|
|
*/
|
420 |
|
|
public function copyDirectory($directory, $destination, $options = null)
|
421 |
|
|
{
|
422 |
|
|
if (! $this->isDirectory($directory)) {
|
423 |
|
|
return false;
|
424 |
|
|
}
|
425 |
|
|
|
426 |
|
|
$options = $options ?: FilesystemIterator::SKIP_DOTS;
|
427 |
|
|
|
428 |
|
|
// If the destination directory does not actually exist, we will go ahead and
|
429 |
|
|
// create it recursively, which just gets the destination prepared to copy
|
430 |
|
|
// the files over. Once we make the directory we'll proceed the copying.
|
431 |
|
|
if (! $this->isDirectory($destination)) {
|
432 |
|
|
$this->makeDirectory($destination, 0777, true);
|
433 |
|
|
}
|
434 |
|
|
|
435 |
|
|
$items = new FilesystemIterator($directory, $options);
|
436 |
|
|
|
437 |
|
|
foreach ($items as $item) {
|
438 |
|
|
// As we spin through items, we will check to see if the current file is actually
|
439 |
|
|
// a directory or a file. When it is actually a directory we will need to call
|
440 |
|
|
// back into this function recursively to keep copying these nested folders.
|
441 |
|
|
$target = $destination.'/'.$item->getBasename();
|
442 |
|
|
|
443 |
|
|
if ($item->isDir()) {
|
444 |
|
|
$path = $item->getPathname();
|
445 |
|
|
|
446 |
|
|
if (! $this->copyDirectory($path, $target, $options)) {
|
447 |
|
|
return false;
|
448 |
|
|
}
|
449 |
|
|
}
|
450 |
|
|
|
451 |
|
|
// If the current items is just a regular file, we will just copy this to the new
|
452 |
|
|
// location and keep looping. If for some reason the copy fails we'll bail out
|
453 |
|
|
// and return false, so the developer is aware that the copy process failed.
|
454 |
|
|
else {
|
455 |
|
|
if (! $this->copy($item->getPathname(), $target)) {
|
456 |
|
|
return false;
|
457 |
|
|
}
|
458 |
|
|
}
|
459 |
|
|
}
|
460 |
|
|
|
461 |
|
|
return true;
|
462 |
|
|
}
|
463 |
|
|
|
464 |
|
|
/**
|
465 |
|
|
* Recursively delete a directory.
|
466 |
|
|
*
|
467 |
|
|
* The directory itself may be optionally preserved.
|
468 |
|
|
*
|
469 |
|
|
* @param string $directory
|
470 |
|
|
* @param bool $preserve
|
471 |
|
|
* @return bool
|
472 |
|
|
*/
|
473 |
|
|
public function deleteDirectory($directory, $preserve = false)
|
474 |
|
|
{
|
475 |
|
|
if (! $this->isDirectory($directory)) {
|
476 |
|
|
return false;
|
477 |
|
|
}
|
478 |
|
|
|
479 |
|
|
$items = new FilesystemIterator($directory);
|
480 |
|
|
|
481 |
|
|
foreach ($items as $item) {
|
482 |
|
|
// If the item is a directory, we can just recurse into the function and
|
483 |
|
|
// delete that sub-directory otherwise we'll just delete the file and
|
484 |
|
|
// keep iterating through each file until the directory is cleaned.
|
485 |
|
|
if ($item->isDir() && ! $item->isLink()) {
|
486 |
|
|
$this->deleteDirectory($item->getPathname());
|
487 |
|
|
}
|
488 |
|
|
|
489 |
|
|
// If the item is just a file, we can go ahead and delete it since we're
|
490 |
|
|
// just looping through and waxing all of the files in this directory
|
491 |
|
|
// and calling directories recursively, so we delete the real path.
|
492 |
|
|
else {
|
493 |
|
|
$this->delete($item->getPathname());
|
494 |
|
|
}
|
495 |
|
|
}
|
496 |
|
|
|
497 |
|
|
if (! $preserve) {
|
498 |
|
|
@rmdir($directory);
|
499 |
|
|
}
|
500 |
|
|
|
501 |
|
|
return true;
|
502 |
|
|
}
|
503 |
|
|
|
504 |
|
|
/**
|
505 |
|
|
* Empty the specified directory of all files and folders.
|
506 |
|
|
*
|
507 |
|
|
* @param string $directory
|
508 |
|
|
* @return bool
|
509 |
|
|
*/
|
510 |
|
|
public function cleanDirectory($directory)
|
511 |
|
|
{
|
512 |
|
|
return $this->deleteDirectory($directory, true);
|
513 |
|
|
}
|
514 |
|
|
}
|