diff --git a/src/FastRoute.php b/src/FastRoute.php index 6187833..dc3dfeb 100644 --- a/src/FastRoute.php +++ b/src/FastRoute.php @@ -20,7 +20,7 @@ final class FastRoute * @param class-string $routeParser * @param class-string $dataGenerator * @param class-string $dispatcher - * @param class-string $routesConfiguration + * @param class-string $configureRoutes * @param class-string $uriGenerator * @param Cache|class-string|null $cacheDriver * @param non-empty-string|null $cacheKey @@ -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, @@ -62,7 +62,7 @@ public function disableCache(): self $this->routeParser, $this->dataGenerator, $this->dispatcher, - $this->routesConfiguration, + $this->configureRoutes, $this->uriGenerator, null, null, @@ -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, @@ -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 + */ + public function useCustomConfigureRoutes(string $configureRoutes): self + { + return new self( + $this->routeDefinitionCallback, + $this->routeParser, + $this->dataGenerator, + $this->dispatcher, + $configureRoutes, $this->uriGenerator, $this->cacheDriver, $this->cacheKey, @@ -133,7 +150,7 @@ public function withUriGenerator(string $uriGenerator): self $this->routeParser, $this->dataGenerator, $this->dispatcher, - $this->routesConfiguration, + $this->configureRoutes, $uriGenerator, $this->cacheDriver, $this->cacheKey, @@ -141,21 +158,16 @@ public function withUriGenerator(string $uriGenerator): self } /** @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) { @@ -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]); } } diff --git a/src/RouteCollector.php b/src/RouteCollector.php index 9de94b6..1a530fa 100644 --- a/src/RouteCollector.php +++ b/src/RouteCollector.php @@ -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 { diff --git a/test/FakeRouteCollector.php b/test/FakeRouteCollector.php new file mode 100644 index 0000000..83a35f3 --- /dev/null +++ b/test/FakeRouteCollector.php @@ -0,0 +1,10 @@ +useCustomConfigureRoutes(FakeRouteCollector::class) + ->configureRoutes(); + + self::assertInstanceOf(FakeRouteCollector::class, $configureRoutes); + } + #[PHPUnit\Test] public function uriGeneratorCanBeOverridden(): void {