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

Debug toolbar