From e582408188c116319d0ac3717d8cd9750671694a Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 9 Mar 2010 17:35:47 +0100 Subject: [PATCH 1/5] [Routing] fixed typo --- src/Symfony/Components/Routing/Generator/UrlGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Components/Routing/Generator/UrlGenerator.php b/src/Symfony/Components/Routing/Generator/UrlGenerator.php index 33e41b41fd..b3bd93fc88 100644 --- a/src/Symfony/Components/Routing/Generator/UrlGenerator.php +++ b/src/Symfony/Components/Routing/Generator/UrlGenerator.php @@ -38,7 +38,7 @@ class UrlGenerator implements UrlGeneratorInterface public function __construct(RouteCollection $routes, array $context = array(), array $defaults = array()) { $this->routes = $routes; - $this->context = array_merge(array('base_url', ''), $context); + $this->context = array_merge(array('base_url' => ''), $context); $this->defaults = $defaults; $this->cache = array(); } From 2bc47f13f563beef30e1c77337e8707deb798a4e Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 9 Mar 2010 17:36:30 +0100 Subject: [PATCH 2/5] [Routing] changed matching to only check for method if it is available in the context --- src/Symfony/Components/Routing/Matcher/UrlMatcher.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Components/Routing/Matcher/UrlMatcher.php b/src/Symfony/Components/Routing/Matcher/UrlMatcher.php index f8246d7639..27f6d07434 100644 --- a/src/Symfony/Components/Routing/Matcher/UrlMatcher.php +++ b/src/Symfony/Components/Routing/Matcher/UrlMatcher.php @@ -59,7 +59,7 @@ class UrlMatcher implements UrlMatcherInterface $compiledRoute = $route->compile(); // check HTTP method requirement - if (!isset($this->context['method']) || (($req = $route->getRequirement('_method')) && !in_array(strtolower($this->context['method']), array_map('strtolower', (array) $req)))) + if (isset($this->context['method']) && (($req = $route->getRequirement('_method')) && !in_array(strtolower($this->context['method']), array_map('strtolower', (array) $req)))) { continue; } From 0d05db0afcefc772b5997c8683b7d018d9f46422 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 9 Mar 2010 19:42:01 +0100 Subject: [PATCH 3/5] [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 c0f088fe42..e901f2f0cc 100644 --- a/src/Symfony/Components/Routing/Generator/Dumper/PhpGeneratorDumper.php +++ b/src/Symfony/Components/Routing/Generator/Dumper/PhpGeneratorDumper.php @@ -59,12 +59,13 @@ class PhpGeneratorDumper extends GeneratorDumper $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 @@ EOF 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 b3bd93fc88..e28f82e3a7 100644 --- a/src/Symfony/Components/Routing/Generator/UrlGenerator.php +++ b/src/Symfony/Components/Routing/Generator/UrlGenerator.php @@ -64,10 +64,10 @@ class UrlGenerator implements UrlGeneratorInterface $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 @@ class UrlGenerator implements UrlGeneratorInterface { 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; } From 7c727355d1ff658f3336fc120a8fe86d318ce53c Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 10 Mar 2010 14:08:55 +0100 Subject: [PATCH 4/5] [Console] fixed default message layout --- src/Symfony/Components/Console/Application.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Components/Console/Application.php b/src/Symfony/Components/Console/Application.php index 94fdbe7b39..e26319be75 100644 --- a/src/Symfony/Components/Console/Application.php +++ b/src/Symfony/Components/Console/Application.php @@ -255,7 +255,7 @@ class Application foreach ($this->definition->getOptions() as $option) { - $messages[] = sprintf(' %-24s %s %s', + $messages[] = sprintf(' %-29s %s %s', '--'.$option->getName().'', $option->getShortcut() ? '-'.$option->getShortcut().'' : ' ', $option->getDescription() From d229ce584f5a0faa9df01ef19c450a266ecc2430 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 10 Mar 2010 17:01:50 +0100 Subject: [PATCH 5/5] [Console] added __get() to Command to have shorter and more readable code in commands --- src/Symfony/Components/Console/Command/Command.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Symfony/Components/Console/Command/Command.php b/src/Symfony/Components/Console/Command/Command.php index db259d330c..bbde5f94e0 100644 --- a/src/Symfony/Components/Console/Command/Command.php +++ b/src/Symfony/Components/Console/Command/Command.php @@ -423,6 +423,20 @@ class Command return $this->application->getHelperSet()->get($name); } + /** + * Gets a helper instance by name. + * + * @param string $name The helper name + * + * @return mixed The helper value + * + * @throws \InvalidArgumentException if the helper is not defined + */ + public function __get($name) + { + return $this->application->getHelperSet()->get($name); + } + /** * Returns a text representation of the command. *