-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(routing): split route construction (#666)
- Loading branch information
1 parent
7fdff1d
commit 32bf4d0
Showing
10 changed files
with
324 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/Tempest/Http/src/Routing/Construction/RouteConfigurator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Http\Routing\Construction; | ||
|
||
use Tempest\Container\Singleton; | ||
use Tempest\Http\Route; | ||
use Tempest\Http\RouteConfig; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
#[Singleton] | ||
final class RouteConfigurator | ||
{ | ||
/** @var string The mark to give the next route in the matching Regex */ | ||
private string $regexMark = 'a'; | ||
|
||
/** @var array<string, array<string, Route>> */ | ||
private array $staticRoutes = []; | ||
|
||
/** @var array<string, array<string, Route>> */ | ||
private array $dynamicRoutes = []; | ||
|
||
private bool $isDirty = false; | ||
|
||
private RoutingTree $routingTree; | ||
|
||
public function __construct() | ||
{ | ||
|
||
$this->routingTree = new RoutingTree(); | ||
} | ||
|
||
public function addRoute(Route $route): void | ||
{ | ||
$this->isDirty = true; | ||
|
||
if ($route->isDynamic) { | ||
$this->addDynamicRoute($route); | ||
} else { | ||
$this->addStaticRoute($route); | ||
} | ||
} | ||
|
||
private function addDynamicRoute(Route $route): void | ||
{ | ||
$markedRoute = $this->markRoute($route); | ||
$this->dynamicRoutes[$route->method->value][$markedRoute->mark] = $route; | ||
|
||
$this->routingTree->add($markedRoute); | ||
} | ||
|
||
private function addStaticRoute(Route $route): void | ||
{ | ||
$uriWithTrailingSlash = rtrim($route->uri, '/'); | ||
|
||
$this->staticRoutes[$route->method->value][$uriWithTrailingSlash] = $route; | ||
$this->staticRoutes[$route->method->value][$uriWithTrailingSlash . '/'] = $route; | ||
} | ||
|
||
private function markRoute(Route $route): MarkedRoute | ||
{ | ||
$this->regexMark = str_increment($this->regexMark); | ||
|
||
return new MarkedRoute( | ||
mark: $this->regexMark, | ||
route: $route, | ||
); | ||
} | ||
|
||
public function toRouteConfig(): RouteConfig | ||
{ | ||
$this->isDirty = false; | ||
|
||
return new RouteConfig( | ||
$this->staticRoutes, | ||
$this->dynamicRoutes, | ||
$this->routingTree->toMatchingRegexes(), | ||
); | ||
} | ||
|
||
public function isDirty(): bool | ||
{ | ||
return $this->isDirty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.