-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouteTable.php
More file actions
151 lines (126 loc) · 4.27 KB
/
Copy pathRouteTable.php
File metadata and controls
151 lines (126 loc) · 4.27 KB
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
<?php
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
/**
* Italix Routing - RouteTable
*
* @package Italix\Routing
*/
declare(strict_types=1);
namespace Italix\Routing;
/**
* Named routes: name => [method, pattern].
*
* Deliberately holds no handlers. The table exists to answer one question —
* "what does the URL for this name look like?" — and keeping the handlers out
* means it can be built by replaying the route definitions without loading a
* single controller class.
*
* That property is what makes named routes survive **route caching**. FastRoute
* does not run the definition closure when the compiled dispatcher is cached,
* so a table populated as a side effect of dispatching would be empty on every
* cached request — and `$url->to()` would throw on production and work in
* development, which is the worst possible failure schedule.
*/
final class RouteTable
{
/** @var array<string, array{method: string, pattern: string}> */
private array $routes = [];
/**
* Build a table by replaying route definitions with no dispatcher attached.
*
* @param callable(Registrar):void $definition
*/
public static function record(callable $definition): self
{
$registrar = Registrar::recording();
$definition($registrar);
return $registrar->table();
}
/**
* @param string|string[] $method
*/
public function add(string $name_c, string $pattern, $method = 'GET'): self
{
if ($name_c === '') {
throw new UrlException("A named route cannot have an empty name (pattern \"{$pattern}\").");
}
$method = is_array($method) ? (string) ($method[0] ?? 'GET') : (string) $method;
if (isset($this->routes[$name_c]) && $this->routes[$name_c]['pattern'] !== $pattern) {
throw new UrlException(sprintf(
'Route name "%s" is used twice, for "%s" and "%s". Names must be unique.',
$name_c,
$this->routes[$name_c]['pattern'],
$pattern
));
}
// The same name for GET and POST of one form is normal and intended:
// the pattern is identical, so the first registration wins and the
// second is a no-op rather than a conflict.
$this->routes[$name_c] = ['method' => strtoupper($method), 'pattern' => $pattern];
return $this;
}
public function has(string $name_c): bool
{
return isset($this->routes[$name_c]);
}
public function pattern(string $name_c): string
{
if (!isset($this->routes[$name_c])) {
throw new UrlException($this->unknown_message($name_c));
}
return $this->routes[$name_c]['pattern'];
}
public function method_code(string $name_c): string
{
if (!isset($this->routes[$name_c])) {
throw new UrlException($this->unknown_message($name_c));
}
return $this->routes[$name_c]['method'];
}
/**
* @return string[]
*/
public function names(): array
{
$names = array_keys($this->routes);
sort($names);
return $names;
}
/**
* @return array<string, array{method: string, pattern: string}>
*/
public function all(): array
{
$routes = $this->routes;
ksort($routes);
return $routes;
}
public function count(): int
{
return count($this->routes);
}
/**
* An unknown name is almost always a typo or a rename, so say what exists
* nearby rather than only what does not.
*/
private function unknown_message(string $name_c): string
{
$near = [];
foreach ($this->names() as $candidate) {
if (strncmp($candidate, $name_c, max(1, strrpos($name_c, '.') ?: strlen($name_c))) === 0
|| levenshtein($name_c, $candidate) <= 3
) {
$near[] = $candidate;
}
}
$message = "No route is named \"{$name_c}\".";
if ($near !== []) {
$message .= ' Did you mean: ' . implode(', ', array_slice($near, 0, 5)) . '?';
}
return $message;
}
}