PhpGeneratorDumper.php 4.1 KB

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