Skip to content

Commit

Permalink
Adopt extract interface for route configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Luís Cobucci <[email protected]>
  • Loading branch information
lcobucci committed Feb 6, 2024
1 parent f09f14c commit 0e4448b
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 66 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Here's a basic usage example:

require '/path/to/vendor/autoload.php';

$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\ConfigureRoutes $r) {
$r->addRoute('GET', '/users', 'get_all_users_handler');
// {id} must be a number (\d+)
$r->addRoute('GET', '/user/{id:\d+}', 'get_user_handler');
Expand Down Expand Up @@ -65,7 +65,7 @@ switch ($routeInfo[0]) {
### Defining routes

The routes are defined by calling the `FastRoute\simpleDispatcher()` function, which accepts
a callable taking a `FastRoute\RouteCollector` instance. The routes are added by calling
a callable taking a `FastRoute\ConfigureRoutes` instance. The routes are added by calling
`addRoute()` on the collector instance:

```php
Expand Down Expand Up @@ -147,7 +147,7 @@ Additionally, you can specify routes inside a group. All routes defined inside a
For example, defining your routes as:

```php
$r->addGroup('/admin', function (FastRoute\RouteCollector $r) {
$r->addGroup('/admin', function (FastRoute\ConfigureRoutes $r) {
$r->addRoute('GET', '/do-something', 'handler');
$r->addRoute('GET', '/do-another-thing', 'handler');
$r->addRoute('GET', '/do-something-else', 'handler');
Expand All @@ -173,7 +173,7 @@ routing data and construct the dispatcher from the cached information:
```php
<?php

$dispatcher = FastRoute\cachedDispatcher(function(FastRoute\RouteCollector $r) {
$dispatcher = FastRoute\cachedDispatcher(function(FastRoute\ConfigureRoutes $r) {
$r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0');
$r->addRoute('GET', '/user/{id:[0-9]+}', 'handler1');
$r->addRoute('GET', '/user/{name}', 'handler2');
Expand Down Expand Up @@ -275,7 +275,7 @@ through the options array:
```php
<?php

$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\ConfigureRoutes $r) {
/* ... */
}, [
'routeParser' => 'FastRoute\\RouteParser\\Std',
Expand Down
6 changes: 3 additions & 3 deletions benchmark/DispatcherForBenchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace FastRoute\Benchmark;

use FastRoute\ConfigureRoutes;
use FastRoute\DataGenerator;
use FastRoute\Dispatcher;
use FastRoute\RouteCollector;
use RuntimeException;

use function FastRoute\simpleDispatcher;
Expand All @@ -17,7 +17,7 @@ final class DispatcherForBenchmark
public static function realLifeExample(string $dispatcher): Dispatcher
{
return simpleDispatcher(
static function (RouteCollector $routes): void {
static function (ConfigureRoutes $routes): void {
$routes->addRoute('GET', '/', ['name' => 'home']);
$routes->addRoute('GET', '/page/{page_slug:[a-zA-Z0-9\-]+}', ['name' => 'page.show']);
$routes->addRoute('GET', '/about-us', ['name' => 'about-us']);
Expand Down Expand Up @@ -68,7 +68,7 @@ static function (RouteCollector $routes): void {
public static function manyRoutes(string $dispatcher, int $routeCount = 400): Dispatcher
{
return simpleDispatcher(
static function (RouteCollector $routes) use ($routeCount): void {
static function (ConfigureRoutes $routes) use ($routeCount): void {
for ($i = 0; $i < $routeCount; ++$i) {
$routes->addRoute('GET', '/abc' . $i, ['name' => 'static-' . $i]);
$routes->addRoute('GET', '/abc{foo}/' . $i, ['name' => 'not-static-' . $i]);
Expand Down
20 changes: 10 additions & 10 deletions src/FastRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@
final class FastRoute
{
/**
* @param Closure(RouteCollector):void $routeDefinitionCallback
* @param Closure(ConfigureRoutes):void $routeDefinitionCallback
* @param class-string<RouteParser> $routeParser
* @param class-string<DataGenerator> $dataGenerator
* @param class-string<Dispatcher> $dispatcher
* @param class-string<RouteCollector> $routeCollector
* @param class-string<ConfigureRoutes> $routesConfiguration
* @param Cache|class-string<Cache>|null $cacheDriver
*/
private function __construct(
private readonly Closure $routeDefinitionCallback,
private readonly string $routeParser,
private readonly string $dataGenerator,
private readonly string $dispatcher,
private readonly string $routeCollector,
private readonly string $routesConfiguration,
private readonly Cache|string|null $cacheDriver,
) {
}

/** @param Closure(RouteCollector):void $routeDefinitionCallback */
/** @param Closure(ConfigureRoutes):void $routeDefinitionCallback */
public static function recommendedSettings(Closure $routeDefinitionCallback): self
{
return new self(
Expand All @@ -48,7 +48,7 @@ public function disableCache(): self
$this->routeParser,
$this->dataGenerator,
$this->dispatcher,
$this->routeCollector,
$this->routesConfiguration,
null,
);
}
Expand All @@ -61,7 +61,7 @@ public function withCache(Cache|string $driver): self
$this->routeParser,
$this->dataGenerator,
$this->dispatcher,
$this->routeCollector,
$this->routesConfiguration,
$driver,
);
}
Expand Down Expand Up @@ -97,22 +97,22 @@ public function useCustomDispatcher(string $dataGenerator, string $dispatcher):
$this->routeParser,
$dataGenerator,
$dispatcher,
$this->routeCollector,
$this->routesConfiguration,
$this->cacheDriver,
);
}

public function dispatcher(string $cacheKey): Dispatcher
{
$loader = function (): array {
$collector = new $this->routeCollector(
$configuredRoutes = new $this->routesConfiguration(
new $this->routeParser(),
new $this->dataGenerator(),
);

($this->routeDefinitionCallback)($collector);
($this->routeDefinitionCallback)($configuredRoutes);

return $collector->getData();
return $configuredRoutes->processedRoutes();
};

if ($this->cacheDriver === null) {
Expand Down
8 changes: 5 additions & 3 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
* @see FastRoute::recommendedSettings()
* @see FastRoute::disableCache()
*
* @param array{routeParser?: class-string<RouteParser>, dataGenerator?: class-string<DataGenerator>, dispatcher?: class-string<Dispatcher>, routeCollector?: class-string<RouteCollector>, cacheDisabled?: bool, cacheKey?: string, cacheFile?: string, cacheDriver?: class-string<Cache>|Cache} $options
* @param callable(ConfigureRoutes):void $routeDefinitionCallback
* @param array{routeParser?: class-string<RouteParser>, dataGenerator?: class-string<DataGenerator>, dispatcher?: class-string<Dispatcher>, routeCollector?: class-string<ConfigureRoutes>, cacheDisabled?: bool, cacheKey?: string, cacheFile?: string, cacheDriver?: class-string<Cache>|Cache} $options
*/
function simpleDispatcher(callable $routeDefinitionCallback, array $options = []): Dispatcher
{
Expand All @@ -31,7 +32,8 @@ function simpleDispatcher(callable $routeDefinitionCallback, array $options = []
*
* @see FastRoute::recommendedSettings()
*
* @param array{routeParser?: class-string<RouteParser>, dataGenerator?: class-string<DataGenerator>, dispatcher?: class-string<Dispatcher>, routeCollector?: class-string<RouteCollector>, cacheDisabled?: bool, cacheKey?: string, cacheFile?: string, cacheDriver?: class-string<Cache>|Cache} $options
* @param callable(ConfigureRoutes):void $routeDefinitionCallback
* @param array{routeParser?: class-string<RouteParser>, dataGenerator?: class-string<DataGenerator>, dispatcher?: class-string<Dispatcher>, routeCollector?: class-string<ConfigureRoutes>, cacheDisabled?: bool, cacheKey?: string, cacheFile?: string, cacheDriver?: class-string<Cache>|Cache} $options
*/
function cachedDispatcher(callable $routeDefinitionCallback, array $options = []): Dispatcher
{
Expand All @@ -52,7 +54,7 @@ function cachedDispatcher(callable $routeDefinitionCallback, array $options = []

$routeDefinitionCallback($routeCollector);

return $routeCollector->getData();
return $routeCollector->processedRoutes();
};

if ($options['cacheDisabled'] === true) {
Expand Down
4 changes: 2 additions & 2 deletions test/Dispatcher/CachingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

namespace FastRoute\Test\Dispatcher;

use FastRoute\ConfigureRoutes;
use FastRoute\Dispatcher;
use FastRoute\RouteCollector;
use PHPUnit\Framework\Attributes as PHPUnit;
use PHPUnit\Framework\TestCase;

Expand All @@ -30,7 +30,7 @@ public function warmUpCache(): void
public function createDispatcher(string $optionName = 'cacheKey'): Dispatcher
{
return cachedDispatcher(
static function (RouteCollector $collector): void {
static function (ConfigureRoutes $collector): void {
$collector->get('/testing', ['test']);
$collector->get('/admin/{page}', ['admin-page']);
},
Expand Down
Loading

0 comments on commit 0e4448b

Please sign in to comment.