Skip to content

Commit d93649f

Browse files
committed
[Routing] Compile static routes when variables are fixed
1 parent cde44fc commit d93649f

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Symfony/Component/Routing/Matcher/Dumper/CompiledUrlMatcherDumper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ private function groupStaticRoutes(RouteCollection $collection): array
182182

183183
if (!$compiledRoute->getPathVariables()) {
184184
$host = !$compiledRoute->getHostVariables() ? $route->getHost() : '';
185-
$url = $route->getPath();
185+
$url = preg_replace_callback('#{([^}]+)}#', static function (array $matches) use ($route) {
186+
return $route->getRequirement($matches[1]);
187+
}, $route->getPath());
186188
if ($hasTrailingSlash) {
187189
$url = substr($url, 0, -1);
188190
}

src/Symfony/Component/Routing/RouteCompiler.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ private static function compilePattern(Route $route, string $pattern, bool $isHo
184184
$regexp = self::transformCapturingGroupsToNonCapturings($regexp);
185185
}
186186

187+
// Fixed variable, could be transformed as text token
188+
if (preg_quote($regexp, self::REGEX_DELIMITER) === $regexp) {
189+
$tokens[] = ['text', ($isSeparator ? $precedingChar : '').$regexp];
190+
continue;
191+
}
192+
187193
if ($important) {
188194
$token = ['variable', $isSeparator ? $precedingChar : '', $regexp, $varName, false, true];
189195
} else {
@@ -198,6 +204,8 @@ private static function compilePattern(Route $route, string $pattern, bool $isHo
198204
$tokens[] = ['text', substr($pattern, $pos)];
199205
}
200206

207+
$tokens = self::mergeContiguousTextTokens($tokens);
208+
201209
// find the first optional token
202210
$firstOptional = PHP_INT_MAX;
203211
if (!$isHost) {
@@ -237,6 +245,21 @@ private static function compilePattern(Route $route, string $pattern, bool $isHo
237245
];
238246
}
239247

248+
private static function mergeContiguousTextTokens(array $tokens): array
249+
{
250+
$mergedTokens = [$tokens[0]];
251+
for ($i = 1; $i < \count($tokens); ++$i) {
252+
if ('text' !== $tokens[$i][0] || 'variable' === end($mergedTokens)[0]) {
253+
$mergedTokens[] = $tokens[$i];
254+
continue;
255+
}
256+
257+
$mergedTokens[count($mergedTokens) - 1][1] .= $tokens[$i][1];
258+
}
259+
260+
return $mergedTokens;
261+
}
262+
240263
/**
241264
* Determines the longest static prefix possible for a route.
242265
*/

0 commit comments

Comments
 (0)