diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/RouteMatch.php | 14 | ||||
| -rw-r--r-- | src/Router.php | 36 |
2 files changed, 40 insertions, 10 deletions
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 @@ +<?php + +declare(strict_types=1); + +namespace Lightscale\Router; + +readonly class RouteMatch +{ + public function __construct( + public PathSegmentMatch $segmentMatch, + public Route $route, + ) { + } +} 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); |
