10000 [Routing] Compiler: fix in the computing of the segment separators · symfony/symfony@8232aa1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8232aa1

Browse files
committed
[Routing] Compiler: fix in the computing of the segment separators
1 parent db1d145 commit 8232aa1

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/Symfony/Component/Routing/RouteCompiler.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class RouteCompiler implements RouteCompilerInterface
2626
* @param Route $route A Route instance
2727
*
2828
* @return CompiledRoute A CompiledRoute instance
29+
*
30+
* @throws \LogicException If a variable is referenced more than once
2931
*/
3032
public function compile(Route $route)
3133
{
@@ -34,22 +36,22 @@ public function compile(Route $route)
3436
$tokens = array();
3537
$variables = array();
3638
$pos = 0;
37-
preg_match_all('#.\{([\w\d_]+)\}#', $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
39+
preg_match_all('#.\{(\w+)\}#', $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
3840
foreach ($matches as $match) {
3941
if ($text = substr($pattern, $pos, $match[0][1] - $pos)) {
4042
$tokens[] = array('text', $text);
4143
}
42-
$seps = array($pattern[$pos]);
44+
4345
$pos = $match[0][1] + strlen($match[0][0]);
4446
$var = $match[1][0];
4547

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

5557
$tokens[] = array('variable', $match[0][0][0], $regexp, $var);

src/Symfony/Component/Routing/Tests/RouteCompilerTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ public function provideCompileData()
9696
'', '#^/(?<bar>(foo|bar))?$#s', array('bar'), array(
9797
array('variable', '/', '(foo|bar)', 'bar'),
9898
)),
99+
100+
array(
101+
'Route with a variable in last position',
102+
array('/foo-{bar}'),
103+
'/foo', '#^/foo\-(?<bar>[^\-]+?)$#s', array('bar'), array(
104+
array('variable', '-', '[^\-]+?', 'bar'),
105+
array('text', '/foo'),
106+
)),
107+
99108
);
100109
}
101110

0 commit comments

Comments
 (0)
0