8000 [Routing] Fix the matching process by vicb · Pull Request #4170 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] Fix the matching process #4170

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 2 commits into from
May 7, 2012
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[Routing] Compiler: fix in the computing of the segment separators
  • Loading branch information
vicb committed May 1, 2012
commit 8232aa150b70572b1d378f202d886539946853a3
14 changes: 8 additions & 6 deletions src/Symfony/Component/Routing/RouteCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class RouteCompiler implements RouteCompilerInterface
* @param Route $route A Route instance
*
* @return CompiledRoute A CompiledRoute instance
*
* @throws \LogicException If a variable is referenced more than once
*/
public function compile(Route $route)
{
Expand All @@ -34,22 +36,22 @@ public function compile(Route $route)
$tokens = array();
$variables = array();
$pos = 0;
preg_match_all('#.\{([\w\d_]+)\}#', $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
preg_match_all('#.\{(\w+)\}#', $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
foreach ($matches as $match) {
if ($text = substr($pattern, $pos, $match[0][1] - $pos)) {
$tokens[] = array('text', $text);
}
$seps = array($pattern[$pos]);

$pos = $match[0][1] + strlen($match[0][0]);
$var = $match[1][0];

if ($req = $route->getRequirement($var)) {
$regexp = $req;
} else {
if ($pos !== $len) {
$seps[] = $pattern[$pos];
}
$regexp = sprintf('[^%s]+?', preg_quote(implode('', array_unique($seps)), self::REGEX_DELIMITER));
// Use the character following the variable as the separator when available
// Use the character preceding the variable otherwise
$separator = $pos !== $len ? $pattern[$pos] : $match[0][0][0];
$regexp = sprintf('[^%s]+?', preg_quote($separator, self::REGEX_DELIMITER));
}

$tokens[] = array('variable', $match[0][0][0], $regexp, $var);
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Routing/Tests/RouteCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ public function provideCompileData()
'', '#^/(?<bar>(foo|bar))?$#s', array('bar'), array(
array('variable', '/', '(foo|bar)', 'bar'),
)),

array(
'Route with a variable in last position',
array('/foo-{bar}'),
'/foo', '#^/foo\-(?<bar>[^\-]+?)$#s', array('bar'), array(
array('variable', '-', '[^\-]+?', 'bar'),
array('text', '/foo'),
)),

);
}

Expand Down
0