Skip to content

Commit

Permalink
Make ConfigureRoutes fluent
Browse files Browse the repository at this point in the history
(cherry picked from commit 81d5460)
  • Loading branch information
codemasher committed Apr 21, 2024
1 parent d3ada01 commit 3efa22b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
20 changes: 10 additions & 10 deletions src/ConfigureRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down
40 changes: 22 additions & 18 deletions src/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 31 in src/RouteCollector.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

The return type of FastRoute\RouteCollector#addRoute() changed from void to the non-covariant static

Check failure on line 31 in src/RouteCollector.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

The return type of FastRoute\RouteCollector#addRoute() changed from void to static
{
$route = $this->currentGroupPrefix . $route;
$parsedRoutes = $this->routeParser->parse($route);
Expand All @@ -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 */
Expand All @@ -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

Check failure on line 65 in src/RouteCollector.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

The return type of FastRoute\RouteCollector#addGroup() changed from void to the non-covariant static

Check failure on line 65 in src/RouteCollector.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

The return type of FastRoute\RouteCollector#addGroup() changed from void to 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

Check failure on line 76 in src/RouteCollector.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

The return type of FastRoute\RouteCollector#any() changed from void to the non-covariant static

Check failure on line 76 in src/RouteCollector.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

The return type of FastRoute\RouteCollector#any() changed from void to 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

Check failure on line 82 in src/RouteCollector.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

The return type of FastRoute\RouteCollector#get() changed from void to the non-covariant static

Check failure on line 82 in src/RouteCollector.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

The return type of FastRoute\RouteCollector#get() changed from void to 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

Check failure on line 88 in src/RouteCollector.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

The return type of FastRoute\RouteCollector#post() changed from void to the non-covariant static

Check failure on line 88 in src/RouteCollector.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

The return type of FastRoute\RouteCollector#post() changed from void to 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 */
Expand Down

0 comments on commit 3efa22b

Please sign in to comment.