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