菜谱项目

PhpGeneratorDumper.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. * This class has been auto-generated
  44. * by the Symfony Routing Component.
  45. */
  46. class {$options['class']} extends {$options['base_class']}
  47. {
  48. private static \$declaredRoutes;
  49. public function __construct(RequestContext \$context, LoggerInterface \$logger = null)
  50. {
  51. \$this->context = \$context;
  52. \$this->logger = \$logger;
  53. if (null === self::\$declaredRoutes) {
  54. self::\$declaredRoutes = {$this->generateDeclaredRoutes()};
  55. }
  56. }
  57. {$this->generateGenerateMethod()}
  58. }
  59. EOF;
  60. }
  61. /**
  62. * Generates PHP code representing an array of defined routes
  63. * together with the routes properties (e.g. requirements).
  64. *
  65. * @return string PHP code
  66. */
  67. private function generateDeclaredRoutes()
  68. {
  69. $routes = "array(\n";
  70. foreach ($this->getRoutes()->all() as $name => $route) {
  71. $compiledRoute = $route->compile();
  72. $properties = array();
  73. $properties[] = $compiledRoute->getVariables();
  74. $properties[] = $route->getDefaults();
  75. $properties[] = $route->getRequirements();
  76. $properties[] = $compiledRoute->getTokens();
  77. $properties[] = $compiledRoute->getHostTokens();
  78. $properties[] = $route->getSchemes();
  79. $routes .= sprintf(" '%s' => %s,\n", $name, str_replace("\n", '', var_export($properties, true)));
  80. }
  81. $routes .= ' )';
  82. return $routes;
  83. }
  84. /**
  85. * Generates PHP code representing the `generate` method that implements the UrlGeneratorInterface.
  86. *
  87. * @return string PHP code
  88. */
  89. private function generateGenerateMethod()
  90. {
  91. return <<<'EOF'
  92. public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
  93. {
  94. if (!isset(self::$declaredRoutes[$name])) {
  95. throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
  96. }
  97. list($variables, $defaults, $requirements, $tokens, $hostTokens, $requiredSchemes) = self::$declaredRoutes[$name];
  98. return $this->doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, $requiredSchemes);
  99. }
  100. EOF;
  101. }
  102. }