CompiledUrlGeneratorDumper.php 3.9 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. use Symfony\Component\Routing\Exception\RouteCircularReferenceException;
  12. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  13. use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper;
  14. /**
  15. * CompiledUrlGeneratorDumper creates a PHP array to be used with CompiledUrlGenerator.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. * @author Tobias Schultze <http://tobion.de>
  19. * @author Nicolas Grekas <p@tchwork.com>
  20. */
  21. class CompiledUrlGeneratorDumper extends GeneratorDumper
  22. {
  23. public function getCompiledRoutes(): array
  24. {
  25. $compiledRoutes = [];
  26. foreach ($this->getRoutes()->all() as $name => $route) {
  27. $compiledRoute = $route->compile();
  28. $compiledRoutes[$name] = [
  29. $compiledRoute->getVariables(),
  30. $route->getDefaults(),
  31. $route->getRequirements(),
  32. $compiledRoute->getTokens(),
  33. $compiledRoute->getHostTokens(),
  34. $route->getSchemes(),
  35. [],
  36. ];
  37. }
  38. return $compiledRoutes;
  39. }
  40. public function getCompiledAliases(): array
  41. {
  42. $routes = $this->getRoutes();
  43. $compiledAliases = [];
  44. foreach ($routes->getAliases() as $name => $alias) {
  45. $deprecations = $alias->isDeprecated() ? [$alias->getDeprecation($name)] : [];
  46. $currentId = $alias->getId();
  47. $visited = [];
  48. while (null !== $alias = $routes->getAlias($currentId) ?? null) {
  49. if (false !== $searchKey = array_search($currentId, $visited)) {
  50. $visited[] = $currentId;
  51. throw new RouteCircularReferenceException($currentId, \array_slice($visited, $searchKey));
  52. }
  53. if ($alias->isDeprecated()) {
  54. $deprecations[] = $deprecation = $alias->getDeprecation($currentId);
  55. trigger_deprecation($deprecation['package'], $deprecation['version'], $deprecation['message']);
  56. }
  57. $visited[] = $currentId;
  58. $currentId = $alias->getId();
  59. }
  60. if (null === $target = $routes->get($currentId)) {
  61. throw new RouteNotFoundException(sprintf('Target route "%s" for alias "%s" does not exist.', $currentId, $name));
  62. }
  63. $compiledTarget = $target->compile();
  64. $compiledAliases[$name] = [
  65. $compiledTarget->getVariables(),
  66. $target->getDefaults(),
  67. $target->getRequirements(),
  68. $compiledTarget->getTokens(),
  69. $compiledTarget->getHostTokens(),
  70. $target->getSchemes(),
  71. $deprecations,
  72. ];
  73. }
  74. return $compiledAliases;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function dump(array $options = [])
  80. {
  81. return <<<EOF
  82. <?php
  83. // This file has been auto-generated by the Symfony Routing Component.
  84. return [{$this->generateDeclaredRoutes()}
  85. ];
  86. EOF;
  87. }
  88. /**
  89. * Generates PHP code representing an array of defined routes
  90. * together with the routes properties (e.g. requirements).
  91. */
  92. private function generateDeclaredRoutes(): string
  93. {
  94. $routes = '';
  95. foreach ($this->getCompiledRoutes() as $name => $properties) {
  96. $routes .= sprintf("\n '%s' => %s,", $name, CompiledUrlMatcherDumper::export($properties));
  97. }
  98. foreach ($this->getCompiledAliases() as $alias => $properties) {
  99. $routes .= sprintf("\n '%s' => %s,", $alias, CompiledUrlMatcherDumper::export($properties));
  100. }
  101. return $routes;
  102. }
  103. }