-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathcreate_symbolic_link.php
55 lines (47 loc) · 1.65 KB
/
create_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
45
46
47
48
49
50
51
52
53
54
55
<?php
declare(strict_types=1);
namespace Psl\Filesystem;
use Psl\Internal;
use Psl\Str;
use function symlink;
/**
* Create a symbolic link for $source.
*
* @param non-empty-string $source The file to create a symbolic link for.
* @param non-empty-string $destination
*
* @throws Exception\RuntimeException If unable to create the symbolic link.
* @throws Exception\NotFoundException If $source is not found.
* @throws Exception\NotReadableException If $destination is a non-empty directory, and is non-readable {@see delete_directory()}.
*/
function create_symbolic_link(string $source, string $destination): void
{
if (!namespace\exists($source)) {
throw Exception\NotFoundException::forNode($source);
}
namespace\create_directory_for_file($destination);
try {
if (namespace\read_symbolic_link($destination) === $source) {
return;
}
} catch (Exception\NotSymbolicLinkException) {
try {
namespace\delete_directory($destination, true);
} catch (Exception\NotDirectoryException) {
/** @psalm-suppress MissingThrowsDocblock - $destination is a file. */
namespace\delete_file($destination);
}
} catch (Exception\NotFoundException) {
}
[$result, $error_message] = Internal\box(static fn() => symlink($source, $destination));
// @codeCoverageIgnoreStart
if (false === $result) {
throw new Exception\RuntimeException(Str\format(
'Failed to create symbolic link "%s" from "%s": %s.',
$destination,
$source,
$error_message ?? 'internal error',
));
}
// @codeCoverageIgnoreEnd
}