8000 [Routing] improved generated class by PhpGeneratorDumper · hostingnuggets/symfony@382b083 · GitHub
[go: up one dir, main page]

Skip to content

Commit 382b083

Browse files
committed
[Routing] improved generated class by PhpGeneratorDumper
1 parent 27a05f4 commit 382b083

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

src/Symfony/Component/Routing/Generator/Dumper/PhpGeneratorDumper.php

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* PhpGeneratorDumper creates a PHP class able to generate URLs for a given set of routes.
1818
*
1919
* @author Fabien Potencier <fabien@symfony.com>
20+
* @author Tobias Schultze <http://tobion.de>
2021
*
2122
* @api
2223
*/
@@ -43,12 +44,6 @@ public function dump(array $options = array())
4344
'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
4445
), $options);
4546

46-
$declaredRouteNames = "array(\n";
47-
foreach ($this->getRoutes()->all() as $name => $route) {
48-
$declaredRouteNames .= " '$name' => true,\n";
49-
}
50-
$declaredRouteNames .= ' );';
51-
5247
return <<<EOF
5348
<?php
5449
@@ -63,7 +58,7 @@ public function dump(array $options = array())
6358
*/
6459
class {$options['class']} extends {$options['base_class']}
6560
{
66-
static private \$declaredRouteNames = $declaredRouteNames
61+
static private \$declaredRoutes = {$this->generateDeclaredRoutes()};
6762
6863
/**
6964
* Constructor.
@@ -73,49 +68,55 @@ public function __construct(RequestContext \$context)
7368
\$this->context = \$context;
7469
}
7570
76-
{$this->addGenerator()}
71+
{$this->generateGenerateMethod()}
7772
}
7873
7974
EOF;
8075
}
8176

82-
private function addGenerator()
77+
/**
78+
* Generates PHP code representing an array of defined routes
79+
* together with the routes properties (e.g. requirements).
80+
*
81+
* @return string PHP code
82+
*/
83+
private function generateDeclaredRoutes()
8384
{
84-
$methods = '';
85+
$routes = "array(\n";
8586
foreach ($this->getRoutes()->all() as $name => $route) {
8687
$compiledRoute = $route->compile();
8788

88-
$variables = str_replace("\n", '', var_export($compiledRoute->getVariables(), true));
89-
$defaults = str_replace("\n", '', var_export($compiledRoute->getDefaults(), true));
90-
$requirements = str_replace("\n", '', var_export($compiledRoute->getRequirements(), true));
91-
$tokens = str_replace("\n", '', var_export($compiledRoute->getTokens(), true));
89+
$properties = array();
90+
$properties[] = $compiledRoute->getVariables();
91+
$properties[] = $compiledRoute->getDefaults();
92+
$properties[] = $compiledRoute->getRequirements();
93+
$properties[] = $compiledRoute->getTokens();
9294

93-
$escapedName = str_replace('.', '__', $name);
95+
$routes .= sprintf(" '%s' => %s,\n", $name, str_replace("\n", '', var_export($properties, true)));
96+
}
97+
$routes .= ' )';
9498

95-
$methods .= <<<EOF
96-
private function get{$escapedName}RouteInfo()
97-
{
98-
return array($variables, $defaults, $requirements, $tokens);
99+
return $routes;
99100
}
100101

101-
EOF;
102-
}
103-
102+
/**
103+
* Generates PHP code representing the `generate` method that implements the UrlGeneratorInterface.
104+
*
105+
* @return string PHP code
106+
*/
107+
private function generateGenerateMethod()
108+
{
104109
return <<<EOF
105110
public function generate(\$name, \$parameters = array(), \$absolute = false)
106111
{
107-
if (!isset(self::\$declaredRouteNames[\$name])) {
112+
if (!isset(self::\$declaredRoutes[\$name])) {
108113
throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', \$name));
109114
}
110115
111-
\$escapedName = str_replace('.', '__', \$name);
112-
113-
list(\$variables, \$defaults, \$requirements, \$tokens) = \$this->{'get'.\$escapedName.'RouteInfo'}();
116+
list(\$variables, \$defaults, \$requirements, \$tokens) = self::\$declaredRoutes[\$name];
114117
115118
return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$absolute);
116119
}
117-
118-
$methods
119120
EOF;
120121
}
121122
}

0 commit comments

Comments
 (0)
0