1. sfListTask.class.php
  2. /** * Lists tasks. * * @package symfony * @subpackage task * @author Fabien Potencier * @version SVN: $Id: sfListTask.class.php 21428 2009-08-25 11:24:13Z fabien $ */
  3. class sfListTask extends sfCommandApplicationTask
  4. {
  5. /**
  6. * @see sfTask
  7. */
  8. protected function configure()
  9. {
  10. $this->addArguments(array(
  11. new sfCommandArgument('namespace', sfCommandArgument::OPTIONAL, 'The namespace name'),
  12. ));
  13. $this->addOptions(array(
  14. new sfCommandOption('xml', null, sfCommandOption::PARAMETER_NONE, 'To output help as XML'),
  15. ));
  16. $this->briefDescription = 'Lists tasks';
  17. $this->detailedDescription = <<<EOF
  18. The [list|INFO] task lists all tasks:
  19. [./symfony list|INFO]
  20. You can also display the tasks for a specific namespace:
  21. [./symfony list test|INFO]
  22. You can also output the information as XML by using the [--xml|COMMENT] option:
  23. [./symfony list --xml|INFO]
  24. EOF;
  25. }
  26. /**
  27. * @see sfTask
  28. */
  29. protected function execute($arguments = array(), $options = array())
  30. {
  31. $tasks = array();
  32. foreach ($this->commandApplication->getTasks() as $name => $task)
  33. {
  34. if ($arguments['namespace'] && $arguments['namespace'] != $task->getNamespace())
  35. {
  36. continue;
  37. }
  38. if ($name != $task->getFullName())
  39. {
  40. // it is an alias
  41. continue;
  42. }
  43. if (!$task->getNamespace())
  44. {
  45. $name = '_default:'.$name;
  46. }
  47. $tasks[$name] = $task;
  48. }
  49. if ($options['xml'])
  50. {
  51. $this->outputAsXml($arguments['namespace'], $tasks);
  52. }
  53. else
  54. {
  55. $this->outputAsText($arguments['namespace'], $tasks);
  56. }
  57. }
  58. protected function outputAsText($namespace, $tasks)
  59. {
  60. $this->commandApplication->help();
  61. $this->log('');
  62. $width = 0;
  63. foreach ($tasks as $name => $task)
  64. {
  65. $width = strlen($task->getName()) > $width ? strlen($task->getName()) : $width;
  66. }
  67. $width += strlen($this->formatter->format(' ', 'INFO'));
  68. $messages = array();
  69. if ($namespace)
  70. {
  71. $messages[] = $this->formatter->format(sprintf("Available tasks for the \"%s\" namespace:", $namespace), 'COMMENT');
  72. }
  73. else
  74. {
  75. $messages[] = $this->formatter->format('Available tasks:', 'COMMENT');
  76. }
  77. // display tasks
  78. ksort($tasks);
  79. $currentNamespace = '';
  80. foreach ($tasks as $name => $task)
  81. {
  82. if (!$namespace && $currentNamespace != $task->getNamespace())
  83. {
  84. $currentNamespace = $task->getNamespace();
  85. $messages[] = $this->formatter->format($task->getNamespace(), 'COMMENT');
  86. }
  87. $aliases = $task->getAliases() ? $this->formatter->format(' ('.implode(', ', $task->getAliases()).')', 'COMMENT') : '';
  88. $messages[] = sprintf(" %-${width}s %s%s", $this->formatter->format(':'.$task->getName(), 'INFO'), $task->getBriefDescription(), $aliases);
  89. }
  90. $this->log($messages);
  91. }
  92. protected function outputAsXml($namespace, $tasks)
  93. {
  94. $dom = new DOMDocument('1.0', 'UTF-8');
  95. $dom->formatOutput = true;
  96. $dom->appendChild($symfonyXML = $dom->createElement('symfony'));
  97. $symfonyXML->appendChild($tasksXML = $dom->createElement('tasks'));
  98. if ($namespace)
  99. {
  100. $tasksXML->setAttribute('namespace', $namespace);
  101. }
  102. else
  103. {
  104. $symfonyXML->appendChild($namespacesXML = $dom->createElement('namespaces'));
  105. }
  106. // display tasks
  107. ksort($tasks);
  108. $currentNamespace = 'foobar';
  109. $namespaceArrayXML = array();
  110. foreach ($tasks as $name => $task)
  111. {
  112. if (!$namespace && $currentNamespace != $task->getNamespace())
  113. {
  114. $currentNamespace = $task->getNamespace();
  115. $namespacesXML->appendChild($namespaceArrayXML[$task->getNamespace()] = $dom->createElement('namespace'));
  116. $namespaceArrayXML[$task->getNamespace()]->setAttribute('id', $task->getNamespace() ? $task->getNamespace() : '_global');
  117. }
  118. if (!$namespace)
  119. {
  120. $namespaceArrayXML[$task->getNamespace()]->appendChild($taskXML = $dom->createElement('task'));
  121. $taskXML->appendChild($dom->createTextNode($task->getName()));
  122. }
  123. $taskXML = new DOMDocument('1.0', 'UTF-8');
  124. $taskXML->formatOutput = true;
  125. $taskXML->loadXML($task->asXml());
  126. $node = $taskXML->getElementsByTagName('task')->item(0);
  127. $node = $dom->importNode($node, true);
  128. $tasksXML->appendChild($node);
  129. }
  130. echo $dom->saveXml();
  131. }
  132. }

Debug toolbar