/
PhpGeneratorDumper.php
137 lines (113 loc) · 3.37 KB
/
PhpGeneratorDumper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace Symfony\Component\Routing\Generator\Dumper;
use Symfony\Component\Routing\Route;
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* PhpGeneratorDumper creates a PHP class able to generate URLs for a given set of routes.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class PhpGeneratorDumper extends GeneratorDumper
{
/**
* Dumps a set of routes to a PHP class.
*
* Available options:
*
* * class: The class name
* * base_class: The base class name
*
* @param array $options An array of options
*
* @return string A PHP class representing the generator class
*/
public function dump(array $options = array())
{
$options = array_merge(array(
'class' => 'ProjectUrlGenerator',
'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
), $options);
return
$this->startClass($options['class'], $options['base_class']).
$this->addConstructor().
$this->addGenerator().
$this->endClass()
;
}
protected function addGenerator()
{
$methods = array();
$routes = array();
foreach ($this->routes->all() as $name => $route) {
$compiledRoute = $route->compile();
$variables = str_replace("\n", '', var_export($compiledRoute->getVariables(), true));
$defaults = str_replace("\n", '', var_export($route->getDefaults(), true));
$requirements = str_replace("\n", '', var_export($compiledRoute->getRequirements(), true));
$tokens = str_replace("\n", '', var_export($compiledRoute->getTokens(), true));
$methods[] = <<<EOF
protected function get{$name}RouteInfo()
{
return array($variables, array_merge(\$this->defaults, $defaults), $requirements, $tokens);
}
EOF
;
$routes[] = " '$name' => true,";
}
$methods = implode("\n", $methods);
$routes = implode("\n", $routes);
return <<<EOF
public function generate(\$name, array \$parameters, \$absolute = false)
{
static \$routes = array(
$routes
);
if (!isset(\$routes[\$name])) {
throw new \InvalidArgumentException(sprintf('Route "%s" does not exist.', \$name));
}
list(\$variables, \$defaults, \$requirements, \$tokens) = \$this->{'get'.\$name.'RouteInfo'}();
return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$absolute);
}
$methods
EOF;
}
protected function startClass($class, $baseClass)
{
return <<<EOF
<?php
/**
* $class
*
* This class has been auto-generated
* by the Symfony Routing Component.
*/
class $class extends $baseClass
{
EOF;
}
protected function addConstructor()
{
return <<<EOF
/**
* Constructor.
*/
public function __construct(array \$context = array(), array \$defaults = array())
{
\$this->context = \$context;
\$this->defaults = \$defaults;
}
EOF;
}
protected function endClass()
{
return <<<EOF
}
EOF;
}
}