Skip to content

Commit

Permalink
Added RouteCollectorImmutable
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Apr 22, 2024
1 parent 317f146 commit 3346f11
Show file tree
Hide file tree
Showing 4 changed files with 349 additions and 104 deletions.
106 changes: 2 additions & 104 deletions src/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,9 @@
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 = []): 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
{
Expand All @@ -48,20 +28,7 @@ public function addRoute(string|array $httpMethod, string $route, mixed $handler
return $this;
}

/** @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);
}

/** @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;
Expand All @@ -71,73 +38,4 @@ public function addGroup(string $prefix, callable $callback): static

return $this;
}

/** @inheritDoc */
public function any(string $route, mixed $handler, array $extraParameters = []): 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);
}

/** @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();
}
}
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

0 comments on commit 3346f11

Please sign in to comment.