From 4c6c1b00e69ea06367dba96b3b024af632b729b6 Mon Sep 17 00:00:00 2001 From: Sam Light Date: Wed, 10 Jun 2026 19:00:32 +0100 Subject: Refactor dispatch to use a seperate findRoute method --- src/RouteMatch.php | 14 ++++++++++++++ src/Router.php | 36 ++++++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 src/RouteMatch.php (limited to 'src') diff --git a/src/RouteMatch.php b/src/RouteMatch.php new file mode 100644 index 0000000..d43bf80 --- /dev/null +++ b/src/RouteMatch.php @@ -0,0 +1,14 @@ +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); -- cgit v1.2.3