Skip to content

Commit

Permalink
Enable something more like 'real' dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
pmjones committed Mar 11, 2024
1 parent bd9b290 commit fcc6278
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 45 deletions.
69 changes: 25 additions & 44 deletions src/FastRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,21 @@ final class FastRoute

/**
* @param Closure(ConfigureRoutes):void $routeDefinitionCallback
* @param class-string<RouteParser> $routeParser
* @param class-string<DataGenerator> $dataGenerator
* @param class-string<Dispatcher> $dispatcher
* @param class-string<ConfigureRoutes> $routesConfiguration
* @param class-string<GenerateUri> $uriGenerator
* @param Cache|class-string<Cache>|null $cacheDriver
* @param non-empty-string|null $cacheKey
*/
private function __construct(
private readonly Closure $routeDefinitionCallback,
private readonly string $routeParser,
private readonly string $dataGenerator,
private readonly string $dispatcher,
private readonly string $routesConfiguration,
private readonly string $uriGenerator,
private readonly Cache|string|null $cacheDriver,
private readonly ?string $cacheKey,
public function __construct(
private Closure $routeDefinitionCallback,
private DataGenerator $dataGenerator,
private Dispatcher $dispatcher,
private ConfigureRoutes $routesConfiguration,
private GenerateUri $uriGenerator,
private ?Cache $cacheDriver,
private ?string $cacheKey,
) {
}

Expand All @@ -45,12 +43,11 @@ public static function recommendedSettings(Closure $routeDefinitionCallback, str
{
return new self(
$routeDefinitionCallback,
RouteParser\Std::class,
DataGenerator\MarkBased::class,
Dispatcher\MarkBased::class,
RouteCollector::class,
GenerateUri\FromProcessedConfiguration::class,
FileCache::class,
new DataGenerator\MarkBased(),
new Dispatcher\MarkBased(),
new RouteCollector(new RouteParser\Std()),
new GenerateUri\FromProcessedConfiguration,
new FileCache(),
$cacheKey,
);
}
Expand All @@ -59,7 +56,6 @@ public function disableCache(): self
{
return new self(
$this->routeDefinitionCallback,
$this->routeParser,
$this->dataGenerator,
$this->dispatcher,
$this->routesConfiguration,
Expand All @@ -73,11 +69,10 @@ public function disableCache(): self
* @param Cache|class-string<Cache> $driver
* @param non-empty-string $cacheKey
*/
public function withCache(Cache|string $driver, string $cacheKey): self
public function withCache(Cache $driver, string $cacheKey): self
{
return new self(
$this->routeDefinitionCallback,
$this->routeParser,
$this->dataGenerator,
$this->dispatcher,
$this->routesConfiguration,
Expand All @@ -89,33 +84,32 @@ public function withCache(Cache|string $driver, string $cacheKey): self

public function useCharCountDispatcher(): self
{
return $this->useCustomDispatcher(DataGenerator\CharCountBased::class, Dispatcher\CharCountBased::class);
return $this->useCustomDispatcher(new DataGenerator\CharCountBased(), new Dispatcher\CharCountBased());
}

public function useGroupCountDispatcher(): self
{
return $this->useCustomDispatcher(DataGenerator\GroupCountBased::class, Dispatcher\GroupCountBased::class);
return $this->useCustomDispatcher(new DataGenerator\GroupCountBased(), new Dispatcher\GroupCountBased());
}

public function useGroupPosDispatcher(): self
{
return $this->useCustomDispatcher(DataGenerator\GroupPosBased::class, Dispatcher\GroupPosBased::class);
return $this->useCustomDispatcher(new DataGenerator\GroupPosBased(), new Dispatcher\GroupPosBased());
}

public function useMarkDispatcher(): self
{
return $this->useCustomDispatcher(DataGenerator\MarkBased::class, Dispatcher\MarkBased::class);
return $this->useCustomDispatcher(new DataGenerator\MarkBased(), new Dispatcher\MarkBased());
}

/**
* @param class-string<DataGenerator> $dataGenerator
* @param class-string<Dispatcher> $dispatcher
*/
public function useCustomDispatcher(string $dataGenerator, string $dispatcher): self
public function useCustomDispatcher(DataGenerator $dataGenerator, Dispatcher $dispatcher): self
{
return new self(
$this->routeDefinitionCallback,
$this->routeParser,
$dataGenerator,
$dispatcher,
$this->routesConfiguration,
Expand All @@ -126,11 +120,10 @@ public function useCustomDispatcher(string $dataGenerator, string $dispatcher):
}

/** @param class-string<GenerateUri> $uriGenerator */
public function withUriGenerator(string $uriGenerator): self
public function withUriGenerator(GenerateUri $uriGenerator): self
{
return new self(
$this->routeDefinitionCallback,
$this->routeParser,
$this->dataGenerator,
$this->dispatcher,
$this->routesConfiguration,
Expand All @@ -148,14 +141,8 @@ private function buildConfiguration(): array
}

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

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

$dataGenerator = new $this->dataGenerator();
return $configuredRoutes->processedRoutes($dataGenerator);
($this->routeDefinitionCallback)($this->routesConfiguration);
return $this->routesConfiguration->processedRoutes($this->dataGenerator);
};

if ($this->cacheDriver === null) {
Expand All @@ -164,22 +151,16 @@ private function buildConfiguration(): array

assert(is_string($this->cacheKey));

$cache = is_string($this->cacheDriver)
? new $this->cacheDriver()
: $this->cacheDriver;

return $this->processedConfiguration = $cache->get($this->cacheKey, $loader);
return $this->processedConfiguration = $this->cacheDriver->get($this->cacheKey, $loader);
}

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

public function uriGenerator(): GenerateUri
{
$uriGenerator = new $this->uriGenerator();
return $uriGenerator->with($this->buildConfiguration()[2]);
return $this->uriGenerator->with($this->buildConfiguration()[2]);
}
}
2 changes: 1 addition & 1 deletion test/FastRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function with(array $processedConfiguration): self

$uriGenerator = FastRoute::recommendedSettings(self::routes(...), 'test')
->disableCache()
->withUriGenerator($generator::class)
->withUriGenerator($generator)
->uriGenerator();

self::assertInstanceOf($generator::class, $uriGenerator);
Expand Down

0 comments on commit fcc6278

Please sign in to comment.