Skip to content

Commit

Permalink
Merge pull request #3235 from BusterNeece/4.x
Browse files Browse the repository at this point in the history
Persist routes indexed by name in RouteCollector for improved performance.
  • Loading branch information
akrabat committed Nov 6, 2022
2 parents ca629c1 + c0bc6a5 commit b0f4ca3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
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;
}
}

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

0 comments on commit b0f4ca3

Please sign in to comment.