|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Helldar\Support\Helpers\Filesystem; |
|
4
|
|
|
|
|
5
|
|
|
use DirectoryIterator; |
|
6
|
|
|
use FilesystemIterator; |
|
7
|
|
|
use Helldar\Support\Exceptions\DirectoryNotFoundException; |
|
8
|
|
|
use Helldar\Support\Facades\Helpers\Filesystem\File; |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
final class Directory |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @param string $path |
|
14
|
|
|
* |
|
15
|
|
|
* @throws \Helldar\Support\Exceptions\DirectoryNotFoundException |
|
16
|
|
|
* |
|
17
|
|
|
* @return DirectoryIterator |
|
18
|
|
|
*/ |
|
19
|
|
|
public function all(string $path): DirectoryIterator |
|
20
|
|
|
{ |
|
21
|
|
|
if ($this->doesntExist($path)) { |
|
22
|
|
|
throw new DirectoryNotFoundException($path); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
return new DirectoryIterator($path); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function names(string $path): array |
|
29
|
|
|
{ |
|
30
|
|
|
$items = []; |
|
31
|
|
|
|
|
32
|
|
|
foreach ($this->all($path) as $directory) { |
|
33
|
|
|
if ($directory->isDir() && ! $directory->isDot()) { |
|
34
|
|
|
$items[] = $directory->getFilename(); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
sort($items); |
|
39
|
|
|
|
|
40
|
|
|
return array_values($items); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function make(string $path, int $mode = 755): bool |
|
44
|
|
|
{ |
|
45
|
|
|
if ($this->doesntExist($path)) { |
|
46
|
|
|
return mkdir($path, $mode, true); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function delete(string $path): bool |
|
53
|
|
|
{ |
|
54
|
|
|
if ($this->doesntExist($path)) { |
|
55
|
|
|
throw new DirectoryNotFoundException($path); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$items = new FilesystemIterator($path); |
|
59
|
|
|
|
|
60
|
|
|
$success = true; |
|
61
|
|
|
|
|
62
|
|
|
foreach ($items as $item) { |
|
63
|
|
|
$item->isDir() && ! $item->isLink() |
|
64
|
|
|
? $this->delete($item->getPathname()) |
|
65
|
|
|
: File::delete($item->getPathname()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
@rmdir($path); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
return $success; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function exists(string $path): bool |
|
74
|
|
|
{ |
|
75
|
|
|
return file_exists($path) && is_dir($path); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function doesntExist(string $path): bool |
|
79
|
|
|
{ |
|
80
|
|
|
return ! $this->exists($path); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: