|
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 as FileHelper; |
|
9
|
|
|
use Helldar\Support\Facades\Helpers\Instance; |
|
10
|
|
|
use Helldar\Support\Facades\Helpers\Str; |
|
11
|
|
|
use SplFileInfo; |
|
12
|
|
|
|
|
13
|
|
|
final class Directory |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Get a list of files and folders in a directory. |
|
17
|
|
|
* |
|
18
|
|
|
* @param string $path |
|
19
|
|
|
* |
|
20
|
|
|
* @throws \Helldar\Support\Exceptions\DirectoryNotFoundException |
|
21
|
|
|
* |
|
22
|
|
|
* @return DirectoryIterator |
|
23
|
|
|
*/ |
|
24
|
18 |
|
public function all(string $path): DirectoryIterator |
|
25
|
|
|
{ |
|
26
|
18 |
|
if ($this->doesntExist($path)) { |
|
27
|
4 |
|
throw new DirectoryNotFoundException($path); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
14 |
|
return new DirectoryIterator($path); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get a list of directory names along a path. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $path |
|
37
|
|
|
* @param callable|null $callback |
|
38
|
|
|
* @param bool $recursive |
|
39
|
|
|
* |
|
40
|
|
|
* @throws \Helldar\Support\Exceptions\DirectoryNotFoundException |
|
41
|
|
|
* |
|
42
|
|
|
* @return array |
|
43
|
|
|
*/ |
|
44
|
6 |
|
public function names(string $path, callable $callback = null, bool $recursive = false): array |
|
45
|
|
|
{ |
|
46
|
6 |
|
$items = []; |
|
47
|
|
|
|
|
48
|
|
|
/** @var \DirectoryIterator $directory */ |
|
49
|
6 |
|
foreach ($this->all($path) as $directory) { |
|
50
|
6 |
|
if ($directory->isDir() && ! $directory->isDot()) { |
|
51
|
6 |
|
$name = $directory->getFilename(); |
|
52
|
|
|
|
|
53
|
6 |
|
if (! is_callable($callback) || $callback($name)) { |
|
54
|
6 |
|
$items[] = $name; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
6 |
|
if ($recursive && $directory->isDir() && ! $directory->isDot()) { |
|
59
|
2 |
|
$prefix = (string) Str::of($directory->getRealPath()) |
|
60
|
2 |
|
->after(realpath($path)) |
|
61
|
2 |
|
->trim('\\/'); |
|
62
|
|
|
|
|
63
|
2 |
|
foreach ($this->names($directory->getRealPath(), $callback, $recursive) as $value) { |
|
64
|
2 |
|
$items[] = $prefix . '/' . $value; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
6 |
|
sort($items); |
|
70
|
|
|
|
|
71
|
6 |
|
return array_values($items); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Create a directory at the specified path. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $path |
|
78
|
|
|
* @param int $mode |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
32 |
|
public function make(string $path, int $mode = 0755): bool |
|
83
|
|
|
{ |
|
84
|
32 |
|
return ! $this->doesntExist($path) || mkdir($path, $mode, true); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Delete the directory with all contents in the specified path. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $path |
|
91
|
|
|
* |
|
92
|
|
|
* @throws \Helldar\Support\Exceptions\DirectoryNotFoundException |
|
93
|
|
|
* |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
34 |
|
public function delete(string $path): bool |
|
97
|
|
|
{ |
|
98
|
34 |
|
if (! $this->isDirectory($path)) { |
|
99
|
4 |
|
throw new DirectoryNotFoundException($path); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
32 |
|
$items = new FilesystemIterator($path); |
|
103
|
|
|
|
|
104
|
32 |
|
$success = true; |
|
105
|
|
|
|
|
106
|
32 |
|
foreach ($items as $item) { |
|
107
|
30 |
|
$item->isDir() && ! $item->isLink() |
|
108
|
30 |
|
? $this->delete($item->getPathname()) |
|
109
|
24 |
|
: FileHelper::delete($item->getPathname()); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
32 |
|
@rmdir($path); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
32 |
|
return $success; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Check if the directory exists. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $path |
|
121
|
|
|
* |
|
122
|
|
|
* @return bool |
|
123
|
|
|
*/ |
|
124
|
474 |
|
public function exists(string $path): bool |
|
125
|
|
|
{ |
|
126
|
474 |
|
return file_exists($path) && is_dir($path); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Check if the directory doesn't exists. |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $path |
|
133
|
|
|
* |
|
134
|
|
|
* @return bool |
|
135
|
|
|
*/ |
|
136
|
52 |
|
public function doesntExist(string $path): bool |
|
137
|
|
|
{ |
|
138
|
52 |
|
return ! $this->exists($path); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Check if object or path is a directory. |
|
143
|
|
|
* |
|
144
|
|
|
* @param DirectoryIterator|\SplFileInfo|string $value |
|
145
|
|
|
* |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
46 |
|
public function isDirectory($value): bool |
|
149
|
|
|
{ |
|
150
|
46 |
|
if (Instance::of($value, [SplFileInfo::class, DirectoryIterator::class])) { |
|
151
|
2 |
|
return $value->isDir(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
44 |
|
return is_dir($value); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Checks the existence of a directory. |
|
159
|
|
|
* |
|
160
|
|
|
* @param DirectoryIterator|\SplFileInfo|string $path |
|
161
|
|
|
* |
|
162
|
|
|
* @throws \Helldar\Support\Exceptions\DirectoryNotFoundException |
|
163
|
|
|
*/ |
|
164
|
8 |
|
public function validate($path): void |
|
165
|
|
|
{ |
|
166
|
8 |
|
if (! $this->isDirectory($path)) { |
|
167
|
4 |
|
throw new DirectoryNotFoundException($path); |
|
168
|
|
|
} |
|
169
|
4 |
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Checks the existence of a directory and return full path if exist. |
|
173
|
|
|
* |
|
174
|
|
|
* @param DirectoryIterator|\SplFileInfo|string $path |
|
175
|
|
|
* |
|
176
|
|
|
* @throws \Helldar\Support\Exceptions\DirectoryNotFoundException |
|
177
|
|
|
* |
|
178
|
|
|
* @return string |
|
179
|
|
|
*/ |
|
180
|
4 |
|
public function validated($path): string |
|
181
|
|
|
{ |
|
182
|
4 |
|
$this->validate($path); |
|
183
|
|
|
|
|
184
|
2 |
|
return realpath($path); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: