8000 merged branch Tobion/numeric-placeholder (PR #5439) · richardudovich/symfony@8b7ba9b · GitHub
[go: up one dir, main page]

Skip to content

Commit 8b7ba9b

Browse files
committed
merged branch Tobion/numeric-placeholder (PR symfony#5439)
Commits ------- be28e56 [Routing] disallow numeric named variables in pattern Discussion ---------- [Routing] compile check for numeric named variables in pattern Because PHP raises an error for such subpatterns in PCRE and thus would break matching, e.g. this is not allowed as regex `(?<123>.+)`. So add a compile time check for a non-working pattern like '/{123}'. --------------------------------------------------------------------------- by sstok at 2012-09-06T08:31:42Z Strangely enough Regex buddy gives no warning or error with the pattern. Is the name all numeric invalid or just the beginning? 1e4 and 0xFF would be perfectly valid but returns true with is_nummeric() --------------------------------------------------------------------------- by Tobion at 2012-09-06T08:59:07Z Any numeric is not valid. I guess this limitation is unique to PHP's binding to PCRE. I think it's because the returned matches array of of preg_match contains both the subpattern as integer index and as named variable. So having a numeric named variable would conflict as `array['1'] === array[1]`.
2 parents 71b39d5 + be28e56 commit 8b7ba9b

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Symfony/Component/Routing/RouteCompiler.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ class RouteCompiler implements RouteCompilerInterface
2323
/**
2424
* {@inheritDoc}
2525
*
26-
* @throws \LogicException If a variable is referenced more than once
26+
* @throws \LogicException If a variable is referenced more than once
27+
* @throws \DomainException If a variable name is numeric because PHP raises an error for such
28+
* subpatterns in PCRE and thus would break matching, e.g. "(?<123>.+)".
2729
*/
2830
public function compile(Route $route)
2931
{
@@ -56,6 +58,9 @@ public function compile(Route $route)
5658

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

61+
if (is_numeric($var)) {
62+
throw new \DomainException(sprintf('Variable name "%s" cannot be numeric in route pattern "%s". Please use a different name.', $var, $route->getPattern()));
63+
}
5964
if (in_array($var, $variables)) {
6065
throw new \LogicException(sprintf('Route pattern "%s" cannot reference variable name "%s" more than once.', $route->getPattern(), $var));
6166
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,23 @@ public function testRouteWithSameVariableTwice()
133133

134134
$compiled = $route->compile();
135135
}
136+
137+
/**
138+
* @dataProvider getNumericVariableNames
139+
* @expectedException \DomainException
140+
*/
141+
public function testRouteWithNumericVariableName($name)
142+
{
143+
$route = new Route('/{'. $name . '}');
144+
$route->compile();
145+
}
146+
147+
public function getNumericVariableNames()
148+
{
149+
return array(
150+
array('09'),
151+
array('123'),
152+
array('1e2')
153+
);
154+
}
136155
}

0 commit comments

Comments
 (0)
0