-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathread_symbolic_link.php
44 lines (36 loc) · 1.18 KB
/
read_symbolic_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
<?php
declare(strict_types=1);
namespace Psl\Filesystem;
use Psl;
use Psl\Internal;
use Psl\Str;
use function readlink;
/**
* Returns the target of a symbolic link.
*
* @throws Psl\Exception\InvariantViolationException If the link specified by
* $symbolic_link does not exist, or is not a symbolic link.
* @throws Exception\RuntimeException If unable to retrieve the target
* of $symbolic_link.
*/
function read_symbolic_link(string $symbolic_link): string
{
Psl\invariant(exists($symbolic_link), '$symbolic_link does not exists.');
Psl\invariant(is_symbolic_link($symbolic_link), '$symbolic_link is not a symbolic link.');
[$result, $message] = Internal\box(
/**
* @return false|string
*/
static fn() => readlink($symbolic_link)
);
// @codeCoverageIgnoreStart
if (false === $result) {
throw new Exception\RuntimeException(Str\format(
'Failed to retrieve the target of symbolic link "%s": %s',
$symbolic_link,
$message ?? 'internal error'
));
}
// @codeCoverageIgnoreEnd
return $result;
}