[Routing] disallow numeric named variables in pattern

because PHP raises an error for such subpatterns in PCRE and thus would break matching, e.g. '(?<123>.+)'.
This commit is contained in:
Tobias Schultze 2012-09-04 07:10:18 +02:00
parent c0673d77d0
commit be28e56f4e
2 changed files with 26 additions and 2 deletions

View File

@ -23,8 +23,10 @@ class RouteCompiler implements RouteCompilerInterface
/**
* {@inheritDoc}
*
* @throws \LogicException If a variable is referenced more than once or if a required variable
* has a default value that doesn't meet its own requirement.
* @throws \LogicException If a variable is referenced more than once or if a required variable
* has a default value that doesn't meet its own requirement.
* @throws \DomainException If a variable name is numeric because PHP raises an error for such
* subpatterns in PCRE and thus would break matching, e.g. "(?<123>.+)".
*/
public function compile(Route $route)
{
@ -57,6 +59,9 @@ class RouteCompiler implements RouteCompilerInterface
$tokens[] = array('variable', $match[0][0][0], $regexp, $var);
if (is_numeric($var)) {
throw new \DomainException(sprintf('Variable name "%s" cannot be numeric in route pattern "%s". Please use a different name.', $var, $route->getPattern()));
}
if (in_array($var, $variables)) {
throw new \LogicException(sprintf('Route pattern "%s" cannot reference variable name "%s" more than once.', $route->getPattern(), $var));
}

View File

@ -144,4 +144,23 @@ class RouteCompilerTest extends \PHPUnit_Framework_TestCase
// irrelevant for both matching and generating URLs.
$route->compile();
}
/**
* @dataProvider getNumericVariableNames
* @expectedException \DomainException
*/
public function testRouteWithNumericVariableName($name)
{
$route = new Route('/{'. $name . '}');
$route->compile();
}
public function getNumericVariableNames()
{
return array(
array('09'),
array('123'),
array('1e2')
);
}
}