|
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
|
|
|
use Helldar\Support\Facades\Helpers\Instance; |
|
10
|
|
|
use SplFileInfo; |
|
11
|
|
|
|
|
12
|
|
|
final class Directory |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Get a list of files and folders in a directory. |
|
16
|
|
|
* |
|
17
|
|
|
* @param string $path |
|
18
|
|
|
* |
|
19
|
|
|
* @throws \Helldar\Support\Exceptions\DirectoryNotFoundException |
|
20
|
|
|
* |
|
21
|
|
|
* @return DirectoryIterator |
|
22
|
|
|
*/ |
|
23
|
|
|
public function all(string $path): DirectoryIterator |
|
24
|
|
|
{ |
|
25
|
|
|
if ($this->doesntExist($path)) { |
|
26
|
|
|
throw new DirectoryNotFoundException($path); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return new DirectoryIterator($path); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get a list of directory names along a path. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $path |
|
36
|
|
|
* |
|
37
|
|
|
* @throws \Helldar\Support\Exceptions\DirectoryNotFoundException |
|
38
|
|
|
* |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
public function names(string $path): array |
|
42
|
|
|
{ |
|
43
|
|
|
$items = []; |
|
44
|
|
|
|
|
45
|
|
|
foreach ($this->all($path) as $directory) { |
|
46
|
|
|
if ($directory->isDir() && ! $directory->isDot()) { |
|
47
|
|
|
$items[] = $directory->getFilename(); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
sort($items); |
|
52
|
|
|
|
|
53
|
|
|
return array_values($items); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Create a directory at the specified path. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $path |
|
60
|
|
|
* @param int $mode |
|
61
|
|
|
* |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
|
|
public function make(string $path, int $mode = 755): bool |
|
65
|
|
|
{ |
|
66
|
|
|
if ($this->doesntExist($path)) { |
|
67
|
|
|
return mkdir($path, $mode, true); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Delete the directory with all contents in the specified path. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $path |
|
77
|
|
|
* |
|
78
|
|
|
* @throws \Helldar\Support\Exceptions\DirectoryNotFoundException |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function delete(string $path): bool |
|
83
|
|
|
{ |
|
84
|
|
|
if (! $this->isDirectory($path)) { |
|
85
|
|
|
throw new DirectoryNotFoundException($path); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$items = new FilesystemIterator($path); |
|
89
|
|
|
|
|
90
|
|
|
$success = true; |
|
91
|
|
|
|
|
92
|
|
|
foreach ($items as $item) { |
|
93
|
|
|
$item->isDir() && ! $item->isLink() |
|
94
|
|
|
? $this->delete($item->getPathname()) |
|
95
|
|
|
: File::delete($item->getPathname()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
@rmdir($path); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
return $success; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Check if the directory exists. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $path |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
|
|
public function exists(string $path): bool |
|
111
|
|
|
{ |
|
112
|
|
|
return file_exists($path) && is_dir($path); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Check if the directory doesn't exists. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $path |
|
119
|
|
|
* |
|
120
|
|
|
* @return bool |
|
121
|
|
|
*/ |
|
122
|
|
|
public function doesntExist(string $path): bool |
|
123
|
|
|
{ |
|
124
|
|
|
return ! $this->exists($path); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Check if object or path is a directory. |
|
129
|
|
|
* |
|
130
|
|
|
* @param DirectoryIterator|\SplFileInfo|string $value |
|
131
|
|
|
* |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
134
|
|
|
public function isDirectory($value): bool |
|
135
|
|
|
{ |
|
136
|
|
|
if (Instance::of($value, [SplFileInfo::class, DirectoryIterator::class])) { |
|
137
|
|
|
return $value->isDir(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return is_dir($value); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
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: