Skip to content

Commit

Permalink
Extract interface for route configuration
Browse files Browse the repository at this point in the history
This introduces an extension point for providing different
implementations for the configuration of application routes.

Signed-off-by: Luís Cobucci <[email protected]>
  • Loading branch information
lcobucci committed Feb 6, 2024
1 parent f6e5f76 commit f09f14c
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 72 deletions.
107 changes: 107 additions & 0 deletions src/ConfigureRoutes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
declare(strict_types=1);

namespace FastRoute;

/**
* @phpstan-import-type RouteData from DataGenerator
* @phpstan-import-type ExtraParameters from DataGenerator
*/
interface ConfigureRoutes
{
/**
* Registers a new route.
*
* The syntax used in the $route string depends on the used route parser.
*
* @param string|string[] $httpMethod
* @param ExtraParameters $extraParameters
*/
public function addRoute(string|array $httpMethod, string $route, mixed $handler, array $extraParameters = []): void;

/**
* 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;

/**
* Adds a fallback route to the collection
*
* This is simply an alias of $this->addRoute('*', $route, $handler)
*
* @param ExtraParameters $extraParameters
*/
public function any(string $route, mixed $handler, array $extraParameters = []): void;

/**
* Adds a GET route to the collection
*
* This is simply an alias of $this->addRoute('GET', $route, $handler)
*
* @param ExtraParameters $extraParameters
*/
public function get(string $route, mixed $handler, array $extraParameters = []): void;

/**
* Adds a POST route to the collection
*
* This is simply an alias of $this->addRoute('POST', $route, $handler)
*
* @param ExtraParameters $extraParameters
*/
public function post(string $route, mixed $handler, array $extraParameters = []): void;

/**
* Adds a PUT route to the collection
*
* This is simply an alias of $this->addRoute('PUT', $route, $handler)
*
* @param ExtraParameters $extraParameters
*/
public function put(string $route, mixed $handler, array $extraParameters = []): void;

/**
* Adds a DELETE route to the collection
*
* This is simply an alias of $this->addRoute('DELETE', $route, $handler)
*
* @param ExtraParameters $extraParameters
*/
public function delete(string $route, mixed $handler, array $extraParameters = []): void;

/**
* Adds a PATCH route to the collection
*
* This is simply an alias of $this->addRoute('PATCH', $route, $handler)
*
* @param ExtraParameters $extraParameters
*/
public function patch(string $route, mixed $handler, array $extraParameters = []): void;

/**
* Adds a HEAD route to the collection
*
* This is simply an alias of $this->addRoute('HEAD', $route, $handler)
*
* @param ExtraParameters $extraParameters
*/
public function head(string $route, mixed $handler, array $extraParameters = []): void;

/**
* Adds an OPTIONS route to the collection
*
* This is simply an alias of $this->addRoute('OPTIONS', $route, $handler)
*
* @param ExtraParameters $extraParameters
*/
public function options(string $route, mixed $handler, array $extraParameters = []): void;

/**
* Returns the processed aggregated route data.
*
* @return RouteData
*/
public function processedRoutes(): array;
}
92 changes: 20 additions & 72 deletions src/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @phpstan-import-type ExtraParameters from DataGenerator
* @final
*/
class RouteCollector
class RouteCollector implements ConfigureRoutes
{
protected string $currentGroupPrefix = '';

Expand All @@ -18,14 +18,7 @@ public function __construct(
) {
}

/**
* Adds a route to the collection.
*
* The syntax used in the $route string depends on the used route parser.
*
* @param string|string[] $httpMethod
* @param ExtraParameters $extraParameters
*/
/** @inheritDoc */
public function addRoute(string|array $httpMethod, string $route, mixed $handler, array $extraParameters = []): void
{
$route = $this->currentGroupPrefix . $route;
Expand All @@ -40,11 +33,6 @@ public function addRoute(string|array $httpMethod, string $route, mixed $handler
}
}

/**
* 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
{
$previousGroupPrefix = $this->currentGroupPrefix;
Expand All @@ -53,109 +41,69 @@ public function addGroup(string $prefix, callable $callback): void
$this->currentGroupPrefix = $previousGroupPrefix;
}

/**
* Adds a fallback route to the collection
*
* This is simply an alias of $this->addRoute('*', $route, $handler)
*
* @param ExtraParameters $extraParameters
*/
/** @inheritDoc */
public function any(string $route, mixed $handler, array $extraParameters = []): void
{
$this->addRoute('*', $route, $handler, $extraParameters);
}

/**
* Adds a GET route to the collection
*
* This is simply an alias of $this->addRoute('GET', $route, $handler)
*
* @param ExtraParameters $extraParameters
*/
/** @inheritDoc */
public function get(string $route, mixed $handler, array $extraParameters = []): void
{
$this->addRoute('GET', $route, $handler, $extraParameters);
}

/**
* Adds a POST route to the collection
*
* This is simply an alias of $this->addRoute('POST', $route, $handler)
*
* @param ExtraParameters $extraParameters
*/
/** @inheritDoc */
public function post(string $route, mixed $handler, array $extraParameters = []): void
{
$this->addRoute('POST', $route, $handler, $extraParameters);
}

/**
* Adds a PUT route to the collection
*
* This is simply an alias of $this->addRoute('PUT', $route, $handler)
*
* @param ExtraParameters $extraParameters
*/
/** @inheritDoc */
public function put(string $route, mixed $handler, array $extraParameters = []): void
{
$this->addRoute('PUT', $route, $handler, $extraParameters);
}

/**
* Adds a DELETE route to the collection
*
* This is simply an alias of $this->addRoute('DELETE', $route, $handler)
*
* @param ExtraParameters $extraParameters
*/
/** @inheritDoc */
public function delete(string $route, mixed $handler, array $extraParameters = []): void
{
$this->addRoute('DELETE', $route, $handler, $extraParameters);
}

/**
* Adds a PATCH route to the collection
*
* This is simply an alias of $this->addRoute('PATCH', $route, $handler)
*
* @param ExtraParameters $extraParameters
*/
/** @inheritDoc */
public function patch(string $route, mixed $handler, array $extraParameters = []): void
{
$this->addRoute('PATCH', $route, $handler, $extraParameters);
}

/**
* Adds a HEAD route to the collection
*
* This is simply an alias of $this->addRoute('HEAD', $route, $handler)
*
* @param ExtraParameters $extraParameters
*/
/** @inheritDoc */
public function head(string $route, mixed $handler, array $extraParameters = []): void
{
$this->addRoute('HEAD', $route, $handler, $extraParameters);
}

/**
* Adds an OPTIONS route to the collection
*
* This is simply an alias of $this->addRoute('OPTIONS', $route, $handler)
*
* @param ExtraParameters $extraParameters
*/
/** @inheritDoc */
public function options(string $route, mixed $handler, array $extraParameters = []): void
{
$this->addRoute('OPTIONS', $route, $handler, $extraParameters);
}

/** @inheritDoc */
public function processedRoutes(): array
{
return $this->dataGenerator->getData();
}

/**
* Returns the collected route data, as provided by the data generator.
* @deprecated
*
* @see ConfigureRoutes::processedRoutes()
*
* @return RouteData
*/
public function getData(): array
{
return $this->dataGenerator->getData();
return $this->processedRoutes();
}
}

0 comments on commit f09f14c

Please sign in to comment.