[Routing] small optimization of PhpGeneratorDumper

This commit is contained in:
Tobias Schultze 2012-04-12 06:16:26 +02:00
parent b47cb35e7b
commit 27a05f4c24

View File

@ -43,17 +43,45 @@ class PhpGeneratorDumper extends GeneratorDumper
'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator', 'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
), $options); ), $options);
return $declaredRouteNames = "array(\n";
$this->startClass($options['class'], $options['base_class']). foreach ($this->getRoutes()->all() as $name => $route) {
$this->addConstructor(). $declaredRouteNames .= " '$name' => true,\n";
$this->addGenerator(). }
$this->endClass() $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() private function addGenerator()
{ {
$methods = array(); $methods = '';
foreach ($this->getRoutes()->all() as $name => $route) { foreach ($this->getRoutes()->all() as $name => $route) {
$compiledRoute = $route->compile(); $compiledRoute = $route->compile();
@ -64,20 +92,16 @@ class PhpGeneratorDumper extends GeneratorDumper
$escapedName = str_replace('.', '__', $name); $escapedName = str_replace('.', '__', $name);
$methods[] = <<<EOF $methods .= <<<EOF
private function get{$escapedName}RouteInfo() private function get{$escapedName}RouteInfo()
{ {
return array($variables, $defaults, $requirements, $tokens); return array($variables, $defaults, $requirements, $tokens);
} }
EOF EOF;
;
} }
$methods = implode("\n", $methods);
return <<<EOF return <<<EOF
public function generate(\$name, \$parameters = array(), \$absolute = false) public function generate(\$name, \$parameters = array(), \$absolute = false)
{ {
if (!isset(self::\$declaredRouteNames[\$name])) { if (!isset(self::\$declaredRouteNames[\$name])) {
@ -92,59 +116,6 @@ EOF
} }
$methods $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; EOF;
} }
} }