No Description

PhpGeneratorDumper.php 4.6KB

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