Skip to content

Commit e17c43d

Browse files
committed
Merge branch 'master' of github.com:fabpot/symfony
* 'master' of github.com:fabpot/symfony: [Console] added __get() to Command to have shorter and more readable code in commands [Console] fixed default message layout [Routing] added requirements checking when generating a route [Routing] changed matching to only check for method if it is available in the context [Routing] fixed typo
2 parents fff1334 + d229ce5 commit e17c43d

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

src/Symfony/Components/Console/Application.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public function getHelp()
255255

256256
foreach ($this->definition->getOptions() as $option)
257257
{
258-
$messages[] = sprintf(' %-24s %s %s',
258+
$messages[] = sprintf(' %-29s %s %s',
259259
'<info>--'.$option->getName().'</info>',
260260
$option->getShortcut() ? '<info>-'.$option->getShortcut().'</info>' : ' ',
261261
$option->getDescription()

src/Symfony/Components/Console/Command/Command.php

+14
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,20 @@ protected function getHelper($name)
423423
return $this->application->getHelperSet()->get($name);
424424
}
425425

426+
/**
427+
* Gets a helper instance by name.
428+
*
429+
* @param string $name The helper name
430+
*
431+
* @return mixed The helper value
432+
*
433+
* @throws \InvalidArgumentException if the helper is not defined
434+
*/
435+
public function __get($name)
436+
{
437+
return $this->application->getHelperSet()->get($name);
438+
}
439+
426440
/**
427441
* Returns a text representation of the command.
428442
*

src/Symfony/Components/Routing/Generator/Dumper/PhpGeneratorDumper.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ protected function addGenerator()
5959

6060
$variables = str_replace("\n", '', var_export($compiledRoute->getVariables(), true));
6161
$defaults = str_replace("\n", '', var_export($route->getDefaults(), true));
62+
$requirements = str_replace("\n", '', var_export($compiledRoute->getRequirements(), true));
6263
$tokens = str_replace("\n", '', var_export($compiledRoute->getTokens(), true));
6364

6465
$methods[] = <<<EOF
6566
protected function get{$name}RouteInfo()
6667
{
67-
return array($variables, array_merge(\$this->defaults, $defaults), $tokens);
68+
return array($variables, array_merge(\$this->defaults, $defaults), $requirements, $tokens);
6869
}
6970
7071
EOF
@@ -82,9 +83,9 @@ public function generate(\$name, array \$parameters, \$absolute = false)
8283
throw new \InvalidArgumentException(sprintf('Route "%s" does not exist.', \$name));
8384
}
8485
85-
list(\$variables, \$defaults, \$tokens) = \$this->\$method();
86+
list(\$variables, \$defaults, \$requirements, \$tokens) = \$this->\$method();
8687
87-
return \$this->doGenerate(\$variables, \$defaults, \$tokens, \$parameters, \$name, \$absolute);
88+
return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$absolute);
8889
}
8990
9091
$methods

src/Symfony/Components/Routing/Generator/UrlGenerator.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class UrlGenerator implements UrlGeneratorInterface
3838
public function __construct(RouteCollection $routes, array $context = array(), array $defaults = array())
3939
{
4040
$this->routes = $routes;
41-
$this->context = array_merge(array('base_url', ''), $context);
41+
$this->context = array_merge(array('base_url' => ''), $context);
4242
$this->defaults = $defaults;
4343
$this->cache = array();
4444
}
@@ -64,10 +64,10 @@ public function generate($name, array $parameters, $absolute = false)
6464
$this->cache[$name] = $route->compile();
6565
}
6666

67-
return $this->doGenerate($this->cache[$name]->getVariables(), $route->getDefaults(), $this->cache[$name]->getTokens(), $parameters, $name, $absolute);
67+
return $this->doGenerate($this->cache[$name]->getVariables(), $route->getDefaults(), $route->getRequirements(), $this->cache[$name]->getTokens(), $parameters, $name, $absolute);
6868
}
6969

70-
protected function doGenerate($variables, $defaults, $tokens, $parameters, $name, $absolute)
70+
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute)
7171
{
7272
$defaults = array_merge($this->defaults, $defaults);
7373
$tparams = array_merge($defaults, $parameters);
@@ -86,6 +86,12 @@ protected function doGenerate($variables, $defaults, $tokens, $parameters, $name
8686
{
8787
if (false === $optional || !isset($defaults[$token[3]]) || (isset($parameters[$token[3]]) && $parameters[$token[3]] != $defaults[$token[3]]))
8888
{
89+
// check requirement
90+
if (isset($requirements[$token[3]]) && !preg_match('#^'.$requirements[$token[3]].'$#', $tparams[$token[3]]))
91+
{
92+
throw new \InvalidArgumentException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $requirements[$token[3]], $tparams[$token[3]]));
93+
}
94+
8995
$url = $token[1].urlencode($tparams[$token[3]]).$url;
9096
$optional = false;
9197
}

src/Symfony/Components/Routing/Matcher/UrlMatcher.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function match($url)
5959
$compiledRoute = $route->compile();
6060

6161
// check HTTP method requirement
62-
if (!isset($this->context['method']) || (($req = $route->getRequirement('_method')) && !in_array(strtolower($this->context['method']), array_map('strtolower', (array) $req))))
62+
if (isset($this->context['method']) && (($req = $route->getRequirement('_method')) && !in_array(strtolower($this->context['method']), array_map('strtolower', (array) $req))))
6363
{
6464
continue;
6565
}

0 commit comments

Comments
 (0)