/lib/vendor/symfony-1.4.14/lib/task/app/sfAppRoutesTask.class.php

https://github.com/yuya-takeyama/symfony-hackathon-20110924 · PHP · 166 lines · 120 code · 23 blank · 23 comment · 6 complexity · 1720ffa0d516ef8b7773c1f5adcf029e MD5 · raw file

  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 23549 2009-11-03 09:10:12Z fabien $
  16. */
  17. class sfAppRoutesTask extends sfBaseTask
  18. {
  19. protected
  20. $routes = array();
  21. /**
  22. * @see sfTask
  23. */
  24. protected function configure()
  25. {
  26. $this->addArguments(array(
  27. new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),
  28. new sfCommandArgument('name', sfCommandArgument::OPTIONAL, 'A route name'),
  29. ));
  30. $this->namespace = 'app';
  31. $this->name = 'routes';
  32. $this->briefDescription = 'Displays current routes for an application';
  33. $this->detailedDescription = <<<EOF
  34. The [app:routes|INFO] displays the current routes for a given application:
  35. [./symfony app:routes frontend|INFO]
  36. EOF;
  37. }
  38. /**
  39. * @see sfTask
  40. */
  41. protected function execute($arguments = array(), $options = array())
  42. {
  43. $this->routes = $this->getRouting()->getRoutes();
  44. // display
  45. $arguments['name'] ? $this->outputRoute($arguments['application'], $arguments['name']) : $this->outputRoutes($arguments['application']);
  46. }
  47. protected function outputRoutes($application)
  48. {
  49. $this->logSection('app', sprintf('Current routes for application "%s"', $application));
  50. $maxName = 4;
  51. $maxMethod = 6;
  52. foreach ($this->routes as $name => $route)
  53. {
  54. $requirements = $route->getRequirements();
  55. $method = isset($requirements['sf_method']) ? strtoupper(is_array($requirements['sf_method']) ? implode(', ', $requirements['sf_method']) : $requirements['sf_method']) : 'ANY';
  56. if (strlen($name) > $maxName)
  57. {
  58. $maxName = strlen($name);
  59. }
  60. if (strlen($method) > $maxMethod)
  61. {
  62. $maxMethod = strlen($method);
  63. }
  64. }
  65. $format = '%-'.$maxName.'s %-'.$maxMethod.'s %s';
  66. // displays the generated routes
  67. $format1 = '%-'.($maxName + 9).'s %-'.($maxMethod + 9).'s %s';
  68. $this->log(sprintf($format1, $this->formatter->format('Name', 'COMMENT'), $this->formatter->format('Method', 'COMMENT'), $this->formatter->format('Pattern', 'COMMENT')));
  69. foreach ($this->routes as $name => $route)
  70. {
  71. $requirements = $route->getRequirements();
  72. $method = isset($requirements['sf_method']) ? strtoupper(is_array($requirements['sf_method']) ? implode(', ', $requirements['sf_method']) : $requirements['sf_method']) : 'ANY';
  73. $this->log(sprintf($format, $name, $method, $route->getPattern()));
  74. }
  75. }
  76. protected function outputRoute($application, $name)
  77. {
  78. $this->logSection('app', sprintf('Route "%s" for application "%s"', $name, $application));
  79. if (!isset($this->routes[$name]))
  80. {
  81. throw new sfCommandException(sprintf('The route "%s" does not exist.', $name));
  82. }
  83. $route = $this->routes[$name];
  84. $this->log(sprintf('%s %s', $this->formatter->format('Name', 'COMMENT'), $name));
  85. $this->log(sprintf('%s %s', $this->formatter->format('Pattern', 'COMMENT'), $route->getPattern()));
  86. $this->log(sprintf('%s %s', $this->formatter->format('Class', 'COMMENT'), get_class($route)));
  87. $defaults = '';
  88. $d = $route->getDefaults();
  89. ksort($d);
  90. foreach ($d as $name => $value)
  91. {
  92. $defaults .= ($defaults ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
  93. }
  94. $this->log(sprintf('%s %s', $this->formatter->format('Defaults', 'COMMENT'), $defaults));
  95. $requirements = '';
  96. $r = $route->getRequirements();
  97. ksort($r);
  98. foreach ($r as $name => $value)
  99. {
  100. $requirements .= ($requirements ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
  101. }
  102. $this->log(sprintf('%s %s', $this->formatter->format('Requirements', 'COMMENT'), $requirements));
  103. $options = '';
  104. $o = $route->getOptions();
  105. ksort($o);
  106. foreach ($o as $name => $value)
  107. {
  108. $options .= ($options ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
  109. }
  110. $this->log(sprintf('%s %s', $this->formatter->format('Options', 'COMMENT'), $options));
  111. $this->log(sprintf('%s %s', $this->formatter->format('Regex', 'COMMENT'), preg_replace('/^ /', '', preg_replace('/^/m', ' ', $route->getRegex()))));
  112. $tokens = '';
  113. foreach ($route->getTokens() as $token)
  114. {
  115. if (!$tokens)
  116. {
  117. $tokens = $this->displayToken($token);
  118. }
  119. else
  120. {
  121. $tokens .= "\n".str_repeat(' ', 13).$this->displayToken($token);
  122. }
  123. }
  124. $this->log(sprintf('%s %s', $this->formatter->format('Tokens', 'COMMENT'), $tokens));
  125. }
  126. protected function displayToken($token)
  127. {
  128. $type = array_shift($token);
  129. array_shift($token);
  130. return sprintf('%-10s %s', $type, $this->formatValue($token));
  131. }
  132. protected function formatValue($value)
  133. {
  134. if (is_object($value))
  135. {
  136. return sprintf('object(%s)', get_class($value));
  137. }
  138. else
  139. {
  140. return preg_replace("/\n\s*/s", '', var_export($value, true));
  141. }
  142. }
  143. }