Skip to content

Commit 27a05f4

Browse files
committed
[Routing] small optimization of PhpGeneratorDumper
1 parent b47cb35 commit 27a05f4

File tree

1 file changed

+37
-66
lines changed

1 file changed

+37
-66
lines changed

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

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,45 @@ public function dump(array $options = array())
4343
'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
4444
), $options);
4545

46-
return
47-
$this->startClass($options['class'], $options['base_class']).
48-
$this->addConstructor().
49-
$this->addGenerator().
50-
$this->endClass()
51-
;
46+
$declaredRouteNames = "array(\n";
47+
foreach ($this->getRoutes()->all() as $name => $route) {
48+
$declaredRouteNames .= " '$name' => true,\n";
49+
}
50+
$declaredRouteNames .= ' );';
51+
52+
return <<<EOF
53+
<?php
54+
55+
use Symfony\Component\Routing\RequestContext;
56+
use Symfony\Component\Routing\Exception\RouteNotFoundException;
57+
58+
/**
59+
* {$options['class']}
60+
*
61+
* This class has been auto-generated
62+
* by the Symfony Routing Component.
63+
*/
64+
class {$options['class']} extends {$options['base_class']}
65+
{
66+
static private \$declaredRouteNames = $declaredRouteNames
67+
68+
/**
69+
* Constructor.
70+
*/
71+
public function __construct(RequestContext \$context)
72+
{
73+
\$this->context = \$context;
74+
}
75+
76+
{$this->addGenerator()}
77+
}
78+
79+
EOF;
5280
}
5381

5482
private function addGenerator()
5583
{
56-
$methods = array();
84+
$methods = '';
5785
foreach ($this->getRoutes()->all() as $name => $route) {
5886
$compiledRoute = $route->compile();
5987

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

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

67-
$methods[] = <<<EOF
95+
$methods .= <<<EOF
6896
private function get{$escapedName}RouteInfo()
6997
{
7098
return array($variables, $defaults, $requirements, $tokens);
7199
}
72100
73-
EOF
74-
;
101+
EOF;
75102
}
76103

77-
$methods = implode("\n", $methods);
78-
79104
return <<<EOF
80-
81105
public function generate(\$name, \$parameters = array(), \$absolute = false)
82106
{
83107
if (!isset(self::\$declaredRouteNames[\$name])) {
@@ -92,59 +116,6 @@ public function generate(\$name, \$parameters = array(), \$absolute = false)
92116
}
93117
94118
$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-
106-
return <<<EOF
107-
<?php
108-
109-
use Symfony\Component\Routing\RequestContext;
110-
use Symfony\Component\Routing\Exception\RouteNotFoundException;
111-
112-
113-
/**
114-
* $class
115-
*
116-
* This class has been auto-generated
117-
* by the Symfony Routing Component.
118-
*/
119-
class $class extends $baseClass
120-
{
121-
static private \$declaredRouteNames = array(
122-
$routes
123-
);
124-
125-
126-
EOF;
127-
}
128-
129-
private function addConstructor()
130-
{
131-
return <<<EOF
132-
/**
133-
* Constructor.
134-
*/
135-
public function __construct(RequestContext \$context)
136-
{
137-
\$this->context = \$context;
138-
}
139-
140-
EOF;
141-
}
142-
143-
private function endClass()
144-
{
145-
return <<<EOF
146-
}
147-
148119
EOF;
149120
}
150121
}

0 commit comments

Comments
 (0)