From bf0e37870af85af18ce46da527d017a22a0822bd Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Mon, 31 Jul 2023 18:46:34 -0500 Subject: [PATCH] Update for changes in http-server v3.0 Also fixed some new Psalm issues. --- src/Router.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Router.php b/src/Router.php index 5c14f41..3c40e59 100644 --- a/src/Router.php +++ b/src/Router.php @@ -24,18 +24,19 @@ final class Router implements RequestHandler private ?RequestHandler $fallback = null; - /** @var array[] */ + /** @var list */ private array $routes = []; - /** @var Middleware[] */ + /** @var list */ private array $middlewares = []; private string $prefix = "/"; + /** @var LocalCache}> */ private readonly LocalCache $cache; /** - * @param int $cacheSize Maximum number of route matches to cache. + * @param positive-int $cacheSize Maximum number of route matches to cache. * * @throws \Error If `$cacheSize` is less than zero. */ @@ -47,6 +48,7 @@ public function __construct( $httpServer->onStart($this->onStart(...)); $httpServer->onStop($this->onStop(...)); + /** @psalm-suppress DocblockTypeContradiction */ if ($cacheSize <= 0) { throw new \ValueError("The number of cache entries must be greater than zero"); } @@ -77,7 +79,7 @@ public function handleRequest(Request $request): Response case Dispatcher::FOUND: /** * @var RequestHandler $requestHandler - * @var string[] $routeArgs + * @var array $routeArgs */ [, $requestHandler, $routeArgs] = $match; $request->setAttribute(self::class, $routeArgs); @@ -126,8 +128,6 @@ private function makeMethodNotAllowedResponse(array $methods, Request $request): /** * Merge another router's routes into this router. * - * Doing so might improve performance for request dispatching. - * * @param self $router Router to merge. */ public function merge(self $router): void @@ -136,10 +136,10 @@ public function merge(self $router): void throw new \Error("Cannot merge routers after the server has started"); } - foreach ($router->routes as $route) { - $route[1] = \ltrim($router->prefix, "/") . $route[1]; - $route[2] = Middleware\stack($route[2], ...$router->middlewares); - $this->routes[] = $route; + foreach ($router->routes as [$method, $path, $requestHandler]) { + $path = \ltrim($router->prefix, "/") . $path; + $requestHandler = Middleware\stackMiddleware($requestHandler, ...$router->middlewares); + $this->routes[] = [$method, $path, $requestHandler]; } } @@ -200,7 +200,7 @@ public function addRoute( } if (!empty($middlewares)) { - $requestHandler = Middleware\stack($requestHandler, ...$middlewares); + $requestHandler = Middleware\stackMiddleware($requestHandler, ...$middlewares); } $this->routes[] = [$method, \ltrim($uri, "/"), $requestHandler]; @@ -223,7 +223,7 @@ public function stack(Middleware ...$middlewares): void throw new \Error("Cannot set middlewares after the server has started"); } - $this->middlewares = \array_merge($middlewares, $this->middlewares); + $this->middlewares = [...\array_values($middlewares), ...$this->middlewares]; } /** @@ -272,7 +272,7 @@ private function onStart(): void }); foreach ($this->routes as [$method, $uri, $requestHandler]) { - $requestHandler = Middleware\stack($requestHandler, ...$this->middlewares); + $requestHandler = Middleware\stackMiddleware($requestHandler, ...$this->middlewares); $uri = $this->prefix . $uri; // Special-case, otherwise we redirect just to the same URI again