Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Routing] simplified code
  • Loading branch information
fabpot committed Nov 30, 2010
1 parent 794634d commit 45e34c2
Showing 1 changed file with 5 additions and 20 deletions.
Expand Up @@ -51,7 +51,6 @@ protected function addGenerator()
{
$methods = array();
$routes = array();

foreach ($this->routes->all() as $name => $route) {
$compiledRoute = $route->compile();

Expand All @@ -69,17 +68,19 @@ protected function get{$name}RouteInfo()
EOF
;

$routes[$name] = true;
$routes[] = " '$name' => true,";
}

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

return <<<EOF
public function generate(\$name, array \$parameters, \$absolute = false)
{
static \$routes = $routes;
static \$routes = array(
$routes
);
if (!isset(\$routes[\$name])) {
throw new \InvalidArgumentException(sprintf('Route "%s" does not exist.', \$name));
Expand Down Expand Up @@ -133,20 +134,4 @@ 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 45e34c2

Please sign in to comment.