PhpGeneratorDumper.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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\Route;
  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. *
  17. * @api
  18. */
  19. class PhpGeneratorDumper extends GeneratorDumper
  20. {
  21. /**
  22. * Dumps a set of routes to a PHP class.
  23. *
  24. * Available options:
  25. *
  26. * * class: The class name
  27. * * base_class: The base class name
  28. *
  29. * @param array $options An array of options
  30. *
  31. * @return string A PHP class representing the generator class
  32. *
  33. * @api
  34. */
  35. public function dump(array $options = array())
  36. {
  37. $options = array_merge(array(
  38. 'class' => 'ProjectUrlGenerator',
  39. 'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
  40. ), $options);
  41. return
  42. $this->startClass($options['class'], $options['base_class']).
  43. $this->addConstructor().
  44. $this->addGenerator().
  45. $this->endClass()
  46. ;
  47. }
  48. private function addGenerator()
  49. {
  50. $methods = array();
  51. foreach ($this->getRoutes()->all() as $name => $route) {
  52. $compiledRoute = $route->compile();
  53. $variables = str_replace("\n", '', var_export($compiledRoute->getVariables(), true));
  54. $defaults = str_replace("\n", '', var_export($compiledRoute->getDefaults(), true));
  55. $requirements = str_replace("\n", '', var_export($compiledRoute->getRequirements(), true));
  56. $tokens = str_replace("\n", '', var_export($compiledRoute->getTokens(), true));
  57. $escapedName = str_replace('.', '__', $name);
  58. $methods[] = <<<EOF
  59. private function get{$escapedName}RouteInfo()
  60. {
  61. return array($variables, $defaults, $requirements, $tokens);
  62. }
  63. EOF
  64. ;
  65. }
  66. $methods = implode("\n", $methods);
  67. return <<<EOF
  68. public function generate(\$name, array \$parameters = array(), \$absolute = false)
  69. {
  70. if (!isset(self::\$declaredRouteNames[\$name])) {
  71. throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', \$name));
  72. }
  73. \$escapedName = str_replace('.', '__', \$name);
  74. list(\$variables, \$defaults, \$requirements, \$tokens) = \$this->{'get'.\$escapedName.'RouteInfo'}();
  75. return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$absolute);
  76. }
  77. $methods
  78. EOF;
  79. }
  80. private function startClass($class, $baseClass)
  81. {
  82. $routes = array();
  83. foreach ($this->getRoutes()->all() as $name => $route) {
  84. $routes[] = " '$name' => true,";
  85. }
  86. $routes = implode("\n", $routes);
  87. return <<<EOF
  88. <?php
  89. use Symfony\Component\Routing\RequestContext;
  90. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  91. /**
  92. * $class
  93. *
  94. * This class has been auto-generated
  95. * by the Symfony Routing Component.
  96. */
  97. class $class extends $baseClass
  98. {
  99. static private \$declaredRouteNames = array(
  100. $routes
  101. );
  102. EOF;
  103. }
  104. private function addConstructor()
  105. {
  106. return <<<EOF
  107. /**
  108. * Constructor.
  109. */
  110. public function __construct(RequestContext \$context)
  111. {
  112. \$this->context = \$context;
  113. }
  114. EOF;
  115. }
  116. private function endClass()
  117. {
  118. return <<<EOF
  119. }
  120. EOF;
  121. }
  122. }