Skip to content

[Routing] Allow using UTF-8 parameter names #45054

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

Merged
merged 1 commit into from
Jan 19, 2022
Merged
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.1
---

* Allow using UTF-8 parameter names

5.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ private function compileDynamicRoutes(RouteCollection $collection, bool $matchHo
if ($hasTrailingSlash = '/' !== $regex && '/' === $regex[-1]) {
$regex = substr($regex, 0, -1);
}
$hasTrailingVar = (bool) preg_match('#\{\w+\}/?$#', $route->getPath());
$hasTrailingVar = (bool) preg_match('#\{[\w\x80-\xFF]+\}/?$#', $route->getPath());

$tree->addRoute($regex, [$name, $regex, $state->vars, $route, $hasTrailingSlash, $hasTrailingVar]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected function matchCollection(string $pathinfo, RouteCollection $routes): a
continue;
}

$hasTrailingVar = $trimmedPathinfo !== $pathinfo && preg_match('#\{\w+\}/?$#', $route->getPath());
$hasTrailingVar = $trimmedPathinfo !== $pathinfo && preg_match('#\{[\w\x80-\xFF]+\}/?$#', $route->getPath());

if ($hasTrailingVar && ($hasTrailingSlash || (null === $m = $matches[\count($compiledRoute->getPathVariables())] ?? null) || '/' !== ($m[-1] ?? '/')) && preg_match($regex, $trimmedPathinfo, $m)) {
if ($hasTrailingSlash) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected function matchCollection(string $pathinfo, RouteCollection $routes): a
continue;
}

$hasTrailingVar = $trimmedPathinfo !== $pathinfo && preg_match('#\{\w+\}/?$#', $route->getPath());
$hasTrailingVar = $trimmedPathinfo !== $pathinfo && preg_match('#\{[\w\x80-\xFF]+\}/?$#', $route->getPath());

if ($hasTrailingVar && ($hasTrailingSlash || (null === $m = $matches[\count($compiledRoute->getPathVariables())] ?? null) || '/' !== ($m[-1] ?? '/')) && preg_match($regex, $trimmedPathinfo, $m)) {
if ($hasTrailingSlash) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ private function extractInlineDefaultsAndRequirements(string $pattern): string
return $pattern;
}

return preg_replace_callback('#\{(!?)(\w++)(<.*?>)?(\?[^\}]*+)?\}#', function ($m) {
return preg_replace_callback('#\{(!?)([\w\x80-\xFF]++)(<.*?>)?(\?[^\}]*+)?\}#', function ($m) {
if (isset($m[4][0])) {
$this->setDefault($m[2], '?' !== $m[4] ? substr($m[4], 1) : null);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Routing/RouteCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private static function compilePattern(Route $route, string $pattern, bool $isHo

// Match all variables enclosed in "{}" and iterate over them. But we only want to match the innermost variable
// in case of nested "{}", e.g. {foo{bar}}. This in ensured because \w does not match "{" or "}" itself.
preg_match_all('#\{(!)?(\w+)\}#', $pattern, $matches, \PREG_OFFSET_CAPTURE | \PREG_SET_ORDER);
preg_match_all('#\{(!)?([\w\x80-\xFF]+)\}#', $pattern, $matches, \PREG_OFFSET_CAPTURE | \PREG_SET_ORDER);
foreach ($matches as $match) {
$important = $match[1][1] >= 0;
$varName = $match[2][0];
Expand Down Expand Up @@ -170,7 +170,7 @@ private static function compilePattern(Route $route, string $pattern, bool $isHo
preg_quote($defaultSeparator),
$defaultSeparator !== $nextSeparator && '' !== $nextSeparator ? preg_quote($nextSeparator) : ''
);
if (('' !== $nextSeparator && !preg_match('#^\{\w+\}#', $followingPattern)) || '' === $followingPattern) {
if (('' !== $nextSeparator && !preg_match('#^\{[\w\x80-\xFF]+\}#', $followingPattern)) || '' === $followingPattern) {
// When we have a separator, which is disallowed for the variable, we can optimize the regex with a possessive
// quantifier. This prevents useless backtracking of PCRE and improves performance by 20% for matching those patterns.
// Given the above example, there is no point in backtracking into {page} (that forbids the dot) when a dot must follow
Expand Down Expand Up @@ -271,7 +271,7 @@ private static function findNextSeparator(string $pattern, bool $useUtf8): strin
return '';
}
// first remove all placeholders from the pattern so we can find the next real static character
if ('' === $pattern = preg_replace('#\{\w+\}#', '', $pattern)) {
if ('' === $pattern = preg_replace('#\{[\w\x80-\xFF]+\}#', '', $pattern)) {
return '';
}
if ($useUtf8) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,12 @@ public function provideLookAroundRequirementsInPath()
yield ['/app.php/bar/a/b/bam/c/d/e', '/bar/{foo}/bam/{baz}', '(?<!^).+'];
}

public function testUtf8VarName()
{
$routes = $this->getRoutes('test', new Route('/foo/{bär}', [], [], ['utf8' => true]));
$this->assertSame('/app.php/foo/baz', $this->getGenerator($routes)->generate('test', ['bär' => 'baz']));
}

protected function getGenerator(RouteCollection $routes, array $parameters = [], $logger = null, string $defaultLocale = null)
{
$context = new RequestContext('/app.php');
Expand Down
14 changes: 12 additions & 2 deletions src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -833,10 +833,10 @@ public function testSlashVariant()
public function testSlashVariant2()
{
$coll = new RouteCollection();
$coll->add('a', new Route('/foo/{bar}/', [], ['bar' => '.*']));
$coll->add('a', new Route('/foo/{bär}/', [], ['bär' => '.*'], ['utf8' => true]));

$matcher = $this->getUrlMatcher($coll);
$this->assertEquals(['_route' => 'a', 'bar' => 'bar'], $matcher->match('/foo/bar/'));
$this->assertEquals(['_route' => 'a', 'bär' => 'bar'], $matcher->match('/foo/bar/'));
}

public function testSlashWithVerb()
Expand Down Expand Up @@ -941,6 +941,16 @@ public function testRestrictiveTrailingRequirementWithStaticRouteAfter()
$this->assertEquals(['_route' => 'a', '_' => '/'], $matcher->match('/hello/'));
}

public function testUtf8VarName()
{
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo/{bär}/{bäz?foo}', [], [], ['utf8' => true]));

$matcher = $this->getUrlMatcher($collection);

$this->assertEquals(['_route' => 'foo', 'bär' => 'baz', 'bäz' => 'foo'], $matcher->match('/foo/baz'));
}

protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
{
return new UrlMatcher($routes, $context ?? new RequestContext());
Expand Down