-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAlias.php
102 lines (82 loc) · 2.89 KB
/
Alias.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
<?php
declare(strict_types=1);
namespace ManaPHP;
use JsonSerializable;
use ManaPHP\Di\Attribute\Autowired;
use ManaPHP\Exception\InvalidArgumentException;
use ManaPHP\Exception\MisuseException;
class Alias implements AliasInterface, JsonSerializable
{
#[Autowired] protected array $aliases = ['@manaphp' => __DIR__];
public function all(): array
{
return $this->aliases;
}
public function set(string $name, string $path): string
{
if ($name[0] !== '@') {
throw new MisuseException(['`{name}` must start with `@`', 'name' => $name]);
}
if ($path === '') {
$this->aliases[$name] = $path;
} elseif ($path[0] !== '@') {
if (DIRECTORY_SEPARATOR === '/' || str_starts_with($name, '@ns.')) {
$this->aliases[$name] = $path;
} else {
$this->aliases[$name] = strtr($path, '\\', '/');
}
} else {
$this->aliases[$name] = $this->resolve($path);
}
return $this->aliases[$name];
}
public function get(string $name): ?string
{
if ($name[0] !== '@') {
throw new MisuseException(['`{name}` must start with `@`', 'name' => $name]);
}
return $this->aliases[$name] ?? null;
}
public function has(string $name): bool
{
if ($name[0] !== '@') {
throw new MisuseException(['`{name}` must start with `@`', 'name' => $name]);
}
return isset($this->aliases[$name]);
}
public function resolve(string $path): string
{
if ($path[0] !== '@') {
return DIRECTORY_SEPARATOR === '/' ? $path : strtr($path, '\\', '/');
}
if (str_contains($path, '{') && preg_match_all('#{([^}]+)}#', $path, $matches)) {
foreach ((array)$matches[1] as $k => $match) {
if (is_numeric($match)) {
$replaced = substr(bin2hex(random_bytes($match / 2 + 1)), 0, (int)$match);
} else {
$ts = $ts ?? time();
$replaced = date($match, $ts);
}
$path = str_replace($matches[0][$k], $replaced, $path);
}
}
if (DIRECTORY_SEPARATOR === '\\') {
$path = strtr($path, '\\', '/');
}
if (($pos = strpos($path, '/')) === false) {
if (!isset($this->aliases[$path])) {
throw new InvalidArgumentException(['`{alias}` is not exists', 'alias' => $path]);
}
return $this->aliases[$path];
}
$alias = substr($path, 0, $pos);
if (!isset($this->aliases[$alias])) {
throw new InvalidArgumentException(['`{1}` is not exists for `{2}`', $alias, $path]);
}
return $this->aliases[$alias] . substr($path, $pos);
}
public function jsonSerialize(): array
{
return $this->all();
}
}