Skip to content

Commit

Permalink
Allow for custom ConfigureRoutes implementations
Browse files Browse the repository at this point in the history
Also in FastRoute:

- rename $configuredRoutes and $routeConfiguration to $configureRoutes for consistency
- add configureRoutes() method to allow testing for the custom implementation
- rename buildConfiguration() to processedConfiguration(), and make public so utilities (such as a URL dumper) can examine the routes
- make RouteCollector non-final so it can be extended by consumers, e.g. to auto-add route names
  • Loading branch information
pmjones committed Mar 10, 2024
1 parent d3ada01 commit 03cb8ff
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
54 changes: 37 additions & 17 deletions src/FastRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class FastRoute
* @param class-string<RouteParser> $routeParser
* @param class-string<DataGenerator> $dataGenerator
* @param class-string<Dispatcher> $dispatcher
* @param class-string<ConfigureRoutes> $routesConfiguration
* @param class-string<ConfigureRoutes> $configureRoutes
* @param class-string<GenerateUri> $uriGenerator
* @param Cache|class-string<Cache>|null $cacheDriver
* @param non-empty-string|null $cacheKey
Expand All @@ -30,7 +30,7 @@ private function __construct(
private readonly string $routeParser,
private readonly string $dataGenerator,
private readonly string $dispatcher,
private readonly string $routesConfiguration,
private readonly string $configureRoutes,
private readonly string $uriGenerator,
private readonly Cache|string|null $cacheDriver,
private readonly ?string $cacheKey,
Expand Down Expand Up @@ -62,7 +62,7 @@ public function disableCache(): self
$this->routeParser,
$this->dataGenerator,
$this->dispatcher,
$this->routesConfiguration,
$this->configureRoutes,
$this->uriGenerator,
null,
null,
Expand All @@ -80,7 +80,7 @@ public function withCache(Cache|string $driver, string $cacheKey): self
$this->routeParser,
$this->dataGenerator,
$this->dispatcher,
$this->routesConfiguration,
$this->configureRoutes,
$this->uriGenerator,
$driver,
$cacheKey,
Expand Down Expand Up @@ -118,7 +118,24 @@ public function useCustomDispatcher(string $dataGenerator, string $dispatcher):
$this->routeParser,
$dataGenerator,
$dispatcher,
$this->routesConfiguration,
$this->configureRoutes,
$this->uriGenerator,
$this->cacheDriver,
$this->cacheKey,
);
}

/**
* @param class-string<ConfigureRoutes> $configureRoutes
*/
public function useCustomConfigureRoutes(string $configureRoutes): self
{
return new self(
$this->routeDefinitionCallback,
$this->routeParser,
$this->dataGenerator,
$this->dispatcher,
$configureRoutes,
$this->uriGenerator,
$this->cacheDriver,
$this->cacheKey,
Expand All @@ -133,29 +150,24 @@ public function withUriGenerator(string $uriGenerator): self
$this->routeParser,
$this->dataGenerator,
$this->dispatcher,
$this->routesConfiguration,
$this->configureRoutes,
$uriGenerator,
$this->cacheDriver,
$this->cacheKey,
);
}

/** @return ProcessedData */
private function buildConfiguration(): array
public function processedConfiguration(): array
{
if ($this->processedConfiguration !== null) {
return $this->processedConfiguration;
}

$loader = function (): array {
$configuredRoutes = new $this->routesConfiguration(
new $this->routeParser(),
new $this->dataGenerator(),
);

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

return $configuredRoutes->processedRoutes();
$configureRoutes = $this->configureRoutes();
($this->routeDefinitionCallback)($configureRoutes);
return $configureRoutes->processedRoutes();
};

if ($this->cacheDriver === null) {
Expand All @@ -171,13 +183,21 @@ private function buildConfiguration(): array
return $this->processedConfiguration = $cache->get($this->cacheKey, $loader);
}

public function configureRoutes(): ConfigureRoutes
{
return new $this->configureRoutes(
new $this->routeParser(),
new $this->dataGenerator(),
);
}

public function dispatcher(): Dispatcher
{
return new $this->dispatcher($this->buildConfiguration());
return new $this->dispatcher($this->processedConfiguration());
}

public function uriGenerator(): GenerateUri
{
return new $this->uriGenerator($this->buildConfiguration()[2]);
return new $this->uriGenerator($this->processedConfiguration()[2]);
}
}
1 change: 0 additions & 1 deletion src/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* @phpstan-import-type ExtraParameters from DataGenerator
* @phpstan-import-type RoutesForUriGeneration from GenerateUri
* @phpstan-import-type ParsedRoutes from RouteParser
* @final
*/
class RouteCollector implements ConfigureRoutes
{
Expand Down
10 changes: 10 additions & 0 deletions test/FakeRouteCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace FastRoute\Test;

use FastRoute\RouteCollector;

class FakeRouteCollector extends RouteCollector
{
}
11 changes: 11 additions & 0 deletions test/FastRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use FastRoute\Dispatcher;
use FastRoute\FastRoute;
use FastRoute\GenerateUri;
use FastRoute\RouteCollector;
use PHPUnit\Framework\Attributes as PHPUnit;
use PHPUnit\Framework\TestCase;
use RuntimeException;
Expand Down Expand Up @@ -110,6 +111,16 @@ public function defaultUriGeneratorMustBeProvided(): void
self::assertInstanceOf(GenerateUri\FromProcessedConfiguration::class, $uriGenerator);
}

#[PHPUnit\Test]
public function configureRoutesCanBeOverridden(): void
{
$configureRoutes = FastRoute::recommendedSettings(self::routes(...), 'test')
->useCustomConfigureRoutes(FakeRouteCollector::class)
->configureRoutes();

self::assertInstanceOf(FakeRouteCollector::class, $configureRoutes);
}

#[PHPUnit\Test]
public function uriGeneratorCanBeOverridden(): void
{
Expand Down

0 comments on commit 03cb8ff

Please sign in to comment.