DrushHelpCommand.php

  1. 8.0.x lib/Drush/Psysh/DrushHelpCommand.php
  2. master lib/Drush/Psysh/DrushHelpCommand.php

Contains \Drush\Psysh\DrushCommand.

Namespace

Drush\Psysh

Classes

Namesort descending Description
DrushHelpCommand Help command.

File

lib/Drush/Psysh/DrushHelpCommand.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drush\Psysh\DrushCommand.
  5. */
  6. namespace Drush\Psysh;
  7. use Psy\Command\Command as BaseCommand;
  8. use Symfony\Component\Console\Command\Command;
  9. use Symfony\Component\Console\Formatter\OutputFormatter;
  10. use Symfony\Component\Console\Input\InputArgument;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. /**
  14. * Help command.
  15. *
  16. * Lists available commands, and gives command-specific help when asked nicely.
  17. *
  18. * This replaces the PsySH help command to list commands by category.
  19. */
  20. class DrushHelpCommand extends BaseCommand {
  21. /**
  22. * Label for PsySH commands.
  23. */
  24. const PSYSH_CATEGORY = 'PsySH commands';
  25. /**
  26. * The currently set subcommand.
  27. *
  28. * @var \Symfony\Component\Console\Command\Command
  29. */
  30. protected $command;
  31. /**
  32. * {@inheritdoc}
  33. */
  34. protected function configure() {
  35. $this
  36. ->setName('help')
  37. ->setAliases(['?'])
  38. ->setDefinition([
  39. new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', NULL),
  40. ])
  41. ->setDescription('Show a list of commands. Type `help [foo]` for information about [foo].');
  42. }
  43. /**
  44. * Helper for setting a subcommand to retrieve help for.
  45. *
  46. * @param \Symfony\Component\Console\Command\Command $command
  47. */
  48. public function setCommand(Command $command) {
  49. $this->command = $command;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. protected function execute(InputInterface $input, OutputInterface $output) {
  55. if ($this->command !== NULL) {
  56. // Help for an individual command.
  57. $output->page($this->command->asText());
  58. $this->command = NULL;
  59. }
  60. elseif ($name = $input->getArgument('command_name')) {
  61. // Help for an individual command.
  62. $output->page($this->getApplication()->get($name)->asText());
  63. }
  64. else {
  65. $categories = [];
  66. // List available commands.
  67. $commands = $this->getApplication()->all();
  68. // Find the alignment width.
  69. $width = 0;
  70. foreach ($commands as $command) {
  71. $width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width;
  72. }
  73. $width += 2;
  74. foreach ($commands as $name => $command) {
  75. if ($name !== $command->getName()) {
  76. continue;
  77. }
  78. if ($command->getAliases()) {
  79. $aliases = sprintf(' <comment>Aliases:</comment> %s', implode(', ', $command->getAliases()));
  80. }
  81. else {
  82. $aliases = '';
  83. }
  84. if ($command instanceof DrushCommand) {
  85. $category = (string) $command->getCategory();
  86. }
  87. else {
  88. $category = static::PSYSH_CATEGORY;
  89. }
  90. if (!isset($categories[$category])) {
  91. $categories[$category] = [];
  92. }
  93. $categories[$category][] = sprintf(" <info>%-${width}s</info> %s%s", $name, $command->getDescription(), $aliases);
  94. }
  95. $messages = [];
  96. foreach ($categories as $name => $category) {
  97. $messages[] = '';
  98. $messages[] = sprintf('<comment>%s</comment>', OutputFormatter::escape($name));
  99. foreach ($category as $message) {
  100. $messages[] = $message;
  101. }
  102. }
  103. $output->page($messages);
  104. }
  105. }
  106. }