1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace drupol\phpvfs\Node; |
6
|
|
|
|
7
|
|
|
use drupol\phptree\Node\AttributeNode; |
8
|
|
|
use drupol\phptree\Node\NodeInterface; |
9
|
|
|
use drupol\phpvfs\Utils\Path; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Vfs. |
13
|
|
|
*/ |
14
|
|
|
abstract class FilesystemNode extends AttributeNode implements FilesystemNodeInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
* |
19
|
|
|
* @throws \Exception |
20
|
|
|
* |
21
|
|
|
* @return \drupol\phpvfs\Node\DirectoryInterface |
22
|
|
|
*/ |
23
|
15 |
|
public function add(NodeInterface ...$nodes): NodeInterface |
24
|
|
|
{ |
25
|
|
|
/** @var \drupol\phpvfs\Node\FilesystemNodeInterface $node */ |
26
|
15 |
|
foreach ($nodes as $node) { |
27
|
15 |
|
$node = $node->root(); |
|
|
|
|
28
|
|
|
|
29
|
15 |
|
if (!($node instanceof FilesystemNodeInterface)) { |
30
|
|
|
throw new \Exception('Invalid filesystem node type.'); |
31
|
|
|
} |
32
|
|
|
|
33
|
15 |
|
if ($this->getAttribute('id') === $node->getAttribute('id')) { |
34
|
7 |
|
$this->add($node[0]->setParent(null)); |
35
|
|
|
|
36
|
7 |
|
continue; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// If the $cwd contains the nodechild. |
40
|
15 |
|
if (null !== $child = $this->contains($node)) { |
41
|
2 |
|
$child->add($node[0]->setParent(null)); |
42
|
|
|
|
43
|
2 |
|
continue; |
44
|
|
|
} |
45
|
|
|
|
46
|
15 |
|
parent::add($node->setParent(null)); |
47
|
|
|
} |
48
|
|
|
|
49
|
15 |
|
return $this; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
1 |
|
public function getPath(): Path |
56
|
|
|
{ |
57
|
|
|
$paths = [ |
58
|
1 |
|
$this->getAttribute('id'), |
59
|
|
|
]; |
60
|
|
|
|
61
|
1 |
|
foreach ($this->getAncestors() as $ancestor) { |
62
|
1 |
|
\array_unshift($paths, $ancestor->getAttribute('id')); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
return Path::fromString(\str_replace('//', '/', \implode('/', $paths))); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
16 |
|
public function root(): FilesystemNodeInterface |
72
|
|
|
{ |
73
|
16 |
|
$root = $this; |
74
|
|
|
|
75
|
16 |
|
foreach ($this->getAncestors() as $ancestor) { |
76
|
13 |
|
$root = $ancestor; |
77
|
|
|
} |
78
|
|
|
|
79
|
16 |
|
return $root; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param \drupol\phpvfs\Node\FilesystemNodeInterface $node |
84
|
|
|
* |
85
|
|
|
* @return null|\drupol\phpvfs\Node\FilesystemNodeInterface |
86
|
|
|
*/ |
87
|
15 |
|
protected function contains(FilesystemNodeInterface $node): ?FilesystemNodeInterface |
88
|
|
|
{ |
89
|
|
|
/** @var \drupol\phpvfs\Node\FilesystemNodeInterface $child */ |
90
|
15 |
|
foreach ($this->children() as $child) { |
91
|
3 |
|
if ($node->getAttribute('id') === $child->getAttribute('id')) { |
|
|
|
|
92
|
3 |
|
return $child; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
15 |
|
return null; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|