Skip to content

Commit bf0e378

Browse files
committed
Update for changes in http-server v3.0
Also fixed some new Psalm issues.
1 parent 2462b65 commit bf0e378

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/Router.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@ final class Router implements RequestHandler
2424

2525
private ?RequestHandler $fallback = null;
2626

27-
/** @var array[] */
27+
/** @var list<array{string, string, RequestHandler}> */
2828
private array $routes = [];
2929

30-
/** @var Middleware[] */
30+
/** @var list<Middleware> */
3131
private array $middlewares = [];
3232

3333
private string $prefix = "/";
3434

35+
/** @var LocalCache<array{int, RequestHandler, array<string, string>}> */
3536
private readonly LocalCache $cache;
3637

3738
/**
38-
* @param int $cacheSize Maximum number of route matches to cache.
39+
* @param positive-int $cacheSize Maximum number of route matches to cache.
3940
*
4041
* @throws \Error If `$cacheSize` is less than zero.
4142
*/
@@ -47,6 +48,7 @@ public function __construct(
4748
$httpServer->onStart($this->onStart(...));
4849
$httpServer->onStop($this->onStop(...));
4950

51+
/** @psalm-suppress DocblockTypeContradiction */
5052
if ($cacheSize <= 0) {
5153
throw new \ValueError("The number of cache entries must be greater than zero");
5254
}
@@ -77,7 +79,7 @@ public function handleRequest(Request $request): Response
7779
case Dispatcher::FOUND:
7880
/**
7981
* @var RequestHandler $requestHandler
80-
* @var string[] $routeArgs
82+
* @var array<string, string> $routeArgs
8183
*/
8284
[, $requestHandler, $routeArgs] = $match;
8385
$request->setAttribute(self::class, $routeArgs);
@@ -126,8 +128,6 @@ private function makeMethodNotAllowedResponse(array $methods, Request $request):
126128
/**
127129
* Merge another router's routes into this router.
128130
*
129-
* Doing so might improve performance for request dispatching.
130-
*
131131
* @param self $router Router to merge.
132132
*/
133133
public function merge(self $router): void
@@ -136,10 +136,10 @@ public function merge(self $router): void
136136
throw new \Error("Cannot merge routers after the server has started");
137137
}
138138

139-
foreach ($router->routes as $route) {
140-
$route[1] = \ltrim($router->prefix, "/") . $route[1];
141-
$route[2] = Middleware\stack($route[2], ...$router->middlewares);
142-
$this->routes[] = $route;
139+
foreach ($router->routes as [$method, $path, $requestHandler]) {
140+
$path = \ltrim($router->prefix, "/") . $path;
141+
$requestHandler = Middleware\stackMiddleware($requestHandler, ...$router->middlewares);
142+
$this->routes[] = [$method, $path, $requestHandler];
143143
}
144144
}
145145

@@ -200,7 +200,7 @@ public function addRoute(
200200
}
201201

202202
if (!empty($middlewares)) {
203-
$requestHandler = Middleware\stack($requestHandler, ...$middlewares);
203+
$requestHandler = Middleware\stackMiddleware($requestHandler, ...$middlewares);
204204
}
205205

206206
$this->routes[] = [$method, \ltrim($uri, "/"), $requestHandler];
@@ -223,7 +223,7 @@ public function stack(Middleware ...$middlewares): void
223223
throw new \Error("Cannot set middlewares after the server has started");
224224
}
225225

226-
$this->middlewares = \array_merge($middlewares, $this->middlewares);
226+
$this->middlewares = [...\array_values($middlewares), ...$this->middlewares];
227227
}
228228

229229
/**
@@ -272,7 +272,7 @@ private function onStart(): void
272272
});
273273

274274
foreach ($this->routes as [$method, $uri, $requestHandler]) {
275-
$requestHandler = Middleware\stack($requestHandler, ...$this->middlewares);
275+
$requestHandler = Middleware\stackMiddleware($requestHandler, ...$this->middlewares);
276276
$uri = $this->prefix . $uri;
277277

278278
// Special-case, otherwise we redirect just to the same URI again

0 commit comments

Comments
 (0)