diff --git a/src/ConfigureRoutes.php b/src/ConfigureRoutes.php index bcd97fc..740a59b 100644 --- a/src/ConfigureRoutes.php +++ b/src/ConfigureRoutes.php @@ -23,14 +23,14 @@ interface ConfigureRoutes * @param string|string[] $httpMethod * @param ExtraParameters $extraParameters */ - public function addRoute(string|array $httpMethod, string $route, mixed $handler, array $extraParameters = []): void; + public function addRoute(string|array $httpMethod, string $route, mixed $handler, array $extraParameters = []): static; /** * Create a route group with a common prefix. * * All routes created by the passed callback will have the given group prefix prepended. */ - public function addGroup(string $prefix, callable $callback): void; + public function addGroup(string $prefix, callable $callback): static; /** * Adds a fallback route to the collection @@ -39,7 +39,7 @@ public function addGroup(string $prefix, callable $callback): void; * * @param ExtraParameters $extraParameters */ - public function any(string $route, mixed $handler, array $extraParameters = []): void; + public function any(string $route, mixed $handler, array $extraParameters = []): static; /** * Adds a GET route to the collection @@ -48,7 +48,7 @@ public function any(string $route, mixed $handler, array $extraParameters = []): * * @param ExtraParameters $extraParameters */ - public function get(string $route, mixed $handler, array $extraParameters = []): void; + public function get(string $route, mixed $handler, array $extraParameters = []): static; /** * Adds a POST route to the collection @@ -57,7 +57,7 @@ public function get(string $route, mixed $handler, array $extraParameters = []): * * @param ExtraParameters $extraParameters */ - public function post(string $route, mixed $handler, array $extraParameters = []): void; + public function post(string $route, mixed $handler, array $extraParameters = []): static; /** * Adds a PUT route to the collection @@ -66,7 +66,7 @@ public function post(string $route, mixed $handler, array $extraParameters = []) * * @param ExtraParameters $extraParameters */ - public function put(string $route, mixed $handler, array $extraParameters = []): void; + public function put(string $route, mixed $handler, array $extraParameters = []): static; /** * Adds a DELETE route to the collection @@ -75,7 +75,7 @@ public function put(string $route, mixed $handler, array $extraParameters = []): * * @param ExtraParameters $extraParameters */ - public function delete(string $route, mixed $handler, array $extraParameters = []): void; + public function delete(string $route, mixed $handler, array $extraParameters = []): static; /** * Adds a PATCH route to the collection @@ -84,7 +84,7 @@ public function delete(string $route, mixed $handler, array $extraParameters = [ * * @param ExtraParameters $extraParameters */ - public function patch(string $route, mixed $handler, array $extraParameters = []): void; + public function patch(string $route, mixed $handler, array $extraParameters = []): static; /** * Adds a HEAD route to the collection @@ -93,7 +93,7 @@ public function patch(string $route, mixed $handler, array $extraParameters = [] * * @param ExtraParameters $extraParameters */ - public function head(string $route, mixed $handler, array $extraParameters = []): void; + public function head(string $route, mixed $handler, array $extraParameters = []): static; /** * Adds an OPTIONS route to the collection @@ -102,7 +102,7 @@ public function head(string $route, mixed $handler, array $extraParameters = []) * * @param ExtraParameters $extraParameters */ - public function options(string $route, mixed $handler, array $extraParameters = []): void; + public function options(string $route, mixed $handler, array $extraParameters = []): static; /** * Returns the processed aggregated route data. diff --git a/src/RouteCollector.php b/src/RouteCollector.php index 9de94b6..ff61838 100644 --- a/src/RouteCollector.php +++ b/src/RouteCollector.php @@ -28,7 +28,7 @@ public function __construct( } /** @inheritDoc */ - public function addRoute(string|array $httpMethod, string $route, mixed $handler, array $extraParameters = []): void + public function addRoute(string|array $httpMethod, string $route, mixed $handler, array $extraParameters = []): static { $route = $this->currentGroupPrefix . $route; $parsedRoutes = $this->routeParser->parse($route); @@ -44,6 +44,8 @@ public function addRoute(string|array $httpMethod, string $route, mixed $handler if (array_key_exists(self::ROUTE_NAME, $extraParameters)) { $this->registerNamedRoute($extraParameters[self::ROUTE_NAME], $parsedRoutes); } + + return $this; } /** @param ParsedRoutes $parsedRoutes */ @@ -60,60 +62,62 @@ private function registerNamedRoute(mixed $name, array $parsedRoutes): void $this->namedRoutes[$name] = array_reverse($parsedRoutes); } - public function addGroup(string $prefix, callable $callback): void + public function addGroup(string $prefix, callable $callback): static { $previousGroupPrefix = $this->currentGroupPrefix; $this->currentGroupPrefix = $previousGroupPrefix . $prefix; $callback($this); $this->currentGroupPrefix = $previousGroupPrefix; + + return $this; } /** @inheritDoc */ - public function any(string $route, mixed $handler, array $extraParameters = []): void + public function any(string $route, mixed $handler, array $extraParameters = []): static { - $this->addRoute('*', $route, $handler, $extraParameters); + return $this->addRoute('*', $route, $handler, $extraParameters); } /** @inheritDoc */ - public function get(string $route, mixed $handler, array $extraParameters = []): void + public function get(string $route, mixed $handler, array $extraParameters = []): static { - $this->addRoute('GET', $route, $handler, $extraParameters); + return $this->addRoute('GET', $route, $handler, $extraParameters); } /** @inheritDoc */ - public function post(string $route, mixed $handler, array $extraParameters = []): void + public function post(string $route, mixed $handler, array $extraParameters = []): static { - $this->addRoute('POST', $route, $handler, $extraParameters); + return $this->addRoute('POST', $route, $handler, $extraParameters); } /** @inheritDoc */ - public function put(string $route, mixed $handler, array $extraParameters = []): void + public function put(string $route, mixed $handler, array $extraParameters = []): static { - $this->addRoute('PUT', $route, $handler, $extraParameters); + return $this->addRoute('PUT', $route, $handler, $extraParameters); } /** @inheritDoc */ - public function delete(string $route, mixed $handler, array $extraParameters = []): void + public function delete(string $route, mixed $handler, array $extraParameters = []): static { - $this->addRoute('DELETE', $route, $handler, $extraParameters); + return $this->addRoute('DELETE', $route, $handler, $extraParameters); } /** @inheritDoc */ - public function patch(string $route, mixed $handler, array $extraParameters = []): void + public function patch(string $route, mixed $handler, array $extraParameters = []): static { - $this->addRoute('PATCH', $route, $handler, $extraParameters); + return $this->addRoute('PATCH', $route, $handler, $extraParameters); } /** @inheritDoc */ - public function head(string $route, mixed $handler, array $extraParameters = []): void + public function head(string $route, mixed $handler, array $extraParameters = []): static { - $this->addRoute('HEAD', $route, $handler, $extraParameters); + return $this->addRoute('HEAD', $route, $handler, $extraParameters); } /** @inheritDoc */ - public function options(string $route, mixed $handler, array $extraParameters = []): void + public function options(string $route, mixed $handler, array $extraParameters = []): static { - $this->addRoute('OPTIONS', $route, $handler, $extraParameters); + return $this->addRoute('OPTIONS', $route, $handler, $extraParameters); } /** @inheritDoc */