8000 merged branch Tobion/generator-dumper (PR #3894) · hostingnuggets/symfony@5ab006a · GitHub
[go: up one dir, main page]

Skip to content

Commit 5ab006a

Browse files
committed
merged branch Tobion/generator-dumper (PR symfony#3894)
Commits ------- 382b083 [Routing] improved generated class by PhpGeneratorDumper 27a05f4 [Routing] small optimization of PhpGeneratorDumper Discussion ---------- [Routing] improved PhpGeneratorDumper Test pass: yes BC break: no The first commit only replaces arrays with strings and makes some cosmetic changes, so that it's more readable. This makes the `PhpGeneratorDumper` consistent in style with `PhpMatcherDumper` that I fixed recently and should slightly improve performance of the generation of the class. The second commit changes the output of the `PhpGeneratorDumper->dump` and tries to optimize the resulting class that is used to generate URLs. It's best explained with an example. Before my changes: ```php class ProjectUrlGenerator extends Symfony\Component\Routing\Generator\UrlGenerator { static private $declaredRouteNames = array( 'Test' => true, 'Test2' => true, ); /** * Constructor. */ public function __construct(RequestContext $context) { $this->context = $context; } public function generate($name, $parameters = array(), $absolute = false) { if (!isset(self::$declaredRouteNames[$name])) { throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name)); } $escapedName = str_replace('.', '__', $name); list($variables, $defaults, $requirements, $tokens) = $this->{'get'.$escapedName.'RouteInfo'}(); return $this->doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute); } private function getTestRouteInfo() { return array(array ( 0 => 'foo',), array (), array (), array ( 0 => array ( 0 => 'variable', 1 => '/', 2 => '[^/]+?', 3 => 'foo', ), 1 => array ( 0 => 'text', 1 => '/testing', ),)); } private function getTest2RouteInfo() { return array(array (), array (), array (), array ( 0 => array ( 0 => 'text', 1 => '/testing2', ),)); } } ``` After my changes in second commit: ```php class ProjectUrlGenerator extends Symfony\Component\Routing\Generator\UrlGenerator { static private $declaredRoutes = array( 'Test' => array ( 0 => array ( 0 => 'foo', ), 1 => array ( ), 2 => array ( ), 3 => array ( 0 => array ( 0 => 'variable', 1 => '/', 2 => '[^/]+?', 3 => 'foo', ), 1 => array ( 0 => 'text', 1 => '/testing', ), ),), 'Test2' => array ( 0 => array ( ), 1 => array ( ), 2 => array ( ), 3 => array ( 0 => array ( 0 => 'text', 1 => '/testing2', ), ),), ); /** * Constructor. */ public function __construct(RequestContext $context) { $this->context = $context; } public function generate($name, $parameters = array(), $absolute = false) { if (!isset(self::$declaredRoutes[$name])) { throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name)); } list($variables, $defaults, $requirements, $tokens) = self::$declaredRoutes[$name]; return $this->doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute); } } ``` As you can see, there is no need to escape the route name and invoke a special method anymore. Instead the route properties are included in the static route array directly, that existed anyway. Is also easier to read as defined routes and their properties are in the same place.
2 parents c1aa618 + 382b083 commit 5ab006a

File tree

1 file changed

+47
-75
lines changed

1 file changed

+47
-75
lines changed

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

Lines changed: 47 additions & 75 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,92 +44,22 @@ public function dump(array $options = array())
4344
'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
4445
), $options);
4546

46-
return
47-
$this->startClass($options['class'], $options['base_class']).
48-
$this->addConstructor().
49-
$this->addGenerator().
50-
$this->endClass()
51-
;
52-
}
53-
54-
private function addGenerator()
55-
{
56-
$methods = array();
57-
foreach ($this->getRoutes()->all() as $name => $route) {
58-
$compiledRoute = $route->compile();
59-
60-
$variables = str_replace("\n", '', var_export($compiledRoute->getVariables(), true));
61-
$defaults = str_replace("\n", '', var_export($compiledRoute->getDefaults(), true));
62-
$requirements = str_replace("\n", '', var_export($compiledRoute->getRequirements(), true));
63-
$tokens = str_replace("\n", '', var_export($compiledRoute->getTokens(), true));
64-
65-
$escapedName = str_replace('.', '__', $name);
66-
67-
$methods[] = <<<EOF
68-
private function get{$escapedName}RouteInfo()
69-
{
70-
return array($variables, $defaults, $requirements, $tokens);
71-
}
72-
73-
EOF
74-
;
75-
}
76-
77-
$methods = implode("\n", $methods);
78-
79-
return <<<EOF
80-
81-
public function generate(\$name, \$parameters = array(), \$absolute = false)
82-
{
83-
if (!isset(self::\$declaredRouteNames[\$name])) {
84-
throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', \$name));
85-
}
86-
87-
\$escapedName = str_replace('.', '__', \$name);
88-
89-
list(\$variables, \$defaults, \$requirements, \$tokens) = \$this->{'get'.\$escapedName.'RouteInfo'}();
90-
91-
return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$absolute);
92-
}
93-
94-
$methods
95-
EOF;
96-
}
97-
98-
private function startClass($class, $baseClass)
99-
{
100-
$routes = array();
101-
foreach ($this->getRoutes()->all() as $name => $route) {
102-
$routes[] = " '$name' => true,";
103-
}
104-
$routes = implode("\n", $routes);
105-
10647
return <<<EOF
10748
<?php
10849
10950
use Symfony\Component\Routing\RequestContext;
11051
use Symfony\Component\Routing\Exception\RouteNotFoundException;
11152
112-
11353
/**
114-
* $class
54+
* {$options['class']}
11555
*
11656
* This class has been auto-generated
11757
* by the Symfony Routing Component.
11858
*/
119-
class $class extends $baseClass
59+
class {$options['class']} extends {$options['base_class']}
12060
{
121-
static private \$declaredRouteNames = array(
122-
$routes
123-
);
124-
125-
126-
EOF;
127-
}
61+
static private \$declaredRoutes = {$this->generateDeclaredRoutes()};
12862
129-
private function addConstructor()
130-
{
131-
return <<<EOF
13263
/**
13364
* Constructor.
13465
*/
@@ -137,14 +68,55 @@ public function __construct(RequestContext \$context)
13768
\$this->context = \$context;
13869
}
13970
71+
{$this->generateGenerateMethod()}
72+
}
73+
14074
EOF;
14175
}
14276

143-
private function endClass()
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()
84+
{
85+
$routes = "array(\n";
86+
foreach ($this->getRoutes()->all() as $name => $route) {
87+
$compiledRoute = $route->compile();
88+
89+
$properties = array();
90+
$properties[] = $compiledRoute->getVariables();
91+
$properties[] = $compiledRoute->getDefaults();
92+
$properties[] = $compiledRoute->getRequirements();
93+
$properties[] = $compiledRoute->getTokens();
94+
95+
$routes .= sprintf(" '%s' => %s,\n", $name, str_replace("\n", '', var_export($properties, true)));
96+
}
97+
$routes .= ' )';
98+
99+
return $routes;
100+
}
101+
102+
/**
103+
* Generates PHP code representing the `generate` method that implements the UrlGeneratorInterface.
104+
*
105+
* @return string PHP code
106+
*/
107+
private function generateGenerateMethod()
144108
{
145109
return <<<EOF
146-
}
110+
public function generate(\$name, \$parameters = array(), \$absolute = false)
111+
{
112+
if (!isset(self::\$declaredRoutes[\$name])) {
113+
throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', \$name));
114+
}
115+
116+
list(\$variables, \$defaults, \$requirements, \$tokens) = self::\$declaredRoutes[\$name];
147117
118+
return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$absolute);
119+
}
148120
EOF;
149121
}
150122
}

0 commit comments

Comments
 (0)
0