summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam Light <sam@lightscale.co.uk>2026-06-10 19:00:32 +0100
committerSam Light <sam@lightscale.co.uk>2026-06-10 19:00:32 +0100
commit7f3ad6625e00e39cad47cbfca97e6b872712d10a (patch)
treed3e5880f96eb70d4a32d023971ac3bf3531d163c /src
parent8abfce2091e9665af93a920aed246e3b04abc2aa (diff)
Route methods now return RouteDefinition class and testing
Diffstat (limited to 'src')
-rw-r--r--src/RouteDefinition.php13
-rw-r--r--src/Router.php36
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);
}
}