Skip to content

Commit b0f4ca3

Browse files
authored
Merge pull request #3235 from BusterNeece/4.x
Persist routes indexed by name in RouteCollector for improved performance.
2 parents ca629c1 + c0bc6a5 commit b0f4ca3

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

Slim/Routing/RouteCollector.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ class RouteCollector implements RouteCollectorInterface
5959
*/
6060
protected array $routes = [];
6161

62+
/**
63+
* Routes indexed by name
64+
*
65+
* @var RouteInterface[]
66+
*/
67+
protected array $routesByName = [];
68+
6269
/**
6370
* Route groups
6471
*
@@ -172,7 +179,8 @@ public function getRoutes(): array
172179
public function removeNamedRoute(string $name): RouteCollectorInterface
173180
{
174181
$route = $this->getNamedRoute($name);
175-
unset($this->routes[$route->getIdentifier()]);
182+
183+
unset($this->routesByName[$route->getName()], $this->routes[$route->getIdentifier()]);
176184
return $this;
177185
}
178186

@@ -181,11 +189,22 @@ public function removeNamedRoute(string $name): RouteCollectorInterface
181189
*/
182190
public function getNamedRoute(string $name): RouteInterface
183191
{
192+
if (isset($this->routesByName[$name])) {
193+
$route = $this->routesByName[$name];
194+
if ($route->getName() === $name) {
195+
return $route;
196+
}
197+
198+
unset($this->routesByName[$name]);
199+
}
200+
184201
foreach ($this->routes as $route) {
185202
if ($name === $route->getName()) {
203+
$this->routesByName[$name] = $route;
186204
return $route;
187205
}
188206
}
207+
189208
throw new RuntimeException('Named route does not exist for name: ' . $name);
190209
}
191210

@@ -229,6 +248,12 @@ public function map(array $methods, string $pattern, $handler): RouteInterface
229248
{
230249
$route = $this->createRoute($methods, $pattern, $handler);
231250
$this->routes[$route->getIdentifier()] = $route;
251+
252+
$routeName = $route->getName();
253+
if ($routeName !== null && !isset($this->routesByName[$routeName])) {
254+
$this->routesByName[$routeName] = $route;
255+
}
256+
232257
$this->routeCounter++;
233258

234259
return $route;

tests/Handlers/ErrorHandlerTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public function testOptions()
296296
$exception->setAllowedMethods(['POST', 'PUT']);
297297

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

301301
$this->assertSame(200, $res->getStatusCode());
302302
$this->assertTrue($res->hasHeader('Allow'));
@@ -367,7 +367,7 @@ public function testDefaultErrorRenderer()
367367
$exception = new RuntimeException();
368368

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

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

391+
$displayErrorDetailsProperty = new ReflectionProperty($handler, 'displayErrorDetails');
392+
$displayErrorDetailsProperty->setAccessible(true);
393+
$displayErrorDetailsProperty->setValue($handler, true);
394+
391395
$exception = new RuntimeException();
392396
$exceptionProperty = new ReflectionProperty($handler, 'exception');
393397
$exceptionProperty->setAccessible(true);

0 commit comments

Comments
 (0)