-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Router.php
284 lines (261 loc) · 10.6 KB
/
Router.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
namespace Neos\Flow\Mvc\Routing;
/*
* This file is part of the Neos.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Configuration\ConfigurationManager;
use Neos\Flow\Http\Helper\RequestInformationHelper;
use Neos\Flow\Http\Helper\UriHelper;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Flow\Mvc\Exception\InvalidRoutePartValueException;
use Neos\Flow\Mvc\Exception\InvalidRouteSetupException;
use Neos\Flow\Mvc\Exception\NoMatchingRouteException;
use Neos\Flow\Mvc\Routing\Dto\ResolveContext;
use Neos\Flow\Mvc\Routing\Dto\RouteContext;
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface;
/**
* The default web router
*
* @Flow\Scope("singleton")
* @api
*/
class Router implements RouterInterface
{
/**
* @Flow\Inject(name="Neos.Flow:SystemLogger")
* @var LoggerInterface
*/
protected $logger;
/**
* @Flow\Inject
* @var ConfigurationManager
*/
protected $configurationManager;
/**
* @Flow\Inject
* @var RouterCachingService
*/
protected $routerCachingService;
/**
* Array containing the configuration for all routes
*
* @var array
*/
protected $routesConfiguration = null;
/**
* Array of routes to match against
*
* @var array
*/
protected $routes = [];
/**
* true if route object have been created, otherwise false
*
* @var boolean
*/
protected $routesCreated = false;
/**
* @var Route
*/
protected $lastMatchedRoute;
/**
* @var Route
*/
protected $lastResolvedRoute;
/**
* Injects the (system) logger based on PSR-3.
*
* @param LoggerInterface $logger
* @return void
*/
public function injectLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* Sets the routes configuration.
*
* @param array $routesConfiguration The routes configuration or NULL if it should be fetched from configuration
* @return void
*/
public function setRoutesConfiguration(array $routesConfiguration = null)
{
$this->routesConfiguration = $routesConfiguration;
$this->routesCreated = false;
}
/**
* Iterates through all configured routes and calls matches() on them.
* Returns the matchResults of the matching route or NULL if no matching
* route could be found.
*
* @param RouteContext $routeContext The Route Context containing the current HTTP Request and, optional, Routing RouteParameters
* @return array The results of the matching route or NULL if no route matched
* @throws InvalidRouteSetupException
* @throws NoMatchingRouteException if no route matched the given $routeContext
* @throws InvalidRoutePartValueException
*/
public function route(RouteContext $routeContext): array
{
$this->lastMatchedRoute = null;
$cachedRouteResult = $this->routerCachingService->getCachedMatchResults($routeContext);
if ($cachedRouteResult !== false) {
return $cachedRouteResult;
}
$this->createRoutesFromConfiguration();
$httpRequest = $routeContext->getHttpRequest();
/** @var $route Route */
foreach ($this->routes as $route) {
if ($route->matches($routeContext) === true) {
$this->lastMatchedRoute = $route;
$matchResults = $route->getMatchResults();
$this->routerCachingService->storeMatchResults($routeContext, $matchResults, $route->getMatchedTags());
$this->logger->debug(sprintf('Router route(): Route "%s" matched the request "%s (%s)".', $route->getName(), $httpRequest->getUri(), $httpRequest->getMethod()));
return $matchResults;
}
}
$routePath = UriHelper::getRelativePath(RequestInformationHelper::generateBaseUri($httpRequest), $httpRequest->getUri());
$this->logger->debug(sprintf('Router route(): No route matched the route path "%s".', $routePath));
throw new NoMatchingRouteException('Could not match a route for the HTTP request.', 1510846308);
}
/**
* Returns the route that has been matched with the last route() call.
* Returns NULL if no route matched or route() has not been called yet
*
* @return Route
*/
public function getLastMatchedRoute()
{
return $this->lastMatchedRoute;
}
/**
* Returns a list of configured routes
*
* @return array
*/
public function getRoutes()
{
$this->createRoutesFromConfiguration();
return $this->routes;
}
/**
* Manually adds a route to the beginning of the configured routes
*
* @param Route $route
* @return void
*/
public function addRoute(Route $route)
{
$this->createRoutesFromConfiguration();
array_unshift($this->routes, $route);
}
/**
* Builds the corresponding uri (excluding protocol and host) by iterating
* through all configured routes and calling their respective resolves()
* method. If no matching route is found, an empty string is returned.
* Note: calls of this message are cached by RouterCachingAspect
*
* @param ResolveContext $resolveContext The Resolve Context containing the route values, the request URI and some flags to be resolved
* @return UriInterface The resolved URI
* @throws NoMatchingRouteException if no route could resolve the given $resolveContext
*/
public function resolve(ResolveContext $resolveContext): UriInterface
{
$this->lastResolvedRoute = null;
$cachedResolvedUriConstraints = $this->routerCachingService->getCachedResolvedUriConstraints($resolveContext);
if ($cachedResolvedUriConstraints !== false) {
return $cachedResolvedUriConstraints->applyTo($resolveContext->getBaseUri(), $resolveContext->isForceAbsoluteUri());
}
$this->createRoutesFromConfiguration();
/** @var $route Route */
foreach ($this->routes as $route) {
if ($route->resolves($resolveContext) === true) {
$uriConstraints = $route->getResolvedUriConstraints()->withPathPrefix($resolveContext->getUriPathPrefix());
$resolvedUri = $uriConstraints->applyTo($resolveContext->getBaseUri(), $resolveContext->isForceAbsoluteUri());
$this->routerCachingService->storeResolvedUriConstraints($resolveContext, $uriConstraints, $route->getResolvedTags());
$this->lastResolvedRoute = $route;
return $resolvedUri;
}
}
$this->logger->warning('Router resolve(): Could not resolve a route for building an URI for the given resolve context.', LogEnvironment::fromMethodName(__METHOD__) + ['routeValues' => $resolveContext->getRouteValues()]);
throw new NoMatchingRouteException('Could not resolve a route and its corresponding URI for the given parameters. This may be due to referring to a not existing package / controller / action while building a link or URI. Refer to log and check the backtrace for more details.', 1301610453);
}
/**
* Returns the route that has been resolved with the last resolve() call.
* Returns NULL if no route was found or resolve() has not been called yet
*
* @return Route
*/
public function getLastResolvedRoute()
{
return $this->lastResolvedRoute;
}
/**
* Creates \Neos\Flow\Mvc\Routing\Route objects from the injected routes
* configuration.
*
* @return void
* @throws InvalidRouteSetupException
*/
protected function createRoutesFromConfiguration()
{
if ($this->routesCreated === true) {
return;
}
$this->initializeRoutesConfiguration();
$this->routes = [];
$routesWithHttpMethodConstraints = [];
foreach ($this->routesConfiguration as $routeConfiguration) {
$route = new Route();
if (isset($routeConfiguration['name'])) {
$route->setName($routeConfiguration['name']);
}
$uriPattern = $routeConfiguration['uriPattern'];
$route->setUriPattern($uriPattern);
if (isset($routeConfiguration['defaults'])) {
$route->setDefaults($routeConfiguration['defaults']);
}
if (isset($routeConfiguration['routeParts'])) {
$route->setRoutePartsConfiguration($routeConfiguration['routeParts']);
}
if (isset($routeConfiguration['toLowerCase'])) {
$route->setLowerCase($routeConfiguration['toLowerCase']);
}
if (isset($routeConfiguration['appendExceedingArguments'])) {
$route->setAppendExceedingArguments($routeConfiguration['appendExceedingArguments']);
}
if (isset($routeConfiguration['httpMethods'])) {
if (isset($routesWithHttpMethodConstraints[$uriPattern]) && $routesWithHttpMethodConstraints[$uriPattern] === false) {
throw new InvalidRouteSetupException(sprintf('There are multiple routes with the uriPattern "%s" and "httpMethods" option set. Please specify accepted HTTP methods for all of these, or adjust the uriPattern', $uriPattern), 1365678427);
}
$routesWithHttpMethodConstraints[$uriPattern] = true;
$route->setHttpMethods($routeConfiguration['httpMethods']);
} else {
if (isset($routesWithHttpMethodConstraints[$uriPattern]) && $routesWithHttpMethodConstraints[$uriPattern] === true) {
throw new InvalidRouteSetupException(sprintf('There are multiple routes with the uriPattern "%s" and "httpMethods" option set. Please specify accepted HTTP methods for all of these, or adjust the uriPattern', $uriPattern), 1365678432);
}
$routesWithHttpMethodConstraints[$uriPattern] = false;
}
$this->routes[] = $route;
}
$this->routesCreated = true;
}
/**
* Checks if a routes configuration was set and otherwise loads the configuration from the configuration manager.
*
* @return void
*/
protected function initializeRoutesConfiguration()
{
if ($this->routesConfiguration === null) {
$this->routesConfiguration = $this->configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_ROUTES);
}
}
}