PageRenderTime 68ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/sbweb/sbweb_logica/lib/symfony/task/app/sfAppRoutesTask.class.php

http://opac-sbweb.googlecode.com/
PHP | 165 lines | 116 code | 25 blank | 24 comment | 5 complexity | 10fff53cce472dce49b953209ea6f6c4 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  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. }