[Routing] Compiler: fix in the computing of the segment separators
This commit is contained in:
parent
db1d145b35
commit
8232aa150b
@ -26,6 +26,8 @@ class RouteCompiler implements RouteCompilerInterface
|
|||||||
* @param Route $route A Route instance
|
* @param Route $route A Route instance
|
||||||
*
|
*
|
||||||
* @return CompiledRoute A CompiledRoute instance
|
* @return CompiledRoute A CompiledRoute instance
|
||||||
|
*
|
||||||
|
* @throws \LogicException If a variable is referenced more than once
|
||||||
*/
|
*/
|
||||||
public function compile(Route $route)
|
public function compile(Route $route)
|
||||||
{
|
{
|
||||||
@ -34,22 +36,22 @@ class RouteCompiler implements RouteCompilerInterface
|
|||||||
$tokens = array();
|
$tokens = array();
|
||||||
$variables = array();
|
$variables = array();
|
||||||
$pos = 0;
|
$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) {
|
foreach ($matches as $match) {
|
||||||
if ($text = substr($pattern, $pos, $match[0][1] - $pos)) {
|
if ($text = substr($pattern, $pos, $match[0][1] - $pos)) {
|
||||||
$tokens[] = array('text', $text);
|
$tokens[] = array('text', $text);
|
||||||
}
|
}
|
||||||
$seps = array($pattern[$pos]);
|
|
||||||
$pos = $match[0][1] + strlen($match[0][0]);
|
$pos = $match[0][1] + strlen($match[0][0]);
|
||||||
$var = $match[1][0];
|
$var = $match[1][0];
|
||||||
|
|
||||||
if ($req = $route->getRequirement($var)) {
|
if ($req = $route->getRequirement($var)) {
|
||||||
$regexp = $req;
|
$regexp = $req;
|
||||||
} else {
|
} else {
|
||||||
if ($pos !== $len) {
|
// Use the character following the variable as the separator when available
|
||||||
$seps[] = $pattern[$pos];
|
// Use the character preceding the variable otherwise
|
||||||
}
|
$separator = $pos !== $len ? $pattern[$pos] : $match[0][0][0];
|
||||||
$regexp = sprintf('[^%s]+?', preg_quote(implode('', array_unique($seps)), self::REGEX_DELIMITER));
|
$regexp = sprintf('[^%s]+?', preg_quote($separator, self::REGEX_DELIMITER));
|
||||||
}
|
}
|
||||||
|
|
||||||
$tokens[] = array('variable', $match[0][0][0], $regexp, $var);
|
$tokens[] = array('variable', $match[0][0][0], $regexp, $var);
|
||||||
|
@ -96,6 +96,15 @@ class RouteCompilerTest extends \PHPUnit_Framework_TestCase
|
|||||||
'', '#^/(?<bar>(foo|bar))?$#s', array('bar'), array(
|
'', '#^/(?<bar>(foo|bar))?$#s', array('bar'), array(
|
||||||
array('variable', '/', '(foo|bar)', 'bar'),
|
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'),
|
||||||
|
)),
|
||||||
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user