/lib/vendor/symfony-1.4.14/lib/task/help/sfListTask.class.php

https://github.com/yuya-takeyama/symfony-hackathon-20110924 · PHP · 177 lines · 122 code · 31 blank · 24 comment · 18 complexity · 242f682ba360f7ca7257e9afa5a7add8 MD5 · raw file

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