|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Flight Routing. |
|
7
|
|
|
* |
|
8
|
|
|
* PHP version 7.1 and above required |
|
9
|
|
|
* |
|
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
|
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
|
13
|
|
|
* |
|
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
15
|
|
|
* file that was distributed with this source code. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace Flight\Routing; |
|
19
|
|
|
|
|
20
|
|
|
use Flight\Routing\Exceptions\{MethodNotAllowedException, UriHandlerException, UrlGenerationException}; |
|
21
|
|
|
use Flight\Routing\Interfaces\{RouteCompilerInterface, RouteMatcherInterface}; |
|
22
|
|
|
use Psr\Http\Message\{ServerRequestInterface, UriInterface}; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The bidirectional route matcher responsible for matching |
|
26
|
|
|
* HTTP request and generating url from routes. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class RouteMatcher implements RouteMatcherInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var iterable<int,Route> */ |
|
33
|
|
|
protected $routes = []; |
|
34
|
|
|
|
|
35
|
|
|
/** @var array<string,array<string,mixed>> */ |
|
36
|
|
|
protected $staticRouteMap = []; |
|
37
|
|
|
|
|
38
|
|
|
/** @var array<int,array> */ |
|
39
|
|
|
protected $variableRouteData = []; |
|
40
|
|
|
|
|
41
|
|
|
/** @var string|null */ |
|
42
|
|
|
protected $regexToRoutesMap = null; |
|
43
|
|
|
|
|
44
|
|
|
/** @var DebugRoute|null */ |
|
45
|
|
|
protected $debug; |
|
46
|
|
|
|
|
47
|
|
|
/** @var RouteCompilerInterface */ |
|
48
|
|
|
private $compiler; |
|
49
|
|
|
|
|
50
|
|
|
public function __construct(RouteCollection $collection) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->compiler = $collection->getCompiler(); |
|
53
|
|
|
$this->routes = $collection->getIterator(); |
|
54
|
|
|
|
|
55
|
|
|
// Load the route maps from $collection. |
|
56
|
|
|
[$this->staticRouteMap, $dynamicRouteMap, $this->variableRouteData] = $collection->getRouteMaps(); |
|
57
|
|
|
|
|
58
|
|
|
if (!empty($dynamicRouteMap)) { |
|
59
|
|
|
$this->regexToRoutesMap = '~^(?|' . \implode('|', $dynamicRouteMap) . ')$~Ju'; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// Enable routes profiling ... |
|
63
|
|
|
$this->debug = $collection->getDebugRoute(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function matchRequest(ServerRequestInterface $request): ?Route |
|
70
|
|
|
{ |
|
71
|
|
|
$requestUri = $request->getUri(); |
|
72
|
|
|
|
|
73
|
|
|
// Resolve request path to match sub-directory or /index.php/path |
|
74
|
|
|
if (!empty($pathInfo = $request->getServerParams()['PATH_INFO'] ?? '')) { |
|
75
|
|
|
$requestUri = $requestUri->withPath($pathInfo); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this->match($request->getMethod(), $requestUri); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function match(string $method, UriInterface $uri): ?Route |
|
85
|
|
|
{ |
|
86
|
|
|
$requestPath = $uri->getPath(); |
|
87
|
|
|
|
|
88
|
|
|
if ('/' !== $requestPath && isset(Route::URL_PREFIX_SLASHES[$requestPath[-1]])) { |
|
89
|
|
|
$uri = $uri->withPath($requestPath = \substr($requestPath, 0, -1)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (isset($this->staticRouteMap[$requestPath])) { |
|
93
|
|
|
$staticRoute = $this->staticRouteMap[$requestPath]; |
|
94
|
|
|
|
|
95
|
|
|
if (!isset($staticRoute[$method])) { |
|
96
|
|
|
throw new MethodNotAllowedException(\array_keys($staticRoute), $uri->getPath(), $method); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if (!empty($hostsRegex = $staticRoute[$method][0])) { |
|
100
|
|
|
$hostAndPost = $uri->getHost() . (null !== $uri->getPort() ? ':' . $uri->getPort() : ''); |
|
101
|
|
|
|
|
102
|
|
|
if (!\preg_match($hostsRegex, $hostAndPost, $hostsVar)) { |
|
103
|
|
|
if (isset($this->regexToRoutesMap)) { |
|
104
|
|
|
goto retry_routing; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
throw new UriHandlerException(\sprintf('Unfortunately current host "%s" is not allowed on requested static path [%s].', $uri->getHost(), $uri->getPath()), 400); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$route = $this->routes[$routeId = $staticRoute[$method][1]]; |
|
112
|
|
|
|
|
113
|
|
|
return $this->matchRoute($route, $uri, \array_merge($this->variableRouteData[$routeId], $hostsVar ?? [])); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
retry_routing: |
|
117
|
|
|
if (isset($this->regexToRoutesMap) && \preg_match($this->regexToRoutesMap, $method . \strpbrk((string) $uri, '/'), $matches)) { |
|
|
|
|
|
|
118
|
|
|
$route = $this->routes[$routeId = $matches['MARK']]; |
|
119
|
|
|
|
|
120
|
|
|
if (!empty($matches[1])) { |
|
121
|
|
|
throw new MethodNotAllowedException($route->get('methods'), $uri->getPath(), $method); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
unset($matches[0], $matches[1], $matches['MARK']); |
|
125
|
|
|
|
|
126
|
|
|
return $this->matchRoute($route, $uri, $this->variableRouteData[$routeId], $matches); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return null; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritdoc} |
|
134
|
|
|
*/ |
|
135
|
|
|
public function generateUri(string $routeName, array $parameters = []): GeneratedUri |
|
136
|
|
|
{ |
|
137
|
|
|
foreach ($this->routes as $route) { |
|
138
|
|
|
if ($routeName === $route->get('name')) { |
|
139
|
|
|
$defaults = $route->get('defaults'); |
|
140
|
|
|
unset($defaults['_arguments']); |
|
141
|
|
|
|
|
142
|
|
|
return $this->compiler->generateUri($route, $parameters, $defaults); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
throw new UrlGenerationException(\sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $routeName), 404); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function getCompiler(): RouteCompilerInterface |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->compiler; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Get the profiled routes. |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getProfile(): ?DebugRoute |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->debug; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
protected function matchRoute(Route $route, UriInterface $uri, array $variables, array $allowed = []): Route |
|
163
|
|
|
{ |
|
164
|
|
|
$schemes = $route->get('schemes'); |
|
165
|
|
|
|
|
166
|
|
|
if (!empty($schemes) && !\array_key_exists($uri->getScheme(), $schemes)) { |
|
167
|
|
|
throw new UriHandlerException(\sprintf('Unfortunately current scheme "%s" is not allowed on requested uri [%s].', $uri->getScheme(), $uri->getPath()), 400); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$matchVar = 2; |
|
171
|
|
|
|
|
172
|
|
|
foreach ($variables as $key => $value) { |
|
173
|
|
|
$route->argument($key, $allowed[$matchVar] ?? $value); |
|
174
|
|
|
|
|
175
|
|
|
++$matchVar; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
if (null !== $this->debug) { |
|
179
|
|
|
$this->debug->setMatched($route->get('name')); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $route; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|