Skip to content

Commit

Permalink
[Routing] changed method_exists by an array with the routes names in …
Browse files Browse the repository at this point in the history
…the php generator dumper, its more efficient
  • Loading branch information
pablodip authored and fabpot committed Nov 30, 2010
1 parent 73331cf commit 794634d
Showing 1 changed file with 24 additions and 2 deletions.
Expand Up @@ -50,6 +50,7 @@ public function dump(array $options = array())
protected function addGenerator()
{
$methods = array();
$routes = array();

foreach ($this->routes->all() as $name => $route) {
$compiledRoute = $route->compile();
Expand All @@ -67,19 +68,24 @@ protected function get{$name}RouteInfo()
EOF
;

$routes[$name] = true;
}

$methods = implode("\n", $methods);
$routes = $this->exportParameters($routes);

return <<<EOF
public function generate(\$name, array \$parameters, \$absolute = false)
{
if (!method_exists(\$this, \$method = 'get'.\$name.'RouteInfo')) {
static \$routes = $routes;
if (!isset(\$routes[\$name])) {
throw new \InvalidArgumentException(sprintf('Route "%s" does not exist.', \$name));
}
list(\$variables, \$defaults, \$requirements, \$tokens) = \$this->\$method();
list(\$variables, \$defaults, \$requirements, \$tokens) = \$this->{'get'.\$name.'RouteInfo'}();
return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$absolute);
}
Expand Down Expand Up @@ -127,4 +133,20 @@ protected function endClass()
EOF;
}

protected function exportParameters($parameters, $indent = 12)
{
$php = array();
foreach ($parameters as $key => $value) {
if (is_array($value)) {
$value = $this->exportParameters($value, $indent + 4);
} else {
$value = var_export($value, true);
}

$php[] = sprintf('%s%s => %s,', str_repeat(' ', $indent), var_export($key, true), $value);
}

return sprintf("array(\n%s\n%s)", implode("\n", $php), str_repeat(' ', $indent - 4));
}
}

0 comments on commit 794634d

Please sign in to comment.