/
CompletionShell.php
260 lines (228 loc) · 5.97 KB
/
CompletionShell.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
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP Project
* @package Cake.Console.Command
* @since CakePHP v 2.5
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('CommandListShell', 'Console/Command');
/**
* Provide command completion shells such as bash.
*
* @package Cake.Console.Command
*/
class CompletionShell extends CommandListShell {
/**
* Echo no header by overriding the startup method
*
* @return void
*/
public function startup() {
}
/**
* Not called by the autocomplete shell - this is for curious users
*
* @return void
*/
public function main() {
return $this->out($this->OptionParser->help());
}
/**
* list commands
*
* @return void
*/
public function commands() {
$options = $this->_commands();
return $this->_output($options);
}
/**
* list options for the named command
*
* @return void
*/
public function options() {
if (!$this->args) {
$parser = new ConsoleOptionParser();
} else {
$Shell = $this->_getShell($this->args[0]);
if (!$Shell) {
$parser = new ConsoleOptionParser();
} else {
$parser = $Shell->getOptionParser();
}
}
$options = array();
$array = $parser->options();
foreach ($array as $name => $obj) {
$options[] = "--$name";
$short = $obj->short();
if ($short) {
$options[] = "-$short";
}
}
return $this->_output($options);
}
/**
* list subcommands for the named command
*
* @return void
*/
public function subCommands() {
if (!$this->args) {
return $this->_output();
}
$options = $this->_subCommands($this->args[0]);
return $this->_output($options);
}
/**
* Guess autocomplete from the whole argument string
*
* @return void
*/
public function fuzzy() {
return $this->_output();
}
/**
* getOptionParser for _this_ shell
*
* @return ConsoleOptionParser
*/
public function getOptionParser() {
$translationDomain = 'bash_completion';
$parser = AppShell::getOptionParser();
$parser->description(__d($translationDomain, 'Used by bash to autocomplete command name, options and arguments'))
->addSubcommand('commands', array(
'help' => __d($translationDomain, 'Output a list of available commands'),
'parser' => array(
'description' => __d($translationDomain, 'List all availables'),
'arguments' => array(
)
)
))->addSubcommand('subcommands', array(
'help' => __d($translationDomain, 'Output a list of available subcommands'),
'parser' => array(
'description' => __d($translationDomain, 'List subcommands for a command'),
'arguments' => array(
'command' => array(
'help' => __d($translationDomain, 'The command name'),
'required' => true,
)
)
)
))->addSubcommand('options', array(
'help' => __d($translationDomain, 'Output a list of available options'),
'parser' => array(
'description' => __d($translationDomain, 'List options'),
'arguments' => array(
'command' => array(
'help' => __d($translationDomain, 'The command name'),
'required' => false,
)
)
)
))->epilog(
array(
'This command is not intended to be called manually',
)
);
return $parser;
}
/**
* Return a list of all commands
*
* @return array
*/
protected function _commands() {
$shellList = $this->_getShellList();
unset($shellList['Completion']);
$options = array();
foreach ($shellList as $type => $commands) {
$prefix = '';
if (!in_array($type, array('app', 'core', 'APP', 'CORE'))) {
$prefix = $type . '.';
}
foreach ($commands as $shell) {
$options[] = $prefix . $shell;
}
}
return $options;
}
/**
* Return a list of subcommands for a given command
*
* @param string $commandName
* @return array
*/
protected function _subCommands($commandName) {
$Shell = $this->_getShell($commandName);
if (!$Shell) {
return array();
}
$return = array();
$taskMap = TaskCollection::normalizeObjectArray((array)$Shell->tasks);
foreach ($taskMap as $task => $properties) {
$return[] = $task;
}
$return = array_map('Inflector::underscore', $return);
$ShellReflection = new ReflectionClass('AppShell');
$shellMethods = $ShellReflection->getMethods(ReflectionMethod::IS_PUBLIC);
$shellMethodNames = array('main', 'help');
foreach ($shellMethods as $method) {
$shellMethodNames[] = $method->getName();
}
$Reflection = new ReflectionClass($Shell);
$methods = $Reflection->getMethods(ReflectionMethod::IS_PUBLIC);
$methodNames = array();
foreach ($methods as $method) {
$methodNames[] = $method->getName();
}
$return += array_diff($methodNames, $shellMethodNames);
sort($return);
return $return;
}
/**
* Get Shell instance for the given command
*
* @param mixed $commandName
* @return mixed
*/
protected function _getShell($commandName) {
list($plugin, $name) = pluginSplit($commandName, true);
if ($plugin === 'CORE.' || $plugin === 'APP.' || $plugin === 'core.' || $plugin === 'app.') {
$commandName = $name;
$plugin = '';
}
if (!in_array($commandName, $this->_commands())) {
return false;
}
$name = Inflector::camelize($name);
$plugin = Inflector::camelize($plugin);
$class = $name . 'Shell';
APP::uses($class, $plugin . 'Console/Command');
$Shell = new $class();
$Shell->plugin = trim($plugin, '.');
$Shell->initialize();
$Shell->loadTasks();
return $Shell;
}
/**
* Emit results as a string, space delimited
*
* @param array $options
* @return void
*/
protected function _output($options = array()) {
if ($options) {
return $this->out(implode($options, ' '));
}
}
}