HelpCommand.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.6.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Command;
  16. use Cake\Console\Arguments;
  17. use Cake\Console\Command;
  18. use Cake\Console\CommandCollection;
  19. use Cake\Console\CommandCollectionAwareInterface;
  20. use Cake\Console\ConsoleIo;
  21. use Cake\Console\ConsoleOptionParser;
  22. use Cake\Console\ConsoleOutput;
  23. use SimpleXMLElement;
  24. /**
  25. * Print out command list
  26. */
  27. class HelpCommand extends Command implements CommandCollectionAwareInterface
  28. {
  29. /**
  30. * The command collection to get help on.
  31. *
  32. * @var \Cake\Console\CommandCollection
  33. */
  34. protected $commands;
  35. /**
  36. * {@inheritDoc}
  37. */
  38. public function setCommandCollection(CommandCollection $commands)
  39. {
  40. $this->commands = $commands;
  41. }
  42. /**
  43. * Main function Prints out the list of commands.
  44. *
  45. * @param \Cake\Console\Arguments $args The command arguments.
  46. * @param \Cake\Console\ConsoleIo $io The console io
  47. * @return int
  48. */
  49. public function execute(Arguments $args, ConsoleIo $io)
  50. {
  51. if (!$args->getOption('xml')) {
  52. $io->out('<info>Current Paths:</info>', 2);
  53. $io->out('* app: ' . APP_DIR . DIRECTORY_SEPARATOR);
  54. $io->out('* root: ' . ROOT . DIRECTORY_SEPARATOR);
  55. $io->out('* core: ' . CORE_PATH);
  56. $io->out('');
  57. $io->out('<info>Available Commands:</info>', 2);
  58. }
  59. $commands = $this->commands->getIterator();
  60. $commands->ksort();
  61. if ($args->getOption('xml')) {
  62. $this->asXml($io, $commands);
  63. return static::CODE_SUCCESS;
  64. }
  65. $this->asText($io, $commands);
  66. return static::CODE_SUCCESS;
  67. }
  68. /**
  69. * Output text.
  70. *
  71. * @param \Cake\Console\ConsoleIo $io The console io
  72. * @param \ArrayIterator $commands The command collection to output.
  73. * @return void
  74. */
  75. protected function asText($io, $commands)
  76. {
  77. $invert = [];
  78. foreach ($commands as $name => $class) {
  79. if (is_object($class)) {
  80. $class = get_class($class);
  81. }
  82. if (!isset($invert[$class])) {
  83. $invert[$class] = [];
  84. }
  85. $invert[$class][] = $name;
  86. }
  87. foreach ($commands as $name => $class) {
  88. if (is_object($class)) {
  89. $class = get_class($class);
  90. }
  91. if (count($invert[$class]) == 1) {
  92. $io->out('- ' . $name);
  93. }
  94. if (count($invert[$class]) > 1) {
  95. // Sort by length so we can get the shortest name.
  96. usort($invert[$class], function ($a, $b) {
  97. return strlen($a) - strlen($b);
  98. });
  99. $io->out('- ' . array_shift($invert[$class]));
  100. // Empty the list to prevent duplicates
  101. $invert[$class] = [];
  102. }
  103. }
  104. $io->out('');
  105. $io->out('To run a command, type <info>`cake command_name [args|options]`</info>');
  106. $io->out('To get help on a specific command, type <info>`cake command_name --help`</info>', 2);
  107. }
  108. /**
  109. * Output as XML
  110. *
  111. * @param \Cake\Console\ConsoleIo $io The console io
  112. * @param \ArrayIterator $commands The command collection to output
  113. * @return void
  114. */
  115. protected function asXml($io, $commands)
  116. {
  117. $shells = new SimpleXMLElement('<shells></shells>');
  118. foreach ($commands as $name => $class) {
  119. if (is_object($class)) {
  120. $class = get_class($class);
  121. }
  122. $shell = $shells->addChild('shell');
  123. $shell->addAttribute('name', $name);
  124. $shell->addAttribute('call_as', $name);
  125. $shell->addAttribute('provider', $class);
  126. $shell->addAttribute('help', $name . ' -h');
  127. }
  128. $io->setOutputAs(ConsoleOutput::RAW);
  129. $io->out($shells->saveXML());
  130. }
  131. /**
  132. * Gets the option parser instance and configures it.
  133. *
  134. * @param \Cake\Console\ConsoleOptionParser $parser The parser to build
  135. * @return \Cake\Console\ConsoleOptionParser
  136. */
  137. protected function buildOptionParser(ConsoleOptionParser $parser)
  138. {
  139. $parser->setDescription(
  140. 'Get the list of available commands for this application.'
  141. )->addOption('xml', [
  142. 'help' => 'Get the listing as XML.',
  143. 'boolean' => true
  144. ]);
  145. return $parser;
  146. }
  147. }