diff options
| author | Sam Light <sam@lightscale.co.uk> | 2026-06-10 19:00:32 +0100 |
|---|---|---|
| committer | Sam Light <sam@lightscale.co.uk> | 2026-06-10 19:00:32 +0100 |
| commit | 4c6c1b00e69ea06367dba96b3b024af632b729b6 (patch) | |
| tree | 6f4d4700df5cd7d45a25082d0096ad70532438cd /src/Router.php | |
| parent | b1b5696cf2fa0b2050d5b0387c4d819d2f41e4d4 (diff) | |
Refactor dispatch to use a seperate findRoute method
Diffstat (limited to 'src/Router.php')
| -rw-r--r-- | src/Router.php | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/src/Router.php b/src/Router.php index d8580bd..9632c69 100644 --- a/src/Router.php +++ b/src/Router.php @@ -65,34 +65,50 @@ class Router ); } - public function dispatch(RequestInterface $request): ResponseInterface + public function findRoute(HttpMethod|string $method, string $path): ?RouteMatch { - $uri = $request->getUri(); - $match = $this->findSegment($uri->getPath()); + $match = $this->findSegment($path); if (null === $match) { - return $this->strategy->notFound($request); + return null; } - $segment = $match->segment; - $method = $request->getMethod(); - $method = HttpMethod::tryFrom(strtolower($method)); + $method = ( + $method instanceof HttpMethod ? + $method : + HttpMethod::tryFrom(strtolower($method)) + ); if (null === $method) { - return $this->strategy->notFound($request); + return null; } + $segment = $match->segment; $route = $segment->getRoute($method); $route ??= $segment->getRoute(HttpMethod::Any); if (null === $route) { + return null; + } + + return new RouteMatch( + $match, + $route, + ); + } + + public function dispatch(RequestInterface $request): ResponseInterface + { + $uri = $request->getUri(); + $match = $this->findRoute($request->getMethod(), $uri->getPath()); + if (null === $match) { return $this->strategy->notFound($request); } $call = new RouteCall( $request, - $route, - $match->parameters, + $match->route, + $match->segmentMatch->parameters, ); return $this->strategy->runRoute($call); |
