4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Generator\Dumper
;
15 * PhpGeneratorDumper creates a PHP class able to generate URLs for a given set of routes.
17 * @author Fabien Potencier <fabien@symfony.com>
18 * @author Tobias Schultze <http://tobion.de>
22 class PhpGeneratorDumper
extends GeneratorDumper
25 * Dumps a set of routes to a PHP class.
29 * * class: The class name
30 * * base_class: The base class name
32 * @param array $options An array of options
34 * @return string A PHP class representing the generator class
38 public function dump(array $options = array())
40 $options = array_merge(array(
41 'class' => 'ProjectUrlGenerator',
42 'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
48 use Symfony\Component\Routing\RequestContext;
49 use Symfony\Component\Routing\Exception\RouteNotFoundException;
50 use Psr\Log\LoggerInterface;
55 * This class has been auto-generated
56 * by the Symfony Routing Component.
58 class {$options['class']} extends {$options['base_class']}
60 static private \$declaredRoutes = {$this->generateDeclaredRoutes()};
65 public function __construct(RequestContext \$context, LoggerInterface \$logger = null)
67 \$this->context = \$context;
68 \$this->logger = \$logger;
71 {$this->generateGenerateMethod()}
78 * Generates PHP code representing an array of defined routes
79 * together with the routes properties (e.g. requirements).
81 * @return string PHP code
83 private function generateDeclaredRoutes()
86 foreach ($this->getRoutes()->all() as $name => $route) {
87 $compiledRoute = $route->compile();
89 $properties = array();
90 $properties[] = $compiledRoute->getVariables();
91 $properties[] = $route->getDefaults();
92 $properties[] = $route->getRequirements();
93 $properties[] = $compiledRoute->getTokens();
94 $properties[] = $compiledRoute->getHostTokens();
96 $routes .= sprintf(" '%s' => %s,\n", $name, str_replace("\n", '', var_export($properties, true)));
104 * Generates PHP code representing the `generate` method that implements the UrlGeneratorInterface.
106 * @return string PHP code
108 private function generateGenerateMethod()
111 public function generate(\$name, \$parameters = array(), \$referenceType = self::ABSOLUTE_PATH)
113 if (!isset(self::\$declaredRoutes[\$name])) {
114 throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', \$name));
117 list(\$variables, \$defaults, \$requirements, \$tokens, \$hostTokens) = self::\$declaredRoutes[\$name];
119 return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$referenceType, \$hostTokens);