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