|
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 Vfs extends AttributeNode implements VfsInterface |
|
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\VfsInterface $node */ |
|
26
|
15 |
|
foreach ($nodes as $node) { |
|
27
|
15 |
|
if ($this->getAttribute('id') === $node->getAttribute('id')) { |
|
|
|
|
|
|
28
|
5 |
|
$this->add($node[0]->setParent(null)); |
|
29
|
|
|
|
|
30
|
5 |
|
continue; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
// If the $cwd contains the nodechild. |
|
34
|
15 |
|
if (null !== $child = $this->contains($node)) { |
|
35
|
|
|
$child->add($node[0]->setParent(null)); |
|
36
|
|
|
|
|
37
|
|
|
continue; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
15 |
|
parent::add($node->setParent(null)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
15 |
|
return $this; |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
1 |
|
public function getPath(): Path |
|
50
|
|
|
{ |
|
51
|
|
|
$paths = [ |
|
52
|
1 |
|
$this->getAttribute('id'), |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
1 |
|
foreach ($this->getAncestors() as $ancestor) { |
|
56
|
1 |
|
\array_unshift($paths, $ancestor->getAttribute('id')); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
return Path::fromString(\str_replace('//', '/', \implode('/', $paths))); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
14 |
|
public function root(): VfsInterface |
|
66
|
|
|
{ |
|
67
|
14 |
|
$root = $this; |
|
68
|
|
|
|
|
69
|
14 |
|
foreach ($this->getAncestors() as $ancestor) { |
|
70
|
13 |
|
$root = $ancestor; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
14 |
|
return $root; |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param \drupol\phpvfs\Node\VfsInterface $node |
|
78
|
|
|
* |
|
79
|
|
|
* @return null|\drupol\phpvfs\Node\VfsInterface |
|
80
|
|
|
*/ |
|
81
|
15 |
|
protected function contains(VfsInterface $node): ?VfsInterface |
|
82
|
|
|
{ |
|
83
|
|
|
/** @var \drupol\phpvfs\Node\VfsInterface $child */ |
|
84
|
15 |
|
foreach ($this->children() as $child) { |
|
85
|
3 |
|
if ($node->getAttribute('id') === $child->getAttribute('id')) { |
|
|
|
|
|
|
86
|
|
|
return $child; |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
15 |
|
return null; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|