Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ConfigureRoutes fluent #288

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
110 changes: 6 additions & 104 deletions src/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,11 @@
namespace FastRoute;

use function array_key_exists;
use function array_reverse;
use function is_string;

/**
* @phpstan-import-type ProcessedData from ConfigureRoutes
* @phpstan-import-type ExtraParameters from DataGenerator
* @phpstan-import-type RoutesForUriGeneration from GenerateUri
* @phpstan-import-type ParsedRoutes from RouteParser
* @final
*/
class RouteCollector implements ConfigureRoutes
final class RouteCollector extends RouteCollectorAbstract

Check failure on line 8 in src/RouteCollector.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

Class FastRoute\RouteCollector became final

Check failure on line 8 in src/RouteCollector.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

Property FastRoute\RouteCollector#$currentGroupPrefix was removed

Check failure on line 8 in src/RouteCollector.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

Property FastRoute\RouteCollector#$routeParser was removed

Check failure on line 8 in src/RouteCollector.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

Property FastRoute\RouteCollector#$dataGenerator was removed
{
protected string $currentGroupPrefix = '';

/** @var RoutesForUriGeneration */
private array $namedRoutes = [];

public function __construct(
protected readonly RouteParser $routeParser,
protected readonly DataGenerator $dataGenerator,
) {
}

/** @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 11 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 11 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,96 +24,18 @@
if (array_key_exists(self::ROUTE_NAME, $extraParameters)) {
$this->registerNamedRoute($extraParameters[self::ROUTE_NAME], $parsedRoutes);
}
}

/** @param ParsedRoutes $parsedRoutes */
private function registerNamedRoute(mixed $name, array $parsedRoutes): void
{
if (! is_string($name) || $name === '') {
throw BadRouteException::invalidRouteName($name);
}

if (array_key_exists($name, $this->namedRoutes)) {
throw BadRouteException::namedRouteAlreadyDefined($name);
}

$this->namedRoutes[$name] = array_reverse($parsedRoutes);
return $this;
}

public function addGroup(string $prefix, callable $callback): void
/** @inheritDoc */

Check failure on line 31 in src/RouteCollector.php

View workflow job for this annotation

GitHub Actions / Check Coding Standards (locked, 8.1, ubuntu-latest)

Useless documentation comment with @inheritdoc.
public function addGroup(string $prefix, callable $callback): static

Check failure on line 32 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 32 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;
}

/** @inheritDoc */
public function any(string $route, mixed $handler, array $extraParameters = []): void
{
$this->addRoute('*', $route, $handler, $extraParameters);
}

/** @inheritDoc */
public function get(string $route, mixed $handler, array $extraParameters = []): void
{
$this->addRoute('GET', $route, $handler, $extraParameters);
}

/** @inheritDoc */
public function post(string $route, mixed $handler, array $extraParameters = []): void
{
$this->addRoute('POST', $route, $handler, $extraParameters);
}

/** @inheritDoc */
public function put(string $route, mixed $handler, array $extraParameters = []): void
{
$this->addRoute('PUT', $route, $handler, $extraParameters);
}

/** @inheritDoc */
public function delete(string $route, mixed $handler, array $extraParameters = []): void
{
$this->addRoute('DELETE', $route, $handler, $extraParameters);
}

/** @inheritDoc */
public function patch(string $route, mixed $handler, array $extraParameters = []): void
{
$this->addRoute('PATCH', $route, $handler, $extraParameters);
}

/** @inheritDoc */
public function head(string $route, mixed $handler, array $extraParameters = []): void
{
$this->addRoute('HEAD', $route, $handler, $extraParameters);
}

/** @inheritDoc */
public function options(string $route, mixed $handler, array $extraParameters = []): void
{
$this->addRoute('OPTIONS', $route, $handler, $extraParameters);
}

/** @inheritDoc */
public function processedRoutes(): array
{
$data = $this->dataGenerator->getData();
$data[] = $this->namedRoutes;

return $data;
}

/**
* @deprecated
*
* @see ConfigureRoutes::processedRoutes()
*
* @return ProcessedData
*/
public function getData(): array
{
return $this->processedRoutes();
return $this;
}
}
111 changes: 111 additions & 0 deletions src/RouteCollectorAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
declare(strict_types=1);

namespace FastRoute;

use function array_key_exists;
use function array_reverse;
use function is_string;

/**
* @phpstan-import-type ProcessedData from ConfigureRoutes
* @phpstan-import-type ExtraParameters from DataGenerator
* @phpstan-import-type RoutesForUriGeneration from GenerateUri
* @phpstan-import-type ParsedRoutes from RouteParser
*/
abstract class RouteCollectorAbstract implements ConfigureRoutes

Check failure on line 16 in src/RouteCollectorAbstract.php

View workflow job for this annotation

GitHub Actions / Check Coding Standards (locked, 8.1, ubuntu-latest)

Superfluous suffix "Abstract".
{
protected string $currentGroupPrefix = '';

/** @var RoutesForUriGeneration */
protected array $namedRoutes = [];

public function __construct(
protected readonly RouteParser $routeParser,
protected DataGenerator $dataGenerator,
) {
}

/** @param ParsedRoutes $parsedRoutes */
protected function registerNamedRoute(mixed $name, array $parsedRoutes): void
{
if (! is_string($name) || $name === '') {
throw BadRouteException::invalidRouteName($name);
}

if (array_key_exists($name, $this->namedRoutes)) {
throw BadRouteException::namedRouteAlreadyDefined($name);
}

$this->namedRoutes[$name] = array_reverse($parsedRoutes);
}

/** @inheritDoc */
public function processedRoutes(): array
{
$data = $this->dataGenerator->getData();
$data[] = $this->namedRoutes;

return $data;
}

/**
* @deprecated
*
* @see ConfigureRoutes::processedRoutes()
*
* @return ProcessedData
*/
public function getData(): array
{
return $this->processedRoutes();
}

/** @inheritDoc */
public function any(string $route, mixed $handler, array $extraParameters = []): static

Check failure on line 65 in src/RouteCollectorAbstract.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 65 in src/RouteCollectorAbstract.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

The return type of FastRoute\RouteCollector#any() changed from void to static
{
return $this->addRoute('*', $route, $handler, $extraParameters);
}

/** @inheritDoc */
public function get(string $route, mixed $handler, array $extraParameters = []): static
{
return $this->addRoute('GET', $route, $handler, $extraParameters);
}

/** @inheritDoc */
public function post(string $route, mixed $handler, array $extraParameters = []): static
{
return $this->addRoute('POST', $route, $handler, $extraParameters);
}

/** @inheritDoc */
public function put(string $route, mixed $handler, array $extraParameters = []): static
{
return $this->addRoute('PUT', $route, $handler, $extraParameters);
}

/** @inheritDoc */
public function delete(string $route, mixed $handler, array $extraParameters = []): static
{
return $this->addRoute('DELETE', $route, $handler, $extraParameters);
}

/** @inheritDoc */
public function patch(string $route, mixed $handler, array $extraParameters = []): static
{
return $this->addRoute('PATCH', $route, $handler, $extraParameters);
}

/** @inheritDoc */
public function head(string $route, mixed $handler, array $extraParameters = []): static
{
return $this->addRoute('HEAD', $route, $handler, $extraParameters);
}

/** @inheritDoc */
public function options(string $route, mixed $handler, array $extraParameters = []): static
{
return $this->addRoute('OPTIONS', $route, $handler, $extraParameters);
}
}
46 changes: 46 additions & 0 deletions src/RouteCollectorImmutable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

namespace FastRoute;

use function array_key_exists;

final class RouteCollectorImmutable extends RouteCollectorAbstract
{
/** @inheritDoc */
public function addRoute(string|array $httpMethod, string $route, mixed $handler, array $extraParameters = []): static
{
$clone = clone $this;
$clone->dataGenerator = clone $clone->dataGenerator;

$route = $clone->currentGroupPrefix . $route;
$parsedRoutes = $clone->routeParser->parse($route);

$extraParameters = [self::ROUTE_REGEX => $route] + $extraParameters;

foreach ((array) $httpMethod as $method) {
foreach ($parsedRoutes as $parsedRoute) {
$clone->dataGenerator->addRoute($method, $parsedRoute, $handler, $extraParameters);
}
}

if (array_key_exists(self::ROUTE_NAME, $extraParameters)) {
$clone->registerNamedRoute($extraParameters[self::ROUTE_NAME], $parsedRoutes);
}

return $clone;
}

/** @inheritDoc */

Check failure on line 34 in src/RouteCollectorImmutable.php

View workflow job for this annotation

GitHub Actions / Check Coding Standards (locked, 8.1, ubuntu-latest)

Useless documentation comment with @inheritdoc.
public function addGroup(string $prefix, callable $callback): static
{
$clone = clone $this;

$previousGroupPrefix = $clone->currentGroupPrefix;
$clone->currentGroupPrefix = $previousGroupPrefix . $prefix;
$clone = $callback($clone);
$clone->currentGroupPrefix = $previousGroupPrefix;

return $clone;
}
}
Loading
Loading