1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Flight Routing. |
5
|
|
|
* |
6
|
|
|
* PHP version 8.0 and above required |
7
|
|
|
* |
8
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
9
|
|
|
* @copyright 2019 Divine Niiquaye Ibok (https://divinenii.com/) |
10
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please view the LICENSE |
13
|
|
|
* file that was distributed with this source code. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Flight\Routing\Traits; |
17
|
|
|
|
18
|
|
|
use Flight\Routing\Exceptions\{MethodNotAllowedException, UriHandlerException}; |
19
|
|
|
use Psr\Http\Message\UriInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The default implementation for route match. |
23
|
|
|
* |
24
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
trait ResolverTrait |
27
|
|
|
{ |
28
|
|
|
/** @var array<int|string,mixed> */ |
29
|
|
|
private array $optimized = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array<string,mixed> $route |
33
|
|
|
* @param array<int,array<string,bool|string>> $errors |
34
|
|
|
*/ |
35
|
58 |
|
protected function assertRoute(string $method, UriInterface $uri, array &$route, array &$errors): bool |
36
|
|
|
{ |
37
|
58 |
|
$matched = true; |
38
|
|
|
|
39
|
58 |
|
if (!\array_key_exists($method, $route['methods'] ?? [])) { |
40
|
7 |
|
$errors[0] += $route['methods'] ?? []; |
41
|
7 |
|
$matched = false; |
42
|
57 |
|
} elseif (isset($route['hosts']) && !\array_key_exists($errors[2] ??= \rtrim($uri->getHost().':'.$uri->getPort(), ':'), $route['hosts'])) { |
|
|
|
|
43
|
2 |
|
$hosts = \array_keys($route['hosts'], true, true); |
44
|
2 |
|
[$hostsRegex, $hostVar] = $this->compiler->compile(\implode('|', $hosts), $route['placeholders'] ?? []); |
45
|
|
|
|
46
|
2 |
|
if ($matched = 1 === \preg_match($hostsRegex.'i', $errors[2], $matches, \PREG_UNMATCHED_AS_NULL)) { |
|
|
|
|
47
|
2 |
|
foreach ($hostVar as $key => $value) { |
48
|
1 |
|
$route['arguments'][$key] = $matches[$key] ?: $route['defaults'][$key] ?? $value; |
49
|
|
|
} |
50
|
|
|
} |
51
|
55 |
|
} elseif (isset($route['schemes']) && !\array_key_exists($uri->getScheme(), $route['schemes'])) { |
52
|
1 |
|
$errors[1] += $route['schemes'] ?? []; |
53
|
1 |
|
$matched = false; |
54
|
|
|
} |
55
|
|
|
|
56
|
58 |
|
return $matched; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return null|array<string,mixed> |
61
|
|
|
*/ |
62
|
72 |
|
protected function resolveRoute(string $path, string $method, UriInterface $uri): ?array |
63
|
|
|
{ |
64
|
72 |
|
$errors = [[], []]; |
65
|
|
|
|
66
|
72 |
|
foreach ($this->getCollection()->getRoutes() as $i => $r) { |
|
|
|
|
67
|
71 |
|
if (isset($r['prefix']) && !\str_starts_with($path, '/'.\ltrim($r['prefix'], '/'))) { |
68
|
11 |
|
continue; |
69
|
|
|
} |
70
|
62 |
|
[$p, $v] = $this->optimized[$i] ??= $this->compiler->compile('/'.\ltrim($r['path'], '/'), $r['placeholders'] ?? []); |
71
|
|
|
|
72
|
62 |
|
if (!\preg_match($p, $path, $m, \PREG_UNMATCHED_AS_NULL) || !$this->assertRoute($method, $uri, $r, $errors)) { |
73
|
13 |
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
51 |
|
foreach ($v as $key => $value) { |
77
|
27 |
|
$r['arguments'][$key] = $m[$key] ?? $r['defaults'][$key] ?? $value; |
78
|
|
|
} |
79
|
|
|
|
80
|
51 |
|
return $r; |
81
|
|
|
} |
82
|
|
|
|
83
|
23 |
|
return $this->resolveError($errors, $method, $uri); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return null|array<string,mixed> |
88
|
|
|
*/ |
89
|
4 |
|
protected function resolveCache(string $path, string $method, UriInterface $uri): ?array |
90
|
|
|
{ |
91
|
4 |
|
$errors = [[], []]; |
92
|
4 |
|
$routes = $this->optimized[2] ?? $this->doCache(); |
|
|
|
|
93
|
|
|
|
94
|
4 |
|
if (!$matched = $this->optimized[0][$path] ?? $this->optimized[1][0]($path)) { |
95
|
4 |
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
4 |
|
foreach ($matched as $match) { |
99
|
4 |
|
$r = $routes[$o = \intval($match['MARK'] ?? $match)] ?? $routes->getRoutes()[$o]; |
100
|
|
|
|
101
|
4 |
|
if (!$this->assertRoute($method, $uri, $r, $errors)) { |
102
|
4 |
|
continue; |
103
|
|
|
} |
104
|
|
|
|
105
|
4 |
|
if (isset($match['MARK'])) { |
106
|
4 |
|
$i = 0; |
107
|
|
|
|
108
|
4 |
|
foreach ($this->optimized[1][1][$o] ?? [] as $key => $value) { |
109
|
4 |
|
$r['arguments'][$key] = $match[++$i] ?: $r['defaults'][$key] ?? $value; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
4 |
|
return $r; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->resolveError($errors, $method, $uri); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param array<int,array<string,bool|string>> $errors |
121
|
|
|
*/ |
122
|
23 |
|
protected function resolveError(array $errors, string $method, UriInterface $uri) |
123
|
|
|
{ |
124
|
23 |
|
if (!empty($errors[0])) { |
125
|
1 |
|
throw new MethodNotAllowedException(\array_keys($errors[0]), $uri->getPath(), $method); |
126
|
|
|
} |
127
|
|
|
|
128
|
22 |
|
if (!empty($errors[1])) { |
129
|
1 |
|
throw new UriHandlerException( |
130
|
1 |
|
\sprintf( |
131
|
|
|
'Route with "%s" path requires request scheme(s) [%s], "%s" is invalid.', |
132
|
1 |
|
$uri->getPath(), |
133
|
1 |
|
\implode(', ', \array_keys($errors[1])), |
134
|
1 |
|
$uri->getScheme(), |
135
|
|
|
), |
136
|
|
|
400 |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
21 |
|
return null; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|