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