PhpGeneratorDumper.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Routing\Generator\Dumper;
  11. /**
  12. * PhpGeneratorDumper creates a PHP class able to generate URLs for a given set of routes.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. * @author Tobias Schultze <http://tobion.de>
  16. */
  17. class PhpGeneratorDumper extends GeneratorDumper
  18. {
  19. /**
  20. * Dumps a set of routes to a PHP class.
  21. *
  22. * Available options:
  23. *
  24. * * class: The class name
  25. * * base_class: The base class name
  26. *
  27. * @param array $options An array of options
  28. *
  29. * @return string A PHP class representing the generator class
  30. */
  31. public function dump(array $options = array())
  32. {
  33. $options = array_merge(array(
  34. 'class' => 'ProjectUrlGenerator',
  35. 'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
  36. ), $options);
  37. return <<<EOF
  38. <?php
  39. use Symfony\Component\Routing\RequestContext;
  40. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  41. use Psr\Log\LoggerInterface;
  42. /**
  43. * {$options['class']}
  44. *
  45. * This class has been auto-generated
  46. * by the Symfony Routing Component.
  47. */
  48. class {$options['class']} extends {$options['base_class']}
  49. {
  50. private static \$declaredRoutes;
  51. /**
  52. * Constructor.
  53. */
  54. public function __construct(RequestContext \$context, LoggerInterface \$logger = null)
  55. {
  56. \$this->context = \$context;
  57. \$this->logger = \$logger;
  58. if (null === self::\$declaredRoutes) {
  59. self::\$declaredRoutes = {$this->generateDeclaredRoutes()};
  60. }
  61. }
  62. {$this->generateGenerateMethod()}
  63. }
  64. EOF;
  65. }
  66. /**
  67. * Generates PHP code representing an array of defined routes
  68. * together with the routes properties (e.g. requirements).
  69. *
  70. * @return string PHP code
  71. */
  72. private function generateDeclaredRoutes()
  73. {
  74. $routes = "array(\n";
  75. foreach ($this->getRoutes()->all() as $name => $route) {
  76. $compiledRoute = $route->compile();
  77. $properties = array();
  78. $properties[] = $compiledRoute->getVariables();
  79. $properties[] = $route->getDefaults();
  80. $properties[] = $route->getRequirements();
  81. $properties[] = $compiledRoute->getTokens();
  82. $properties[] = $compiledRoute->getHostTokens();
  83. $properties[] = $route->getSchemes();
  84. $routes .= sprintf(" '%s' => %s,\n", $name, str_replace("\n", '', var_export($properties, true)));
  85. }
  86. $routes .= ' )';
  87. return $routes;
  88. }
  89. /**
  90. * Generates PHP code representing the `generate` method that implements the UrlGeneratorInterface.
  91. *
  92. * @return string PHP code
  93. */
  94. private function generateGenerateMethod()
  95. {
  96. return <<<'EOF'
  97. public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
  98. {
  99. if (!isset(self::$declaredRoutes[$name])) {
  100. throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
  101. }
  102. list($variables, $defaults, $requirements, $tokens, $hostTokens, $requiredSchemes) = self::$declaredRoutes[$name];
  103. return $this->doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, $requiredSchemes);
  104. }
  105. EOF;
  106. }
  107. }