|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace drupol\phpvfs\Node; |
|
6
|
|
|
|
|
7
|
|
|
use drupol\phpvfs\Utils\Path; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Directory. |
|
11
|
|
|
*/ |
|
12
|
|
|
class Directory extends FilesystemNode implements DirectoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @param string $id |
|
16
|
|
|
* |
|
17
|
|
|
* @throws \Exception |
|
18
|
|
|
* |
|
19
|
|
|
* @return \drupol\phpvfs\Node\DirectoryInterface |
|
20
|
|
|
*/ |
|
21
|
3 |
|
public function cd(string $id): DirectoryInterface |
|
22
|
|
|
{ |
|
23
|
3 |
|
if (!$this->exist($id)) { |
|
24
|
|
|
throw new \Exception(\sprintf('Cannot change directory to %s: No such file or directory.', $id)); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
3 |
|
if (($cwd = $this->get($id)) instanceof DirectoryInterface) { |
|
|
|
|
|
|
28
|
3 |
|
return $cwd; |
|
29
|
|
|
} |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $id |
|
34
|
|
|
* |
|
35
|
|
|
* @return null|\drupol\phpvfs\Node\DirectoryInterface|\drupol\phpvfs\Node\FileInterface|\drupol\phpvfs\Node\FilesystemNodeInterface |
|
36
|
|
|
*/ |
|
37
|
10 |
|
public function containsAttributeId(string $id): ?FilesystemNodeInterface |
|
38
|
|
|
{ |
|
39
|
|
|
/** @var \drupol\phpvfs\Node\FilesystemNodeInterface $child */ |
|
40
|
10 |
|
foreach ($this->children() as $child) { |
|
41
|
10 |
|
if ($child->getAttribute('id') === $id) { |
|
|
|
|
|
|
42
|
10 |
|
return $child; |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
3 |
|
return null; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $id |
|
51
|
|
|
* @param array $attributes |
|
52
|
|
|
* |
|
53
|
|
|
* @throws \Exception |
|
54
|
|
|
* |
|
55
|
|
|
* @return \drupol\phpvfs\Node\Directory |
|
56
|
|
|
*/ |
|
57
|
20 |
|
public static function create(string $id, array $attributes = []) |
|
58
|
|
|
{ |
|
59
|
20 |
|
$path = Path::fromString($id); |
|
60
|
|
|
|
|
61
|
20 |
|
if (\DIRECTORY_SEPARATOR !== $id && false !== \strpos($id, \DIRECTORY_SEPARATOR)) { |
|
62
|
18 |
|
if ($path->isAbsolute()) { |
|
63
|
16 |
|
$firstPart = \DIRECTORY_SEPARATOR; |
|
64
|
|
|
} else { |
|
65
|
4 |
|
$firstPart = $path->shift(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
18 |
|
$root = self::create($firstPart, $attributes); |
|
69
|
|
|
|
|
70
|
18 |
|
foreach ($path->getIterator() as $pathPart) { |
|
71
|
18 |
|
$child = new self(['id' => $pathPart]); |
|
72
|
18 |
|
$root->add($child); |
|
73
|
18 |
|
$root = $child; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
18 |
|
return $root; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
20 |
|
$attributes = ['id' => $id] + $attributes; |
|
80
|
|
|
|
|
81
|
20 |
|
return new self($attributes); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string ...$ids |
|
86
|
|
|
* |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
11 |
|
public function exist(string ...$ids): bool |
|
90
|
|
|
{ |
|
91
|
11 |
|
$exist = true; |
|
92
|
11 |
|
$existId = true; |
|
93
|
|
|
|
|
94
|
11 |
|
foreach ($ids as $id) { |
|
95
|
11 |
|
$path = Path::fromString($id); |
|
96
|
|
|
|
|
97
|
|
|
/** @var \drupol\phpvfs\Node\DirectoryInterface $cwd */ |
|
98
|
11 |
|
$cwd = $path->isAbsolute() ? |
|
99
|
9 |
|
$this->root() : |
|
100
|
11 |
|
$this; |
|
101
|
|
|
|
|
102
|
11 |
|
foreach ($path->getIterator() as $pathPart) { |
|
103
|
11 |
|
$pathPartExist = false; |
|
104
|
|
|
|
|
105
|
11 |
|
if (\DIRECTORY_SEPARATOR === $pathPart) { |
|
106
|
3 |
|
$pathPartExist = true; |
|
107
|
10 |
|
} elseif (null !== $child = $cwd->containsAttributeId($pathPart)) { |
|
108
|
10 |
|
$pathPartExist = true; |
|
109
|
10 |
|
$cwd = $child; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
11 |
|
$existId = $existId && $pathPartExist; |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
11 |
|
$exist = $exist && $existId; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
11 |
|
return $exist; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $id |
|
123
|
|
|
* |
|
124
|
|
|
* @return \drupol\phpvfs\Node\DirectoryInterface |
|
125
|
|
|
*/ |
|
126
|
11 |
|
public function get(string $id): FilesystemNodeInterface |
|
127
|
|
|
{ |
|
128
|
11 |
|
if (!$this->exist($id)) { |
|
129
|
|
|
throw new \Exception(\sprintf('Unable to get %s', $id)); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
11 |
|
$path = Path::fromString($id); |
|
133
|
|
|
|
|
134
|
11 |
|
if ((($root = $this->root()) instanceof DirectoryInterface) && $path->isRoot()) { |
|
135
|
3 |
|
return $root; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** @var \drupol\phpvfs\Node\DirectoryInterface $cwd */ |
|
139
|
10 |
|
$cwd = $path->isAbsolute() ? |
|
140
|
8 |
|
$this->root() : |
|
141
|
10 |
|
$this; |
|
142
|
|
|
|
|
143
|
10 |
|
foreach ($path->getIterator() as $pathPart) { |
|
144
|
10 |
|
$cwd = $cwd->containsAttributeId($pathPart); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
10 |
|
if (null === $cwd) { |
|
148
|
|
|
\xdebug_break(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
10 |
|
return $cwd; |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param string $id |
|
156
|
|
|
* |
|
157
|
|
|
* @throws \Exception |
|
158
|
|
|
* |
|
159
|
|
|
* @return \drupol\phptree\Node\NodeInterface|\drupol\phpvfs\Node\DirectoryInterface |
|
160
|
|
|
*/ |
|
161
|
2 |
|
public function mkdir(string $id) |
|
162
|
|
|
{ |
|
163
|
2 |
|
$dir = self::create($id); |
|
164
|
|
|
|
|
165
|
2 |
|
return $this->add($dir->root()); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param string $id |
|
170
|
|
|
* |
|
171
|
|
|
* @throws \Exception |
|
172
|
|
|
* |
|
173
|
|
|
* @return \drupol\phpvfs\Node\DirectoryInterface |
|
174
|
|
|
*/ |
|
175
|
3 |
|
public function rmdir(string $id): DirectoryInterface |
|
176
|
|
|
{ |
|
177
|
3 |
|
if (!$this->exist($id)) { |
|
178
|
|
|
throw new \Exception(\sprintf('Cannot remove %s: No such file or directory.', $id)); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
3 |
|
$path = Path::fromString($id); |
|
182
|
|
|
|
|
183
|
3 |
|
if ($path->isRoot()) { |
|
184
|
|
|
throw new \Exception(\sprintf('Cannot remove root directory.')); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
3 |
|
$cwd = $this->get($id); |
|
188
|
|
|
|
|
189
|
3 |
|
if (($cwd instanceof DirectoryInterface) && (null !== $parent = $cwd->getParent())) { |
|
190
|
2 |
|
$parent->remove($cwd); |
|
191
|
|
|
|
|
192
|
2 |
|
if (($parent = $cwd->getParent()) instanceof DirectoryInterface) { |
|
193
|
2 |
|
return $parent; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
1 |
|
return $this; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|