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 | 8c0efd0d9317ad92bd55cd6afcd41bdbab827bf8 (patch) | |
| tree | c654456753e90ab38b7bcb89ece3381ad7a40f4a /src/Router.php | |
| parent | 5fe7c87967ff29c4a8f03a9186918d8359f4887e (diff) | |
Make basic routing work
Diffstat (limited to 'src/Router.php')
| -rw-r--r-- | src/Router.php | 77 |
1 files changed, 70 insertions, 7 deletions
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); } } |
