CompiledUrlGeneratorDumper.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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\CompiledUrlMatcherDumper;
  12. /**
  13. * CompiledUrlGeneratorDumper creates a PHP array to be used with CompiledUrlGenerator.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. * @author Tobias Schultze <http://tobion.de>
  17. * @author Nicolas Grekas <p@tchwork.com>
  18. */
  19. class CompiledUrlGeneratorDumper extends GeneratorDumper
  20. {
  21. public function getCompiledRoutes(): array
  22. {
  23. $compiledRoutes = [];
  24. foreach ($this->getRoutes()->all() as $name => $route) {
  25. $compiledRoute = $route->compile();
  26. $compiledRoutes[$name] = [
  27. $compiledRoute->getVariables(),
  28. $route->getDefaults(),
  29. $route->getRequirements(),
  30. $compiledRoute->getTokens(),
  31. $compiledRoute->getHostTokens(),
  32. $route->getSchemes(),
  33. ];
  34. }
  35. return $compiledRoutes;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function dump(array $options = [])
  41. {
  42. return <<<EOF
  43. <?php
  44. // This file has been auto-generated by the Symfony Routing Component.
  45. return [{$this->generateDeclaredRoutes()}
  46. ];
  47. EOF;
  48. }
  49. /**
  50. * Generates PHP code representing an array of defined routes
  51. * together with the routes properties (e.g. requirements).
  52. */
  53. private function generateDeclaredRoutes(): string
  54. {
  55. $routes = '';
  56. foreach ($this->getCompiledRoutes() as $name => $properties) {
  57. $routes .= sprintf("\n '%s' => %s,", $name, CompiledUrlMatcherDumper::export($properties));
  58. }
  59. return $routes;
  60. }
  61. }