diff --git a/src/Symfony/Component/Routing/RouteCompiler.php b/src/Symfony/Component/Routing/RouteCompiler.php index e19f8d50d5..76877bc14b 100644 --- a/src/Symfony/Component/Routing/RouteCompiler.php +++ b/src/Symfony/Component/Routing/RouteCompiler.php @@ -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)); } diff --git a/src/Symfony/Component/Routing/Tests/RouteCompilerTest.php b/src/Symfony/Component/Routing/Tests/RouteCompilerTest.php index 4a32387fd0..fee2ae1304 100644 --- a/src/Symfony/Component/Routing/Tests/RouteCompilerTest.php +++ b/src/Symfony/Component/Routing/Tests/RouteCompilerTest.php @@ -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') + ); + } }