From 5542d85ddb8645e2b6bba2d91905863ecf380edd Mon Sep 17 00:00:00 2001 From: Buster Neece Date: Sat, 5 Nov 2022 11:19:17 -0500 Subject: [PATCH 1/4] Persist routes indexed by name for improved performance. Signed-off-by: Buster Neece --- Slim/Routing/RouteCollector.php | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Slim/Routing/RouteCollector.php b/Slim/Routing/RouteCollector.php index a6947cdac..5dcdae86c 100644 --- a/Slim/Routing/RouteCollector.php +++ b/Slim/Routing/RouteCollector.php @@ -59,6 +59,13 @@ class RouteCollector implements RouteCollectorInterface */ protected array $routes = []; + /** + * Routes indexed by name + * + * @var RouteInterface[] + */ + protected array $routesByName = []; + /** * Route groups * @@ -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; } @@ -181,11 +189,10 @@ public function removeNamedRoute(string $name): RouteCollectorInterface */ public function getNamedRoute(string $name): RouteInterface { - foreach ($this->routes as $route) { - if ($name === $route->getName()) { - return $route; - } + if (isset($this->routesByName[$name])) { + return $this->routesByName[$name]; } + throw new RuntimeException('Named route does not exist for name: ' . $name); } @@ -229,6 +236,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 (null !== $routeName && !isset($this->routesByName[$routeName])) { + $this->routesByName[$routeName] = $route; + } + $this->routeCounter++; return $route; From aa7f3316a009d4853eda5d720d80c6c5c6d5dee9 Mon Sep 17 00:00:00 2001 From: Buster Neece Date: Sat, 5 Nov 2022 15:43:56 -0500 Subject: [PATCH 2/4] Requested fixes. --- Slim/Routing/RouteCollector.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Slim/Routing/RouteCollector.php b/Slim/Routing/RouteCollector.php index 5dcdae86c..4a7428516 100644 --- a/Slim/Routing/RouteCollector.php +++ b/Slim/Routing/RouteCollector.php @@ -190,7 +190,19 @@ public function removeNamedRoute(string $name): RouteCollectorInterface public function getNamedRoute(string $name): RouteInterface { if (isset($this->routesByName[$name])) { - return $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); @@ -238,7 +250,7 @@ public function map(array $methods, string $pattern, $handler): RouteInterface $this->routes[$route->getIdentifier()] = $route; $routeName = $route->getName(); - if (null !== $routeName && !isset($this->routesByName[$routeName])) { + if ($routeName !== null && !isset($this->routesByName[$routeName])) { $this->routesByName[$routeName] = $route; } From 3abf74276f985a148e6ea575df0ae44eb57b47b1 Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Sun, 6 Nov 2022 10:20:35 -0600 Subject: [PATCH 3/4] Tidy up PHPUnit output Don't output the tip to the log. --- tests/Handlers/ErrorHandlerTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/Handlers/ErrorHandlerTest.php b/tests/Handlers/ErrorHandlerTest.php index 0a97c0c7e..6d6df7fc1 100644 --- a/tests/Handlers/ErrorHandlerTest.php +++ b/tests/Handlers/ErrorHandlerTest.php @@ -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); From c0bc6a53877883d6557b752381c443697b0641b4 Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Sun, 6 Nov 2022 10:30:23 -0600 Subject: [PATCH 4/4] Remove log output from PHPUnit output These tests aren't testing the log output, so they just clutter up the PHPUnit output for no reason. --- tests/Handlers/ErrorHandlerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Handlers/ErrorHandlerTest.php b/tests/Handlers/ErrorHandlerTest.php index 6d6df7fc1..f23acd6b3 100644 --- a/tests/Handlers/ErrorHandlerTest.php +++ b/tests/Handlers/ErrorHandlerTest.php @@ -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')); @@ -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'));