-
Notifications
You must be signed in to change notification settings - Fork 0
/
Link.php
172 lines (152 loc) · 4.43 KB
/
Link.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace Aternos\IO\System\Link;
use Aternos\IO\Exception\DeleteException;
use Aternos\IO\Exception\GetTargetException;
use Aternos\IO\Exception\IOException;
use Aternos\IO\Exception\SetTargetException;
use Aternos\IO\Interfaces\Features\GetPathInterface;
use Aternos\IO\Interfaces\Features\GetTargetPathInterface;
use Aternos\IO\Interfaces\IOElementInterface;
use Aternos\IO\Interfaces\Types\Link\LinkInterface;
use Aternos\IO\System\FilesystemElement;
/**
* Class Link
*
* General filesystem link
*
* @package Aternos\IO\System\Link
*/
class Link extends FilesystemElement implements LinkInterface, GetTargetPathInterface
{
public const int DEPTH_LIMIT = 40;
protected ?IOElementInterface $target = null;
protected ?bool $existsOverride = null;
/**
* @inheritDoc
* @throws DeleteException
*/
public function delete(): static
{
if (!$this->exists()) {
return $this;
}
if (!@unlink($this->path)) {
throw new DeleteException("Could not delete link (" . $this->path . ")", $this);
}
return $this;
}
/**
* @inheritDoc
* @throws GetTargetException
*/
public function getTarget(): IOElementInterface
{
if ($this->target) {
return $this->target;
}
$targetPath = $this->getTargetPath();
if (!$this->targetExists()) {
throw new GetTargetException("Could not get link target because target does not exist (" . $targetPath . ")", $this);
}
return $this->target = static::getIOElementFromPath($targetPath);
}
/**
* @inheritDoc
* @throws DeleteException
* @throws SetTargetException
*/
public function setTarget(GetPathInterface $target): static
{
if ($this->exists()) {
$this->delete();
}
if (!@symlink($target->getPath(), $this->path)) {
throw new SetTargetException("Could not set link target (" . $this->path . " -> " . $target->getPath() . ")", $this);
}
$this->target = $target;
return $this;
}
/**
* @inheritDoc
*/
public function exists(): bool
{
if ($this->existsOverride !== null) {
return $this->existsOverride;
}
return is_link($this->path);
}
/**
* @inheritDoc
* @throws GetTargetException
*/
public function getTargetPath(): string
{
if (!$this->exists()) {
throw new GetTargetException("Could not get link target because link does not exist (" . $this->path . ")", $this);
}
return readlink($this->path);
}
/**
* @inheritDoc
* @throws GetTargetException
*/
public function targetExists(): bool
{
$targetPath = $this->getTargetPath();
return file_exists($targetPath) || is_link($targetPath);
}
/**
* @inheritDoc
* @throws GetTargetException
*/
public function getFinalTarget(): IOElementInterface
{
return $this->getFinalLink()->getTarget();
}
/**
* Get the last link in the chain, detecting loops and too many levels of links
*
* @return LinkInterface
* @throws GetTargetException
* @throws IOException
*/
protected function getFinalLink(): LinkInterface
{
$paths = [];
$current = $this;
do {
if (in_array($current->getPath(), $paths)) {
throw new GetTargetException("Could not get link target because of infinite link loop (" . $this->getPath() . ")", $this);
}
$paths[] = $current->getPath();
if (!$current->targetExists()) {
return $current;
}
$target = $current->getTarget();
if (!$target instanceof LinkInterface) {
return $current;
}
$current = $target;
} while (count($paths) < static::DEPTH_LIMIT);
throw new GetTargetException("Could not get link target because of too many levels of links (" . $this->getPath() . ")", $this);
}
/**
* @inheritDoc
* @throws GetTargetException
*/
public function getFinalTargetPath(): string
{
return $this->getFinalLink()->getTargetPath();
}
/**
* @return array|string[]
*/
public function __serialize(): array
{
return [
...parent::__serialize(),
"target" => $this->target
];
}
}