diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/RouteDefinition.php | 13 | ||||
| -rw-r--r-- | src/Router.php | 36 |
2 files changed, 34 insertions, 15 deletions
diff --git a/src/RouteDefinition.php b/src/RouteDefinition.php new file mode 100644 index 0000000..ed6c721 --- /dev/null +++ b/src/RouteDefinition.php @@ -0,0 +1,13 @@ +<?php + +declare(strict_types=1); + +namespace Lightscale\Router; + +class RouteDefinition +{ + public function __construct( + private Router $router, + private Route $route, + ) {} +} diff --git a/src/Router.php b/src/Router.php index 9632c69..5757b35 100644 --- a/src/Router.php +++ b/src/Router.php @@ -114,8 +114,11 @@ class Router return $this->strategy->runRoute($call); } - public function route(HttpMethod $method, string $path, callable $handler): void - { + public function route( + HttpMethod $method, + string $path, + callable $handler, + ): RouteDefinition { $pathSplit = $this->splitPath($path); $seg = $this->root(); @@ -123,36 +126,39 @@ class Router $seg = $seg->child($v); } - $seg->addRoute(new Route($method, $handler)); + $route = new Route($method, $handler); + $seg->addRoute($route); + + return new RouteDefinition($this, $route); } - public function get(string $path, callable $handler): void + public function get(string $path, callable $handler): RouteDefinition { - $this->route(HttpMethod::Get, $path, $handler); + return $this->route(HttpMethod::Get, $path, $handler); } - public function post(string $path, callable $handler): void + public function post(string $path, callable $handler): RouteDefinition { - $this->route(HttpMethod::Post, $path, $handler); + return $this->route(HttpMethod::Post, $path, $handler); } - public function put(string $path, callable $handler): void + public function put(string $path, callable $handler): RouteDefinition { - $this->route(HttpMethod::Put, $path, $handler); + return $this->route(HttpMethod::Put, $path, $handler); } - public function patch(string $path, callable $handler): void + public function patch(string $path, callable $handler): RouteDefinition { - $this->route(HttpMethod::Patch, $path, $handler); + return $this->route(HttpMethod::Patch, $path, $handler); } - public function delete(string $path, callable $handler): void + public function delete(string $path, callable $handler): RouteDefinition { - $this->route(HttpMethod::Delete, $path, $handler); + return $this->route(HttpMethod::Delete, $path, $handler); } - public function any(string $path, callable $handler): void + public function any(string $path, callable $handler): RouteDefinition { - $this->route(HttpMethod::Any, $path, $handler); + return $this->route(HttpMethod::Any, $path, $handler); } } |
