-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectory.php
247 lines (225 loc) · 7.23 KB
/
Directory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
namespace Aternos\IO\System\Directory;
use Aternos\IO\Exception\CreateDirectoryException;
use Aternos\IO\Exception\DeleteException;
use Aternos\IO\Exception\GetTargetException;
use Aternos\IO\Exception\IOException;
use Aternos\IO\Exception\MissingPermissionsException;
use Aternos\IO\Interfaces\Features\GetChildrenInterface;
use Aternos\IO\Interfaces\Features\GetPathInterface;
use Aternos\IO\Interfaces\Features\GetTargetInterface;
use Aternos\IO\Interfaces\IOElementInterface;
use Aternos\IO\Interfaces\Types\DirectoryInterface;
use Aternos\IO\System\File\File;
use Aternos\IO\System\FilesystemElement;
use Aternos\IO\System\Link\DirectoryLink;
use Aternos\IO\System\Link\FileLink;
use Aternos\IO\System\Link\Link;
use DirectoryIterator;
use Generator;
use InvalidArgumentException;
/**
* Class Directory
*
* Filesystem directory
*
* @package Aternos\IO\System\Directory
*/
class Directory extends FilesystemElement implements DirectoryInterface
{
public const int MAX_DEPTH = 100;
/**
* @inheritDoc
* @throws MissingPermissionsException
* @throws GetTargetException
*/
public function getChildren(bool $allowOutsideLinks = false): Generator
{
if (!is_readable($this->path)) {
throw new MissingPermissionsException("Could not read directory due to missing read permissions (" . $this->path . ")", $this);
}
if (!is_dir($this->path)) {
return;
}
foreach (new DirectoryIterator($this->path) as $fileInfo) {
if ($fileInfo->isDot()) {
continue;
}
$element = static::getIOElementFromPath($fileInfo->getPathname());
if (!$allowOutsideLinks && !$this->isElementInDirectory($element)) {
continue;
}
yield $element;
}
}
/**
* @inheritDoc
* @return FilesystemElement
*/
public function getChild(string $name, string ...$features): FilesystemElement
{
/** @var class-string<FilesystemElement>[] $supportedChildClasses */
$supportedChildClasses = [
Directory::class,
DirectoryLink::class,
File::class,
FileLink::class,
Link::class
];
/** @var class-string<FilesystemElement> $childClass */
$childClass = $this->findInstanceOfAll($supportedChildClasses, $features);
if (!$childClass) {
throw new InvalidArgumentException("No supported child class found for features: " . implode(", ", $features));
}
return new $childClass($this->getPath() . DIRECTORY_SEPARATOR . $name);
}
/**
* Find a class that is an instance of all required classes
*
* @param class-string[] $availableClasses
* @param class-string[] $requiredClasses
* @return string|null
*/
protected function findInstanceOfAll(array $availableClasses, array $requiredClasses): ?string
{
foreach ($availableClasses as $availableClass) {
if ($this->isInstanceOfAll($availableClass, $requiredClasses)) {
return $availableClass;
}
}
return null;
}
/**
* Check if a class is an instance of all required classes
*
* @param class-string $class
* @param class-string[] $requiredClasses
* @return bool
*/
protected function isInstanceOfAll(string $class, array $requiredClasses): bool
{
foreach ($requiredClasses as $requiredClass) {
if (!is_a($class, $requiredClass, true)) {
return false;
}
}
return true;
}
/**
* @inheritDoc
* @throws GetTargetException
* @throws MissingPermissionsException
*/
public function getChildrenRecursive(bool $allowOutsideLinks = false, bool $followLinks = true, int $currentDepth = 0): Generator
{
$currentDepth++;
foreach ($this->getChildren($allowOutsideLinks) as $child) {
yield $child;
if ($child instanceof GetTargetInterface && !$followLinks) {
continue;
}
if ($child instanceof GetChildrenInterface) {
if ($currentDepth >= static::MAX_DEPTH) {
continue;
}
foreach ($child->getChildrenRecursive(true, $followLinks, $currentDepth) as $subChild) {
if (!$allowOutsideLinks && !$this->isElementInDirectory($subChild)) {
continue;
}
yield $subChild;
}
}
}
}
/**
* Check if an element is in this directory
*
* @param GetPathInterface $element
* @return bool
* @throws GetTargetException
* @throws IOException
*/
protected function isElementInDirectory(GetPathInterface $element): bool
{
if ($element instanceof Link) {
return $this->isLinkInDirectory($element);
}
return $this->isPathInDirectory($element->getPath());
}
/**
* Check if a link and all links up to the final target are in this directory
*
* @param Link $link
* @return bool
* @throws GetTargetException
* @throws IOException
*/
protected function isLinkInDirectory(Link $link): bool
{
$paths = [];
$current = $link;
do {
if (in_array($current->getPath(), $paths)) {
return false;
}
$paths[] = $current->getPath();
if (!$this->isPathInDirectory($current->getPath())) {
return false;
}
if (!$current instanceof Link) {
return true;
}
if (!$current->targetExists()) {
return $this->isPathInDirectory($current->getTargetPath());
}
$current = $current->getTarget();
} while (count($paths) < Link::DEPTH_LIMIT);
return false;
}
/**
* Check if a path is in this directory
*
* @param string $path
* @return bool
* @throws IOException
*/
protected function isPathInDirectory(string $path): bool
{
if (!str_ends_with($path, "/")) {
$path .= "/";
}
return str_starts_with($path, $this->getPath());
}
/**
* @inheritDoc
* @throws CreateDirectoryException
*/
public function create(): static
{
if (!@mkdir($this->path, recursive: true)) {
$error = error_get_last();
throw new CreateDirectoryException("Could not create directory (" . $this->path . ")" . ($error ? ": " . $error["message"] : ""), $this);
}
return $this;
}
/**
* @inheritDoc
* @throws DeleteException
* @throws MissingPermissionsException
* @throws GetTargetException
*/
public function delete(): static
{
if (!$this->exists()) {
return $this;
}
foreach ($this->getChildren() as $child) {
$child->delete();
}
if (!@rmdir($this->path)) {
$error = error_get_last();
throw new DeleteException("Could not delete directory (" . $this->path . ")" . ($error ? ": " . $error["message"] : ""), $this);
}
return $this;
}
}