-
Notifications
You must be signed in to change notification settings - Fork 0
/
Directory.php
184 lines (167 loc) · 5.27 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
<?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\Types\DirectoryInterface;
use Aternos\IO\System\FilesystemElement;
use Aternos\IO\System\Link\Link;
use DirectoryIterator;
use Generator;
/**
* 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
* @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;
}
}