[Routing] improved generated class by PhpGeneratorDumper
This commit is contained in:
parent
27a05f4c24
commit
382b08361b
@ -17,6 +17,7 @@ use Symfony\Component\Routing\Route;
|
|||||||
* PhpGeneratorDumper creates a PHP class able to generate URLs for a given set of routes.
|
* PhpGeneratorDumper creates a PHP class able to generate URLs for a given set of routes.
|
||||||
*
|
*
|
||||||
* @author Fabien Potencier <fabien@symfony.com>
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
* @author Tobias Schultze <http://tobion.de>
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@ -43,12 +44,6 @@ class PhpGeneratorDumper extends GeneratorDumper
|
|||||||
'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
|
'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
|
||||||
), $options);
|
), $options);
|
||||||
|
|
||||||
$declaredRouteNames = "array(\n";
|
|
||||||
foreach ($this->getRoutes()->all() as $name => $route) {
|
|
||||||
$declaredRouteNames .= " '$name' => true,\n";
|
|
||||||
}
|
|
||||||
$declaredRouteNames .= ' );';
|
|
||||||
|
|
||||||
return <<<EOF
|
return <<<EOF
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
@ -63,7 +58,7 @@ use Symfony\Component\Routing\Exception\RouteNotFoundException;
|
|||||||
*/
|
*/
|
||||||
class {$options['class']} extends {$options['base_class']}
|
class {$options['class']} extends {$options['base_class']}
|
||||||
{
|
{
|
||||||
static private \$declaredRouteNames = $declaredRouteNames
|
static private \$declaredRoutes = {$this->generateDeclaredRoutes()};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
@ -73,49 +68,55 @@ class {$options['class']} extends {$options['base_class']}
|
|||||||
\$this->context = \$context;
|
\$this->context = \$context;
|
||||||
}
|
}
|
||||||
|
|
||||||
{$this->addGenerator()}
|
{$this->generateGenerateMethod()}
|
||||||
}
|
}
|
||||||
|
|
||||||
EOF;
|
EOF;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function addGenerator()
|
/**
|
||||||
|
* Generates PHP code representing an array of defined routes
|
||||||
|
* together with the routes properties (e.g. requirements).
|
||||||
|
*
|
||||||
|
* @return string PHP code
|
||||||
|
*/
|
||||||
|
private function generateDeclaredRoutes()
|
||||||
{
|
{
|
||||||
$methods = '';
|
$routes = "array(\n";
|
||||||
foreach ($this->getRoutes()->all() as $name => $route) {
|
foreach ($this->getRoutes()->all() as $name => $route) {
|
||||||
$compiledRoute = $route->compile();
|
$compiledRoute = $route->compile();
|
||||||
|
|
||||||
$variables = str_replace("\n", '', var_export($compiledRoute->getVariables(), true));
|
$properties = array();
|
||||||
$defaults = str_replace("\n", '', var_export($compiledRoute->getDefaults(), true));
|
$properties[] = $compiledRoute->getVariables();
|
||||||
$requirements = str_replace("\n", '', var_export($compiledRoute->getRequirements(), true));
|
$properties[] = $compiledRoute->getDefaults();
|
||||||
$tokens = str_replace("\n", '', var_export($compiledRoute->getTokens(), true));
|
$properties[] = $compiledRoute->getRequirements();
|
||||||
|
$properties[] = $compiledRoute->getTokens();
|
||||||
|
|
||||||
$escapedName = str_replace('.', '__', $name);
|
$routes .= sprintf(" '%s' => %s,\n", $name, str_replace("\n", '', var_export($properties, true)));
|
||||||
|
}
|
||||||
|
$routes .= ' )';
|
||||||
|
|
||||||
$methods .= <<<EOF
|
return $routes;
|
||||||
private function get{$escapedName}RouteInfo()
|
|
||||||
{
|
|
||||||
return array($variables, $defaults, $requirements, $tokens);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EOF;
|
/**
|
||||||
}
|
* Generates PHP code representing the `generate` method that implements the UrlGeneratorInterface.
|
||||||
|
*
|
||||||
|
* @return string PHP code
|
||||||
|
*/
|
||||||
|
private function generateGenerateMethod()
|
||||||
|
{
|
||||||
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::\$declaredRoutes[\$name])) {
|
||||||
throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', \$name));
|
throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', \$name));
|
||||||
}
|
}
|
||||||
|
|
||||||
\$escapedName = str_replace('.', '__', \$name);
|
list(\$variables, \$defaults, \$requirements, \$tokens) = self::\$declaredRoutes[\$name];
|
||||||
|
|
||||||
list(\$variables, \$defaults, \$requirements, \$tokens) = \$this->{'get'.\$escapedName.'RouteInfo'}();
|
|
||||||
|
|
||||||
return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$absolute);
|
return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$absolute);
|
||||||
}
|
}
|
||||||
|
|
||||||
$methods
|
|
||||||
EOF;
|
EOF;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user