Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[Routing] Compiler: fix in the computing of the segment separators

  • Loading branch information...
commit 8232aa150b70572b1d378f202d886539946853a3 1 parent db1d145
@vicb vicb authored
View
14 src/Symfony/Component/Routing/RouteCompiler.php
@@ -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)
{
@@ -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);
View
9 src/Symfony/Component/Routing/Tests/RouteCompilerTest.php
@@ -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'),
+ )),
+
);
}
Please sign in to comment.
Something went wrong with that request. Please try again.