Skip to content

Commit

Permalink
feat (#30): custom route matcher support
Browse files Browse the repository at this point in the history
  • Loading branch information
divineniiquaye authored Mar 12, 2022
2 parents e3faac0 + 75e0f11 commit 545dab3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* [BC Break] Refactored route matching algorithm
* [BC Break] Updated and Renamed `RouteCollector` class to `RouteCollection`
* Added benchmark to compare performance over time
* Added custom route matcher support to the router class
* Updated routes cache from serialization to var_export
* Updated PHP minimum version to 7.4 and added PHP 8.1 support
* Updated README file with documentation of new changes
Expand Down
18 changes: 15 additions & 3 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Router implements RouteMatcherInterface, RequestMethodInterface, Middlewar
private \SplQueue $pipeline;
private ?RouteCompilerInterface $compiler;
private ?RouteMatcherInterface $matcher = null;
private string $matcherClass = RouteMatcher::class;

/** @var array<string,array<int,MiddlewareInterface>> */
private array $middlewares = [];
Expand Down Expand Up @@ -187,12 +188,23 @@ public function isCached(): bool
return ($cache instanceof CacheItemPoolInterface && $cache->hasItem(__FILE__)) || \file_exists($cache);
}

/**
* Set a matcher class associated with this Router.
*/
public function setMatcher(string $matcherClass): void
{
if (!\is_subclass_of($matcherClass, RouteMatcherInterface::class)) {
throw new \InvalidArgumentException(\sprintf('"%s" must be a subclass of "%s".', $matcherClass, RouteMatcherInterface::class));
}
$this->matcherClass = $matcherClass;
}

/**
* Gets the Route matcher instance associated with this Router.
*/
public function getMatcher(): RouteMatcherInterface
{
return $this->matcher ??= $this->cacheData ? $this->getCachedData($this->cacheData) : new RouteMatcher($this->getCollection(), $this->compiler);
return $this->matcher ??= $this->cacheData ? $this->getCachedData($this->cacheData) : new $this->matcherClass($this->getCollection(), $this->compiler);
}

/**
Expand Down Expand Up @@ -223,7 +235,7 @@ protected function getCachedData($cache): RouteMatcherInterface

if (!$cachedData instanceof RouteMatcherInterface) {
$cache->deleteItem(__FILE__);
$cache->save($cacheItem->set($cachedData = new RouteMatcher($this->getCollection(), $this->compiler)));
$cache->save($cacheItem->set($cachedData = new $this->matcherClass($this->getCollection(), $this->compiler)));
}

return $cacheItem->get();
Expand All @@ -232,7 +244,7 @@ protected function getCachedData($cache): RouteMatcherInterface
$cachedData = @include $cache;

if (!$cachedData instanceof RouteMatcherInterface) {
$dumpData = "<<<'SERIALIZED'\n" . \serialize(new RouteMatcher($this->getCollection(), $this->compiler)) . "\nSERIALIZED";
$dumpData = "<<<'SERIALIZED'\n" . \serialize(new $this->matcherClass($this->getCollection(), $this->compiler)) . "\nSERIALIZED";

if (!\is_dir($directory = \dirname($cache))) {
@\mkdir($directory, 0775, true);
Expand Down

0 comments on commit 545dab3

Please sign in to comment.