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
|
|
|
* FilesystemNode constructor. |
18
|
|
|
* |
19
|
|
|
* @param array $attributes |
20
|
|
|
* @param null|int $capacity |
21
|
|
|
*/ |
22
|
19 |
|
public function __construct( |
23
|
|
|
array $attributes = [], |
24
|
|
|
?int $capacity = 0 |
25
|
|
|
) { |
26
|
19 |
|
$time = \time(); |
27
|
|
|
|
28
|
|
|
$attributes = [ |
29
|
19 |
|
'uid' => \function_exists('posix_getuid') ? \posix_getuid() : 0, |
30
|
19 |
|
'gid' => \function_exists('posix_getgid') ? \posix_getgid() : 0, |
31
|
19 |
|
'atime' => $time, |
32
|
19 |
|
'mtime' => $time, |
33
|
19 |
|
'ctime' => $time, |
34
|
19 |
|
] + $attributes; |
35
|
|
|
|
36
|
19 |
|
parent::__construct($attributes, $capacity); |
37
|
19 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
* |
42
|
|
|
* @throws \Exception |
43
|
|
|
* |
44
|
|
|
* @return \drupol\phpvfs\Node\DirectoryInterface |
45
|
|
|
*/ |
46
|
15 |
|
public function add(NodeInterface ...$nodes): NodeInterface |
47
|
|
|
{ |
48
|
|
|
/** @var \drupol\phpvfs\Node\FilesystemNodeInterface $node */ |
49
|
15 |
|
foreach ($nodes as $node) { |
50
|
15 |
|
$node = $node->root(); |
|
|
|
|
51
|
|
|
|
52
|
15 |
|
if (!($node instanceof FilesystemNodeInterface)) { |
53
|
|
|
throw new \Exception('Invalid filesystem node type.'); |
54
|
|
|
} |
55
|
|
|
|
56
|
15 |
|
if ($this->getAttribute('id') === $node->getAttribute('id')) { |
57
|
7 |
|
$this->add($node[0]->setParent(null)); |
58
|
|
|
|
59
|
7 |
|
continue; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// If the $cwd contains the nodechild. |
63
|
15 |
|
if (null !== $child = $this->contains($node)) { |
64
|
2 |
|
$child->add($node[0]->setParent(null)); |
65
|
|
|
|
66
|
2 |
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
15 |
|
parent::add($node->setParent(null)); |
70
|
|
|
} |
71
|
|
|
|
72
|
15 |
|
return $this; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
1 |
|
public function getPath(): Path |
79
|
|
|
{ |
80
|
|
|
$paths = [ |
81
|
1 |
|
$this->getAttribute('id'), |
82
|
|
|
]; |
83
|
|
|
|
84
|
1 |
|
foreach ($this->getAncestors() as $ancestor) { |
85
|
1 |
|
\array_unshift($paths, $ancestor->getAttribute('id')); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
return Path::fromString(\str_replace('//', '/', \implode('/', $paths))); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
16 |
|
public function root(): FilesystemNodeInterface |
95
|
|
|
{ |
96
|
16 |
|
$root = $this; |
97
|
|
|
|
98
|
16 |
|
foreach ($this->getAncestors() as $ancestor) { |
99
|
13 |
|
$root = $ancestor; |
100
|
|
|
} |
101
|
|
|
|
102
|
16 |
|
return $root; |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param \drupol\phpvfs\Node\FilesystemNodeInterface $node |
107
|
|
|
* |
108
|
|
|
* @return null|\drupol\phpvfs\Node\FilesystemNodeInterface |
109
|
|
|
*/ |
110
|
15 |
|
protected function contains(FilesystemNodeInterface $node): ?FilesystemNodeInterface |
111
|
|
|
{ |
112
|
|
|
/** @var \drupol\phpvfs\Node\FilesystemNodeInterface $child */ |
113
|
15 |
|
foreach ($this->children() as $child) { |
114
|
3 |
|
if ($node->getAttribute('id') === $child->getAttribute('id')) { |
|
|
|
|
115
|
2 |
|
return $child; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
15 |
|
return null; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|