-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
CompletionCommand.php
281 lines (254 loc) · 9.17 KB
/
CompletionCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP Project
* @since 2.5.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Command;
use Cake\Console\Arguments;
use Cake\Console\CommandCollection;
use Cake\Console\CommandCollectionAwareInterface;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Console\Shell;
use Cake\Utility\Inflector;
use ReflectionClass;
use ReflectionMethod;
/**
* Provide command completion shells such as bash.
*/
class CompletionCommand extends Command implements CommandCollectionAwareInterface
{
/**
* @var \Cake\Console\CommandCollection
*/
protected $commands;
/**
* Set the command collection used to get completion data on.
*
* @param \Cake\Console\CommandCollection $commands The command collection
* @return void
*/
public function setCommandCollection(CommandCollection $commands): void
{
$this->commands = $commands;
}
/**
* Gets the option parser instance and configures it.
*
* @param \Cake\Console\ConsoleOptionParser $parser The parser to build
* @return \Cake\Console\ConsoleOptionParser
*/
public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
$modes = [
'commands' => 'Output a list of available commands',
'subcommands' => 'Output a list of available sub-commands for a command',
'options' => 'Output a list of available options for a command and possible subcommand.',
'fuzzy' => 'Does nothing. Only for backwards compatibility',
];
$modeHelp = '';
foreach ($modes as $key => $help) {
$modeHelp .= "- <info>{$key}</info> {$help}\n";
}
$parser->setDescription(
'Used by shells like bash to autocomplete command name, options and arguments'
)->addArgument('mode', [
'help' => 'The type of thing to get completion on.',
'required' => true,
'choices' => array_keys($modes),
])->addArgument('command', [
'help' => 'The command name to get information on.',
'required' => false,
])->addArgument('subcommand', [
'help' => 'The sub-command related to command to get information on.',
'required' => false,
])->setEpilog([
'The various modes allow you to get help information on commands and their arguments.',
'The available modes are:',
'',
$modeHelp,
'',
'This command is not intended to be called manually, and should be invoked from a ' .
'terminal completion script.',
]);
return $parser;
}
/**
* Main function Prints out the list of commands.
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIo $io The console io
* @return int
*/
public function execute(Arguments $args, ConsoleIo $io): ?int
{
$mode = $args->getArgument('mode');
switch ($mode) {
case 'commands':
return $this->getCommands($args, $io);
case 'subcommands':
return $this->getSubcommands($args, $io);
case 'options':
return $this->getOptions($args, $io);
case 'fuzzy':
return static::CODE_SUCCESS;
default:
$io->err('Invalid mode chosen.');
}
return static::CODE_SUCCESS;
}
/**
* Get the list of defined commands.
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIo $io The console io
* @return int
*/
protected function getCommands(Arguments $args, ConsoleIo $io): int
{
$options = [];
foreach ($this->commands as $key => $value) {
$parts = explode(' ', $key);
$options[] = $parts[0];
}
$options = array_unique($options);
$io->out(implode(' ', $options));
return static::CODE_SUCCESS;
}
/**
* Get the list of defined sub-commands.
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIo $io The console io
* @return int
*/
protected function getSubcommands(Arguments $args, ConsoleIo $io): int
{
$name = $args->getArgument('command');
if ($name === null || !strlen($name)) {
return static::CODE_SUCCESS;
}
$options = [];
foreach ($this->commands as $key => $value) {
$parts = explode(' ', $key);
if ($parts[0] !== $name) {
continue;
}
// Space separate command name, collect
// hits as subcommands
if (count($parts) > 1) {
$options[] = implode(' ', array_slice($parts, 1));
continue;
}
// Handle class strings
if (is_string($value)) {
$reflection = new ReflectionClass($value);
$value = $reflection->newInstance();
}
if ($value instanceof Shell) {
$shellCommands = $this->shellSubcommands($value);
$options = array_merge($options, $shellCommands);
}
}
$options = array_unique($options);
$io->out(implode(' ', $options));
return static::CODE_SUCCESS;
}
/**
* Reflect the subcommands names out of a shell.
*
* @param \Cake\Console\Shell $shell The shell to get commands for
* @return string[] A list of commands
*/
protected function shellSubcommands(Shell $shell): array
{
$shell->initialize();
$shell->loadTasks();
$optionParser = $shell->getOptionParser();
$subcommands = $optionParser->subcommands();
$output = array_keys($subcommands);
// If there are no formal subcommands all methods
// on a shell are 'subcommands'
if (count($subcommands) === 0) {
/** @psalm-suppress DeprecatedClass */
$coreShellReflection = new ReflectionClass(Shell::class);
$reflection = new ReflectionClass($shell);
foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
if (
$shell->hasMethod($method->getName())
&& !$coreShellReflection->hasMethod($method->getName())
) {
$output[] = $method->getName();
}
}
}
$taskNames = array_map('Cake\Utility\Inflector::underscore', $shell->taskNames);
$output = array_merge($output, $taskNames);
return array_unique($output);
}
/**
* Get the options for a command or subcommand
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIo $io The console io
* @return int
*/
protected function getOptions(Arguments $args, ConsoleIo $io): ?int
{
$name = $args->getArgument('command');
$subcommand = $args->getArgument('subcommand');
$options = [];
foreach ($this->commands as $key => $value) {
$parts = explode(' ', $key);
if ($parts[0] !== $name) {
continue;
}
if ($subcommand && !isset($parts[1])) {
continue;
}
if ($subcommand && isset($parts[1]) && $parts[1] !== $subcommand) {
continue;
}
// Handle class strings
if (is_string($value)) {
$reflection = new ReflectionClass($value);
$value = $reflection->newInstance();
}
$parser = null;
if ($value instanceof Command) {
$parser = $value->getOptionParser();
}
if ($value instanceof Shell) {
$value->initialize();
$value->loadTasks();
$parser = $value->getOptionParser();
$subcommand = Inflector::camelize((string)$subcommand);
if ($subcommand && $value->hasTask($subcommand)) {
$parser = $value->{$subcommand}->getOptionParser();
}
}
if ($parser) {
foreach ($parser->options() as $name => $option) {
$options[] = "--$name";
$short = $option->short();
if ($short) {
$options[] = "-$short";
}
}
}
}
$options = array_unique($options);
$io->out(implode(' ', $options));
return static::CODE_SUCCESS;
}
}