-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 GitHub Actions / Backwards compatibility check
Check failure on line 8 in src/RouteCollector.php GitHub Actions / Backwards compatibility check
Check failure on line 8 in src/RouteCollector.php GitHub Actions / Backwards compatibility check
|
||
{ | ||
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 GitHub Actions / Backwards compatibility check
|
||
{ | ||
|
@@ -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 GitHub Actions / Check Coding Standards (locked, 8.1, ubuntu-latest)
|
||
public function addGroup(string $prefix, callable $callback): static | ||
Check failure on line 32 in src/RouteCollector.php GitHub Actions / Backwards compatibility check
|
||
{ | ||
$previousGroupPrefix = $this->currentGroupPrefix; | ||
|
@@ -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(); | ||
} | ||
} |
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 | ||
{ | ||
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 GitHub Actions / Backwards compatibility check
|
||
{ | ||
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); | ||
} | ||
} |
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 GitHub Actions / Check Coding Standards (locked, 8.1, ubuntu-latest)
|
||
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; | ||
} | ||
} |