sfAppRoutesTask.class.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Displays the current routes for an application.
  11. *
  12. * @package symfony
  13. * @subpackage task
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfAppRoutesTask.class.php 12806 2008-11-09 08:07:57Z fabien $
  16. */
  17. class sfAppRoutesTask extends sfBaseTask
  18. {
  19. /**
  20. * @see sfTask
  21. */
  22. protected function configure()
  23. {
  24. $this->addArguments(array(
  25. new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),
  26. new sfCommandArgument('name', sfCommandArgument::OPTIONAL, 'A route name'),
  27. ));
  28. $this->namespace = 'app';
  29. $this->name = 'routes';
  30. $this->briefDescription = 'Displays current routes for an application';
  31. $this->detailedDescription = <<<EOF
  32. The [app:routes|INFO] displays the current routes for a given application:
  33. [./symfony app:routes frontend|INFO]
  34. EOF;
  35. }
  36. /**
  37. * @see sfTask
  38. */
  39. protected function execute($arguments = array(), $options = array())
  40. {
  41. // get routes
  42. $config = new sfRoutingConfigHandler();
  43. $routes = $config->evaluate($this->configuration->getConfigPaths('config/routing.yml'));
  44. $routing = new sfPatternRouting($this->dispatcher);
  45. $routing->setRoutes($routes);
  46. $this->dispatcher->notify(new sfEvent($routing, 'routing.load_configuration'));
  47. $this->routes = $routing->getRoutes();
  48. // display
  49. $arguments['name'] ? $this->outputRoute($arguments['application'], $arguments['name']) : $this->outputRoutes($arguments['application']);
  50. }
  51. protected function outputRoutes($application)
  52. {
  53. $this->logSection('app', sprintf('Current routes for application "%s"', $application));
  54. $maxName = 4;
  55. $maxMethod = 6;
  56. foreach ($this->routes as $name => $route)
  57. {
  58. $requirements = $route->getRequirements();
  59. $method = isset($requirements['sf_method']) ? strtoupper(is_array($requirements['sf_method']) ? implode(', ', $requirements['sf_method']) : $requirements['sf_method']) : 'ANY';
  60. if (strlen($name) > $maxName)
  61. {
  62. $maxName = strlen($name);
  63. }
  64. if (strlen($method) > $maxMethod)
  65. {
  66. $maxMethod = strlen($method);
  67. }
  68. }
  69. $format = '%-'.$maxName.'s %-'.$maxMethod.'s %s';
  70. // displays the generated routes
  71. $format1 = '%-'.($maxName + 9).'s %-'.($maxMethod + 9).'s %s';
  72. $this->log(sprintf($format1, $this->formatter->format('Name', 'COMMENT'), $this->formatter->format('Method', 'COMMENT'), $this->formatter->format('Pattern', 'COMMENT')));
  73. foreach ($this->routes as $name => $route)
  74. {
  75. $requirements = $route->getRequirements();
  76. $method = isset($requirements['sf_method']) ? strtoupper(is_array($requirements['sf_method']) ? implode(', ', $requirements['sf_method']) : $requirements['sf_method']) : 'ANY';
  77. $this->log(sprintf($format, $name, $method, $route->getPattern()));
  78. }
  79. }
  80. protected function outputRoute($application, $name)
  81. {
  82. $this->logSection('app', sprintf('Route "%s" for application "%s"', $name, $application));
  83. if (!isset($this->routes[$name]))
  84. {
  85. throw new sfCommandException(sprintf('The route "%s" does not exist.', $name));
  86. }
  87. $route = $this->routes[$name];
  88. $this->log(sprintf('%s %s', $this->formatter->format('Name', 'COMMENT'), $name));
  89. $this->log(sprintf('%s %s', $this->formatter->format('Pattern', 'COMMENT'), $route->getPattern()));
  90. $this->log(sprintf('%s %s', $this->formatter->format('Class', 'COMMENT'), get_class($route)));
  91. $defaults = '';
  92. $d = $route->getDefaults();
  93. ksort($d);
  94. foreach ($d as $name => $value)
  95. {
  96. $defaults .= ($defaults ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->arrayToString($value);
  97. }
  98. $this->log(sprintf('%s %s', $this->formatter->format('Defaults', 'COMMENT'), $defaults));
  99. $requirements = '';
  100. $r = $route->getRequirements();
  101. ksort($r);
  102. foreach ($r as $name => $value)
  103. {
  104. $requirements .= ($requirements ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->arrayToString($value);
  105. }
  106. $this->log(sprintf('%s %s', $this->formatter->format('Requirements', 'COMMENT'), $requirements));
  107. $options = '';
  108. $o = $route->getOptions();
  109. ksort($o);
  110. foreach ($o as $name => $value)
  111. {
  112. $options .= ($options ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->arrayToString($value);
  113. }
  114. $this->log(sprintf('%s %s', $this->formatter->format('Options', 'COMMENT'), $options));
  115. $this->log(sprintf('%s %s', $this->formatter->format('Regex', 'COMMENT'), preg_replace('/^ /', '', preg_replace('/^/m', ' ', $route->getRegex()))));
  116. $tokens = '';
  117. foreach ($route->getTokens() as $token)
  118. {
  119. if (!$tokens)
  120. {
  121. $tokens = $this->displayToken($token);
  122. }
  123. else
  124. {
  125. $tokens .= "\n".str_repeat(' ', 13).$this->displayToken($token);
  126. }
  127. }
  128. $this->log(sprintf('%s %s', $this->formatter->format('Tokens', 'COMMENT'), $tokens));
  129. }
  130. protected function displayToken($token)
  131. {
  132. $type = array_shift($token);
  133. array_shift($token);
  134. return sprintf('%-10s %s', $type, $this->arrayToString($token));
  135. }
  136. protected function arrayToString($array)
  137. {
  138. return preg_replace("/\n\s*/s", '', var_export($array, true));
  139. }
  140. }