fb3f0a11c34086de8b68c9be70ff92161b438185
[yaffs-website] / vendor / drush / drush / src / 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     /**
28      * Label for PsySH commands.
29      */
30     const PSYSH_CATEGORY = 'PsySH';
31
32     /**
33      * The currently set subcommand.
34      *
35      * @var \Symfony\Component\Console\Command\Command
36      */
37     protected $command;
38
39     /**
40      * {@inheritdoc}
41      */
42     protected function configure()
43     {
44         $this
45         ->setName('help')
46         ->setAliases(['?'])
47         ->setDefinition([
48         new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', null),
49         ])
50         ->setDescription('Show a list of commands. Type `help [foo]` for information about [foo].');
51     }
52
53     /**
54      * Helper for setting a subcommand to retrieve help for.
55      *
56      * @param \Symfony\Component\Console\Command\Command $command
57      */
58     public function setCommand(Command $command)
59     {
60         $this->command = $command;
61     }
62
63     /**
64      * {@inheritdoc}
65      */
66     protected function execute(InputInterface $input, OutputInterface $output)
67     {
68         if ($this->command !== null) {
69             // Help for an individual command.
70             $output->page($this->command->asText());
71             $this->command = null;
72         } elseif ($name = $input->getArgument('command_name')) {
73             // Help for an individual command.
74             $output->page($this->getApplication()->get($name)->asText());
75         } else {
76             $namespaces = [];
77
78             // List available commands.
79             $commands = $this->getApplication()->all();
80
81             // Find the alignment width.
82             $width = 0;
83             foreach ($commands as $command) {
84                 $width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width;
85             }
86             $width += 2;
87
88             foreach ($commands as $name => $command) {
89                 if ($name !== $command->getName()) {
90                     continue;
91                 }
92
93                 if ($command->getAliases()) {
94                     $aliases = sprintf('  <comment>Aliases:</comment> %s', implode(', ', $command->getAliases()));
95                 } else {
96                     $aliases = '';
97                 }
98
99                 $namespace = '';
100                 if ($command instanceof DrushCommand) {
101                     $namespace = $command->getNamespace();
102                 }
103
104                 if (empty($namespace)) {
105                     $namespace = static::PSYSH_CATEGORY;
106                 }
107
108                 if (!isset($namespaces[$namespace])) {
109                     $namespaces[$namespace] = [];
110                 }
111
112                 $namespaces[$namespace][] = sprintf("    <info>%-${width}s</info> %s%s", $name, $command->getDescription(), $aliases);
113             }
114
115             $messages = [];
116
117             foreach ($namespaces as $namespace => $command_messages) {
118                 $messages[] = '';
119                 $messages[] = sprintf('<comment>%s</comment>', OutputFormatter::escape($namespace));
120                 foreach ($command_messages as $command_message) {
121                     $messages[] = $command_message;
122                 }
123             }
124
125             $output->page($messages);
126         }
127     }
128 }