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