From 0d05db0afcefc772b5997c8683b7d018d9f46422 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 9 Mar 2010 19:42:01 +0100 Subject: [PATCH] [Routing] added requirements checking when generating a route --- .../Routing/Generator/Dumper/PhpGeneratorDumper.php | 7 ++++--- .../Components/Routing/Generator/UrlGenerator.php | 10 ++++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Components/Routing/Generator/Dumper/PhpGeneratorDumper.php b/src/Symfony/Components/Routing/Generator/Dumper/PhpGeneratorDumper.php index c0f088fe42d7..e901f2f0cc97 100644 --- a/src/Symfony/Components/Routing/Generator/Dumper/PhpGeneratorDumper.php +++ b/src/Symfony/Components/Routing/Generator/Dumper/PhpGeneratorDumper.php @@ -59,12 +59,13 @@ protected function addGenerator() $variables = str_replace("\n", '', var_export($compiledRoute->getVariables(), true)); $defaults = str_replace("\n", '', var_export($route->getDefaults(), true)); + $requirements = str_replace("\n", '', var_export($compiledRoute->getRequirements(), true)); $tokens = str_replace("\n", '', var_export($compiledRoute->getTokens(), true)); $methods[] = <<defaults, $defaults), $tokens); + return array($variables, array_merge(\$this->defaults, $defaults), $requirements, $tokens); } EOF @@ -82,9 +83,9 @@ public function generate(\$name, array \$parameters, \$absolute = false) throw new \InvalidArgumentException(sprintf('Route "%s" does not exist.', \$name)); } - list(\$variables, \$defaults, \$tokens) = \$this->\$method(); + list(\$variables, \$defaults, \$requirements, \$tokens) = \$this->\$method(); - return \$this->doGenerate(\$variables, \$defaults, \$tokens, \$parameters, \$name, \$absolute); + return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$absolute); } $methods diff --git a/src/Symfony/Components/Routing/Generator/UrlGenerator.php b/src/Symfony/Components/Routing/Generator/UrlGenerator.php index b3bd93fc8835..e28f82e3a77e 100644 --- a/src/Symfony/Components/Routing/Generator/UrlGenerator.php +++ b/src/Symfony/Components/Routing/Generator/UrlGenerator.php @@ -64,10 +64,10 @@ public function generate($name, array $parameters, $absolute = false) $this->cache[$name] = $route->compile(); } - return $this->doGenerate($this->cache[$name]->getVariables(), $route->getDefaults(), $this->cache[$name]->getTokens(), $parameters, $name, $absolute); + return $this->doGenerate($this->cache[$name]->getVariables(), $route->getDefaults(), $route->getRequirements(), $this->cache[$name]->getTokens(), $parameters, $name, $absolute); } - protected function doGenerate($variables, $defaults, $tokens, $parameters, $name, $absolute) + protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute) { $defaults = array_merge($this->defaults, $defaults); $tparams = array_merge($defaults, $parameters); @@ -86,6 +86,12 @@ protected function doGenerate($variables, $defaults, $tokens, $parameters, $name { if (false === $optional || !isset($defaults[$token[3]]) || (isset($parameters[$token[3]]) && $parameters[$token[3]] != $defaults[$token[3]])) { + // check requirement + if (isset($requirements[$token[3]]) && !preg_match('#^'.$requirements[$token[3]].'$#', $tparams[$token[3]])) + { + throw new \InvalidArgumentException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $requirements[$token[3]], $tparams[$token[3]])); + } + $url = $token[1].urlencode($tparams[$token[3]]).$url; $optional = false; }