Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Something like "real" DI, v2: Settings #283

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ConfigureRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,5 @@ public function options(string $route, mixed $handler, array $extraParameters =
*
* @return ProcessedData
*/
public function processedRoutes(): array;
public function processedRoutes(DataGenerator $dataGenerator): array;
}
4 changes: 4 additions & 0 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
use FastRoute\Dispatcher\Result\MethodNotAllowed;
use FastRoute\Dispatcher\Result\NotMatched;

/** @phpstan-import-type ParsedRoutes from RouteParser */
interface Dispatcher
{
public const NOT_FOUND = 0;
public const FOUND = 1;
public const METHOD_NOT_ALLOWED = 2;

/** @param ParsedRoutes $processedData */
public function with(array $processedData): self;

/**
* Dispatches against the provided HTTP method verb and URI.
*
Expand Down
8 changes: 6 additions & 2 deletions src/Dispatcher/RegexBasedAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ abstract class RegexBasedAbstract implements Dispatcher
protected array $variableRouteData = [];

/** @param RouteData $data */
public function __construct(array $data)
public function with(array $data): self
{
[$this->staticRouteMap, $this->variableRouteData] = $data;
$clone = clone $this;
$clone->staticRouteMap = $data[0];
$clone->variableRouteData = $data[1];

return $clone;
}

/** @param DynamicRouteChunks $routeData */
Expand Down
151 changes: 15 additions & 136 deletions src/FastRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace FastRoute;

use Closure;
use FastRoute\Cache\FileCache;

use function assert;
use function is_string;
Expand All @@ -15,129 +14,15 @@ final class FastRoute
/** @var ProcessedData|null */
private ?array $processedConfiguration = null;

/**
* @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,
) {
}

/**
* @param Closure(ConfigureRoutes):void $routeDefinitionCallback
* @param non-empty-string $cacheKey
*/
public static function recommendedSettings(Closure $routeDefinitionCallback, string $cacheKey): self
{
return new self(
$routeDefinitionCallback,
RouteParser\Std::class,
DataGenerator\MarkBased::class,
Dispatcher\MarkBased::class,
RouteCollector::class,
GenerateUri\FromProcessedConfiguration::class,
FileCache::class,
$cacheKey,
);
}

public function disableCache(): self
{
return new self(
$this->routeDefinitionCallback,
$this->routeParser,
$this->dataGenerator,
$this->dispatcher,
$this->routesConfiguration,
$this->uriGenerator,
null,
null,
);
}

/**
* @param Cache|class-string<Cache> $driver
* @param non-empty-string $cacheKey
*/
public function withCache(Cache|string $driver, string $cacheKey): self
{
return new self(
$this->routeDefinitionCallback,
$this->routeParser,
$this->dataGenerator,
$this->dispatcher,
$this->routesConfiguration,
$this->uriGenerator,
$driver,
$cacheKey,
);
}

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

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

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

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

/**
* @param class-string<DataGenerator> $dataGenerator
* @param class-string<Dispatcher> $dispatcher
* @param non-empty-string|null $cacheKey
*/
public function useCustomDispatcher(string $dataGenerator, string $dispatcher): self
{
return new self(
$this->routeDefinitionCallback,
$this->routeParser,
$dataGenerator,
$dispatcher,
$this->routesConfiguration,
$this->uriGenerator,
$this->cacheDriver,
$this->cacheKey,
);
}

/** @param class-string<GenerateUri> $uriGenerator */
public function withUriGenerator(string $uriGenerator): self
{
return new self(
$this->routeDefinitionCallback,
$this->routeParser,
$this->dataGenerator,
$this->dispatcher,
$this->routesConfiguration,
$uriGenerator,
$this->cacheDriver,
$this->cacheKey,
);
public function __construct(
private Closure $routeDefinitionCallback,
private ?string $cacheKey,
private Settings $settings = new FastSettings(),
) {
}

/** @return ProcessedData */
Expand All @@ -148,36 +33,30 @@ private function buildConfiguration(): array
}

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

($this->routeDefinitionCallback)($configuredRoutes);
$routesConfiguration = $this->settings->getRoutesConfiguration();
($this->routeDefinitionCallback)($routesConfiguration);

return $configuredRoutes->processedRoutes();
return $routesConfiguration->processedRoutes($this->settings->getDataGenerator());
};

if ($this->cacheDriver === null) {
$cacheDriver = $this->settings->getCacheDriver();

if ($cacheDriver === null) {
return $this->processedConfiguration = $loader();
}

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 = $cacheDriver->get($this->cacheKey, $loader);
}

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

public function uriGenerator(): GenerateUri
{
return new $this->uriGenerator($this->buildConfiguration()[2]);
return $this->settings->getUriGenerator()->with($this->buildConfiguration()[2]);
}
}
Loading