From 8c0efd0d9317ad92bd55cd6afcd41bdbab827bf8 Mon Sep 17 00:00:00 2001 From: Sam Light Date: Wed, 10 Jun 2026 19:00:32 +0100 Subject: Make basic routing work --- src/BasicStrategy.php | 23 ++++++++++++++ src/Contracts/Strategy.php | 15 +++++++++ src/Route.php | 11 +++---- src/RouteCall.php | 17 ++++++++++ src/Router.php | 77 +++++++++++++++++++++++++++++++++++++++++----- 5 files changed, 130 insertions(+), 13 deletions(-) create mode 100644 src/BasicStrategy.php create mode 100644 src/Contracts/Strategy.php create mode 100644 src/RouteCall.php (limited to 'src') diff --git a/src/BasicStrategy.php b/src/BasicStrategy.php new file mode 100644 index 0000000..62bc48e --- /dev/null +++ b/src/BasicStrategy.php @@ -0,0 +1,23 @@ +route->getHandler())($call); + } + + public function notFound(RequestInterface $request): ResponseInterface + { + throw new NotFoundException(); + } +} diff --git a/src/Contracts/Strategy.php b/src/Contracts/Strategy.php new file mode 100644 index 0000000..81d6ec3 --- /dev/null +++ b/src/Contracts/Strategy.php @@ -0,0 +1,15 @@ +handler)($request); - } - public function getMethod(): HttpMethod { return $this->method; @@ -50,4 +44,9 @@ class Route { return $this->segment->getPath(); } + + public function getHandler(): Closure + { + return $this->handler; + } } diff --git a/src/RouteCall.php b/src/RouteCall.php new file mode 100644 index 0000000..b76a780 --- /dev/null +++ b/src/RouteCall.php @@ -0,0 +1,17 @@ + $parameters */ + public function __construct( + public RequestInterface $request, + public Route $route, + public array $parameters, + ) {} +} diff --git a/src/Router.php b/src/Router.php index f666fe5..ddd3220 100644 --- a/src/Router.php +++ b/src/Router.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Lightscale\Router; +use Lightscale\Router\Contracts\Strategy; use Lightscale\Router\Enums\HttpMethod; use Lightscale\Router\Enums\PathSegmentType; use Lightscale\Router\Exceptions\NotFoundException; @@ -14,10 +15,22 @@ use Psr\Http\Message\ResponseInterface; class Router { private PathSegment $root; + private Strategy $strategy; public function __construct() { $this->root = new PathSegment(type: PathSegmentType::Root); + $this->strategy = new BasicStrategy; + } + + public function getStrategy(): Strategy + { + return $this->strategy; + } + + public function setStrategy(Strategy $strategy): void + { + $this->strategy = $strategy; } public function root(): PathSegment @@ -60,21 +73,71 @@ class Router $match = $this->findSegment($uri->getPath()); if (null === $match) { - throw new NotFoundException(); + return $this->strategy->notFound($request); } $segment = $match->segment; $method = $request->getMethod(); - $method = ( - HttpMethod::tryFrom(strtolower($method)) ?? - throw new UnknownMethodException() - ); + $method = HttpMethod::tryFrom(strtolower($method)); + + if ($method === null) { + return $this->strategy->notFound($request); + } + $route = $segment->getRoute($method); if (null === $route) { - throw new NotFoundException(); + return $this->strategy->notFound($request); + } + + $call = new RouteCall( + $request, + $route, + $match->parameters, + ); + + return $this->strategy->runRoute($call); + } + + public function route(HttpMethod $method, string $path, callable $handler): void + { + $pathSplit = $this->splitPath($path); + + $seg = $this->root(); + while(($v = array_shift($pathSplit)) !== null) { + $seg = $seg->child($v); } - return $route($request); + $seg->addRoute(new Route($method, $handler)); + } + + public function get(string $path, callable $handler): void + { + $this->route(HttpMethod::Get, $path, $handler); + } + + public function post(string $path, callable $handler): void + { + $this->route(HttpMethod::Post, $path, $handler); + } + + public function put(string $path, callable $handler): void + { + $this->route(HttpMethod::Put, $path, $handler); + } + + public function patch(string $path, callable $handler): void + { + $this->route(HttpMethod::Patch, $path, $handler); + } + + public function delete(string $path, callable $handler): void + { + $this->route(HttpMethod::Delete, $path, $handler); + } + + public function any(string $path, callable $handler): void + { + $this->route(HttpMethod::Any, $path, $handler); } } -- cgit v1.2.3