Skip to content

Commit

Permalink
[Routing] small optimization of PhpGeneratorDumper
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobion committed Apr 12, 2012
1 parent b47cb35 commit 27a05f4
Showing 1 changed file with 37 additions and 66 deletions.
103 changes: 37 additions & 66 deletions src/Symfony/Component/Routing/Generator/Dumper/PhpGeneratorDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,45 @@ public function dump(array $options = array())
'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
), $options);

return
$this->startClass($options['class'], $options['base_class']).
$this->addConstructor().
$this->addGenerator().
$this->endClass()
;
$declaredRouteNames = "array(\n";
foreach ($this->getRoutes()->all() as $name => $route) {
$declaredRouteNames .= " '$name' => true,\n";
}
$declaredRouteNames .= ' );';

return <<<EOF
<?php
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
/**
* {$options['class']}
*
* This class has been auto-generated
* by the Symfony Routing Component.
*/
class {$options['class']} extends {$options['base_class']}
{
static private \$declaredRouteNames = $declaredRouteNames
/**
* Constructor.
*/
public function __construct(RequestContext \$context)
{
\$this->context = \$context;
}
{$this->addGenerator()}
}
EOF;
}

private function addGenerator()
{
$methods = array();
$methods = '';
foreach ($this->getRoutes()->all() as $name => $route) {
$compiledRoute = $route->compile();

Expand All @@ -64,20 +92,16 @@ private function addGenerator()

$escapedName = str_replace('.', '__', $name);

$methods[] = <<<EOF
$methods .= <<<EOF
private function get{$escapedName}RouteInfo()
{
return array($variables, $defaults, $requirements, $tokens);
}
EOF
;
EOF;
}

$methods = implode("\n", $methods);

return <<<EOF
public function generate(\$name, \$parameters = array(), \$absolute = false)
{
if (!isset(self::\$declaredRouteNames[\$name])) {
Expand All @@ -92,59 +116,6 @@ public function generate(\$name, \$parameters = array(), \$absolute = false)
}
$methods
EOF;
}

private function startClass($class, $baseClass)
{
$routes = array();
foreach ($this->getRoutes()->all() as $name => $route) {
$routes[] = " '$name' => true,";
}
$routes = implode("\n", $routes);

return <<<EOF
<?php
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
/**
* $class
*
* This class has been auto-generated
* by the Symfony Routing Component.
*/
class $class extends $baseClass
{
static private \$declaredRouteNames = array(
$routes
);
EOF;
}

private function addConstructor()
{
return <<<EOF
/**
* Constructor.
*/
public function __construct(RequestContext \$context)
{
\$this->context = \$context;
}
EOF;
}

private function endClass()
{
return <<<EOF
}
EOF;
}
}

0 comments on commit 27a05f4

Please sign in to comment.