sfListTask.class.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 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. * Lists tasks.
  11. *
  12. * @package symfony
  13. * @subpackage task
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfListTask.class.php 11505 2008-09-13 09:22:23Z fabien $
  16. */
  17. class sfListTask extends sfCommandApplicationTask
  18. {
  19. /**
  20. * @see sfTask
  21. */
  22. protected function configure()
  23. {
  24. $this->addArguments(array(
  25. new sfCommandArgument('namespace', sfCommandArgument::OPTIONAL, 'The namespace name'),
  26. ));
  27. $this->briefDescription = 'Lists tasks';
  28. $this->detailedDescription = <<<EOF
  29. The [list|INFO] task lists all tasks:
  30. [./symfony list|INFO]
  31. You can also display the tasks for a specific namespace:
  32. [./symfony list test|INFO]
  33. EOF;
  34. }
  35. /**
  36. * @see sfTask
  37. */
  38. protected function execute($arguments = array(), $options = array())
  39. {
  40. $this->commandApplication->help();
  41. $this->log('');
  42. $tasks = array();
  43. foreach ($this->commandApplication->getTasks() as $name => $task)
  44. {
  45. if ($arguments['namespace'] && $arguments['namespace'] != $task->getNamespace())
  46. {
  47. continue;
  48. }
  49. if ($name != $task->getFullName())
  50. {
  51. // it is an alias
  52. continue;
  53. }
  54. if (!$task->getNamespace())
  55. {
  56. $name = '_default:'.$name;
  57. }
  58. $tasks[$name] = $task;
  59. }
  60. $width = 0;
  61. foreach ($tasks as $name => $task)
  62. {
  63. $width = strlen($task->getName()) > $width ? strlen($task->getName()) : $width;
  64. }
  65. $width += strlen($this->formatter->format(' ', 'INFO'));
  66. $messages = array();
  67. if ($arguments['namespace'])
  68. {
  69. $messages[] = $this->formatter->format(sprintf("Available tasks for the \"%s\" namespace:", $arguments['namespace']), 'COMMENT');
  70. }
  71. else
  72. {
  73. $messages[] = $this->formatter->format('Available tasks:', 'COMMENT');
  74. }
  75. // display tasks
  76. ksort($tasks);
  77. $currentNamespace = '';
  78. foreach ($tasks as $name => $task)
  79. {
  80. if (!$arguments['namespace'] && $currentNamespace != $task->getNamespace())
  81. {
  82. $currentNamespace = $task->getNamespace();
  83. $messages[] = $this->formatter->format($task->getNamespace(), 'COMMENT');
  84. }
  85. $aliases = $task->getAliases() ? $this->formatter->format(' ('.implode(', ', $task->getAliases()).')', 'COMMENT') : '';
  86. $messages[] = sprintf(" %-${width}s %s%s", $this->formatter->format(':'.$task->getName(), 'INFO'), $task->getBriefDescription(), $aliases);
  87. }
  88. $this->log($messages);
  89. }
  90. }