* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Routing\Generator\Dumper; use Symfony\Component\Routing\Route; /** * PhpGeneratorDumper creates a PHP class able to generate URLs for a given set of routes. * * @author Fabien Potencier * * @api */ 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 * * @api */ 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() ; } private function addGenerator() { $methods = array(); foreach ($this->getRoutes()->all() as $name => $route) { $compiledRoute = $route->compile(); $variables = str_replace("\n", '', var_export($compiledRoute->getVariables(), true)); $defaults = str_replace("\n", '', var_export($compiledRoute->getDefaults(), true)); $requirements = str_replace("\n", '', var_export($compiledRoute->getRequirements(), true)); $tokens = str_replace("\n", '', var_export($compiledRoute->getTokens(), true)); $escapedName = str_replace('.', '__', $name); $methods[] = <<{'get'.\$escapedName.'RouteInfo'}(); return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$absolute); } $methods EOF; } private function startClass($class, $baseClass) { $routes = array(); foreach ($this->getRoutes()->all() as $name => $route) { $routes[] = " '$name' => true,"; } $routes = implode("\n", $routes); return <<context = \$context; } EOF; } private function endClass() { return <<