Skip to content

Commit

Permalink
Move regex extraction to Route
Browse files Browse the repository at this point in the history
Ensuring this logic is reusable elsewhere.
  • Loading branch information
lcobucci committed Sep 19, 2023
1 parent ebac654 commit 396e842
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/BadRouteException.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static function shadowedByVariableRoute(string $route, string $shadowedRe
'Static route "%s" is shadowed by previously defined variable route "%s" for method "%s"',
$route,
$shadowedRegex,
$method
)
$method,
),
);
}

Expand Down
35 changes: 3 additions & 32 deletions src/DataGenerator/RegexBasedAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use function count;
use function is_string;
use function max;
use function preg_quote;
use function round;

// phpcs:ignore SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming.SuperfluousSuffix
Expand Down Expand Up @@ -103,41 +102,13 @@ private function addStaticRoute(string $httpMethod, array $routeData, mixed $han
/** @param array<string|array{0: string, 1:string}> $routeData */
private function addVariableRoute(string $httpMethod, array $routeData, mixed $handler): void
{
[$regex, $variables] = $this->buildRegexForRoute($routeData);
$route = Route::fromParsedRoute($httpMethod, $routeData, $handler);
$regex = $route->regex;

if (isset($this->methodToRegexToRoutesMap[$httpMethod][$regex])) {
throw BadRouteException::alreadyRegistered($regex, $httpMethod);
}

$this->methodToRegexToRoutesMap[$httpMethod][$regex] = new Route(
$httpMethod,
$handler,
$regex,
$variables,
);
}

/**
* @param array<string|array{0: string, 1:string}> $routeData
*
* @return array{0: string, 1: array<string, string>}
*/
private function buildRegexForRoute(array $routeData): array
{
$regex = '';
$variables = [];
foreach ($routeData as $part) {
if (is_string($part)) {
$regex .= preg_quote($part, '~');
continue;
}

[$varName, $regexPart] = $part;

$variables[$varName] = $varName;
$regex .= '(' . $regexPart . ')';
}

return [$regex, $variables];
$this->methodToRegexToRoutesMap[$httpMethod][$regex] = $route;
}
}
40 changes: 40 additions & 0 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,50 @@

namespace FastRoute;

use function is_string;
use function preg_match;
use function preg_quote;

class Route
{
/** @param array<string|array{0: string, 1:string}> $routeData */
public static function fromParsedRoute(string $httpMethod, array $routeData, mixed $handler): self
{
[$regex, $variables] = self::extractRegex($routeData);

return new self(
$httpMethod,
$handler,
$regex,
$variables,
);
}

/**
* @param array<string|array{0: string, 1:string}> $routeData
*
* @return array{0: string, 1: array<string, string>}
*/
private static function extractRegex(array $routeData): array
{
$regex = '';
$variables = [];

foreach ($routeData as $part) {
if (is_string($part)) {
$regex .= preg_quote($part, '~');
continue;
}

[$varName, $regexPart] = $part;

$variables[$varName] = $varName;
$regex .= '(' . $regexPart . ')';
}

return [$regex, $variables];
}

/** @param array<string, string> $variables */
public function __construct(public string $httpMethod, public mixed $handler, public string $regex, public array $variables)
{
Expand Down

0 comments on commit 396e842

Please sign in to comment.