Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Pass the ltrimed path to handleRoute/Middleware in Router #36

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions include/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ public function route(ServerRequestInterface $request): void

$this->request = array_reduce(
$middlewares,
fn(ServerRequestInterface $mid_request, MiddlewareInformation $middleware) => $this->handleMiddleware($middleware, $mid_request),
fn(ServerRequestInterface $mid_request, MiddlewareInformation $middleware) => $this->handleMiddleware($middleware, $path, $mid_request),
$this->request,
);

$this->response = $this->handleRoute($route, $this->request);
$this->response = $this->handleRoute($route, $path, $this->request);
} catch (HTTPException $exception) {
$this->response = $exception->toResponse();
} catch (Throwable $throwable) {
Expand Down Expand Up @@ -96,10 +96,10 @@ public function response(): void
$final_handler->writeResponse($response);
}

private function handleMiddleware(MiddlewareInformation $middleware, ServerRequestInterface $request): ServerRequestInterface
private function handleMiddleware(MiddlewareInformation $middleware, string $path, ServerRequestInterface $request): ServerRequestInterface
{
$attributes = [];
assert(preg_match($middleware->route_regex, $request->getUri()->getPath(), $attributes) === 1);
assert(preg_match($middleware->route_regex, $path, $attributes) === 1);
foreach ($attributes as $key => $value) {
// preg_match array result should have int key for 'normal' groups and string key for named groups
if (is_string($key)) {
Expand All @@ -110,11 +110,11 @@ private function handleMiddleware(MiddlewareInformation $middleware, ServerReque
return $middleware->handler->process($request);
}

private function handleRoute(RouteInformation $route, ServerRequestInterface $request): ResponseInterface
private function handleRoute(RouteInformation $route, string $path, ServerRequestInterface $request): ResponseInterface
{
$factory = new HttpFactory();
$attributes = [];
assert(preg_match($route->route_regex, $request->getUri()->getPath(), $attributes) === 1);
assert(preg_match($route->route_regex, $path, $attributes) === 1);
foreach ($attributes as $key => $value) {
// preg_match array result should have int key for 'normal' groups and string key for named groups
if (is_string($key)) {
Expand Down