Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist routes indexed by name in RouteCollector for improved performance. #3235

Merged
merged 6 commits into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion Slim/Routing/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ class RouteCollector implements RouteCollectorInterface
*/
protected array $routes = [];

/**
* Routes indexed by name
*
* @var RouteInterface[]
*/
protected array $routesByName = [];

/**
* Route groups
*
Expand Down Expand Up @@ -172,7 +179,8 @@ public function getRoutes(): array
public function removeNamedRoute(string $name): RouteCollectorInterface
{
$route = $this->getNamedRoute($name);
unset($this->routes[$route->getIdentifier()]);

unset($this->routesByName[$route->getName()], $this->routes[$route->getIdentifier()]);
return $this;
}

Expand All @@ -181,11 +189,22 @@ public function removeNamedRoute(string $name): RouteCollectorInterface
*/
public function getNamedRoute(string $name): RouteInterface
{
if (isset($this->routesByName[$name])) {
$route = $this->routesByName[$name];
if ($route->getName() === $name) {
return $route;
}

unset($this->routesByName[$name]);
}

foreach ($this->routes as $route) {
if ($name === $route->getName()) {
$this->routesByName[$name] = $route;
return $route;
}
}

BusterNeece marked this conversation as resolved.
Show resolved Hide resolved
throw new RuntimeException('Named route does not exist for name: ' . $name);
}

Expand Down Expand Up @@ -229,6 +248,12 @@ public function map(array $methods, string $pattern, $handler): RouteInterface
{
$route = $this->createRoute($methods, $pattern, $handler);
$this->routes[$route->getIdentifier()] = $route;

$routeName = $route->getName();
if ($routeName !== null && !isset($this->routesByName[$routeName])) {
$this->routesByName[$routeName] = $route;
}

$this->routeCounter++;

return $route;
Expand Down
8 changes: 6 additions & 2 deletions tests/Handlers/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public function testOptions()
$exception->setAllowedMethods(['POST', 'PUT']);

/** @var ResponseInterface $res */
$res = $handler->__invoke($request, $exception, true, true, true);
$res = $handler->__invoke($request, $exception, true, false, true);

$this->assertSame(200, $res->getStatusCode());
$this->assertTrue($res->hasHeader('Allow'));
Expand Down Expand Up @@ -367,7 +367,7 @@ public function testDefaultErrorRenderer()
$exception = new RuntimeException();

/** @var ResponseInterface $res */
$res = $handler->__invoke($request, $exception, true, true, true);
$res = $handler->__invoke($request, $exception, true, false, true);

$this->assertTrue($res->hasHeader('Content-Type'));
$this->assertSame('text/html', $res->getHeaderLine('Content-Type'));
Expand All @@ -388,6 +388,10 @@ public function testLogErrorRenderer()
$handler = new ErrorHandler($callableResolverProphecy->reveal(), $this->getResponseFactory());
$handler->setLogErrorRenderer('logErrorRenderer');

$displayErrorDetailsProperty = new ReflectionProperty($handler, 'displayErrorDetails');
$displayErrorDetailsProperty->setAccessible(true);
$displayErrorDetailsProperty->setValue($handler, true);

$exception = new RuntimeException();
$exceptionProperty = new ReflectionProperty($handler, 'exception');
$exceptionProperty->setAccessible(true);
Expand Down