Skip to content

[Router] added \Z as regex end for route requirement #37714

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CHANGELOG
-----

* Added support for inline definition of requirements and defaults for host
* Added support for `\A` and `\z` as regex start and end for route requirement
* Added support for `\A` as regex start and `\Z` or `\z` as end for route requirement

5.1.0
-----
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 @@ -563,7 +563,7 @@ private function sanitizeRequirement(string $key, string $regex)

if ('$' === substr($regex, -1)) {
$regex = substr($regex, 0, -1);
} elseif (\strlen($regex) - 2 === strpos($regex, '\\z')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to the PR: couldn't this be written as this?
} elseif (false !== strpos($regex, '\\z', -2)) {

Copy link
Contributor Author

@zlodes zlodes Aug 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas tried strpos and strrpos:

strpos(): Offset not contained in string in .... (strpos doesn't accept negative offset)
strrpos(): Offset is greater than the length of haystack string

} elseif (\in_array(\strlen($regex) - 2, [strpos($regex, '\\z'), strpos($regex, '\\Z')], true)) {
$regex = substr($regex, 0, -2);
}

Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Routing/Tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public function testRequirementAlternativeStartAndEndRegexSyntax()
$route->setRequirement('foo', '\A\d+\z');
$this->assertEquals('\d+', $route->getRequirement('foo'), '->setRequirement() removes \A and \z from the path');
$this->assertTrue($route->hasRequirement('foo'));

$route->setRequirement('bar', '\A\d+\Z');
$this->assertEquals('\d+', $route->getRequirement('bar'), '->setRequirement() removes \A and \Z from the path');
$this->assertTrue($route->hasRequirement('bar'));
}

/**
Expand All @@ -147,8 +151,10 @@ public function getInvalidRequirements()
['^$'],
['^'],
['$'],
['\A\Z'],
['\A\z'],
['\A'],
['\Z'],
['\z'],
];
}
Expand Down