-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
CommandListShell.php
223 lines (205 loc) · 5.81 KB
/
CommandListShell.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
<?php
/**
* CommandListTest file
*
* PHP 5
*
* CakePHP : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc.
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc.
* @link http://cakephp.org CakePHP Project
* @since CakePHP v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('Inflector', 'Utility');
/**
* Shows a list of commands available from the console.
*
* @package cake.console.shells
*/
class CommandListShell extends Shell {
/**
* startup
*
* @return void
*/
public function startup() {
if (empty($this->params['xml'])) {
parent::startup();
}
}
/**
* Main function Prints out the list of shells.
*
* @return void
*/
public function main() {
if (empty($this->params['xml'])) {
$this->out(__d('cake_console', "<info>Current Paths:</info>"), 2);
$this->out(" -app: ". APP_DIR);
$this->out(" -working: " . rtrim(APP_PATH, DS));
$this->out(" -root: " . rtrim(ROOT, DS));
$this->out(" -core: " . rtrim(CORE_PATH, DS));
$this->out("");
$this->out(__d('cake_console', "<info>Changing Paths:</info>"), 2);
$this->out(__d('cake_console', "Your working path should be the same as your application path to change your path use the '-app' param."));
$this->out(__d('cake_console', "Example: -app relative/path/to/myapp or -app /absolute/path/to/myapp"), 2);
$this->out(__d('cake_console', "<info>Available Shells:</info>"), 2);
}
$shellList = $this->_getShellList();
if ($shellList) {
ksort($shellList);
if (empty($this->params['xml'])) {
if (!empty($this->params['sort'])) {
$this->_asSorted($shellList);
} else {
$this->_asText($shellList);
}
} else {
$this->_asXml($shellList);
}
}
}
/**
* Gets the shell command listing.
*
* @return array
*/
protected function _getShellList() {
$shellList = array();
$shells = App::objects('file', App::core('Console/Command'));
$shellList = $this->_appendShells('CORE', $shells, $shellList);
$appShells = App::objects('Console/Command', null, false);
$shellList = $this->_appendShells('app', $appShells, $shellList);
$plugins = CakePlugin::loaded();
foreach ($plugins as $plugin) {
$pluginShells = App::objects($plugin . '.Console/Command');
$shellList = $this->_appendShells($plugin, $pluginShells, $shellList);
}
return $shellList;
}
/**
* Scan the provided paths for shells, and append them into $shellList
*
* @return array
*/
protected function _appendShells($type, $shells, $shellList) {
foreach ($shells as $shell) {
$shell = Inflector::underscore(str_replace('Shell', '', $shell));
$shellList[$shell][$type] = $type;
}
return $shellList;
}
/**
* Output text.
*
* @return void
*/
protected function _asText($shellList) {
if (DS === '/') {
$width = exec('tput cols') - 2;
}
if (empty($width)) {
$width = 80;
}
$columns = max(1, floor($width / 30));
$rows = ceil(count($shellList) / $columns);
foreach ($shellList as $shell => $types) {
sort($types);
$shellList[$shell] = str_pad($shell . ' [' . implode ($types, ', ') . ']', $width / $columns);
}
$out = array_chunk($shellList, $rows);
for ($i = 0; $i < $rows; $i++) {
$row = '';
for ($j = 0; $j < $columns; $j++) {
if (!isset($out[$j][$i])) {
continue;
}
$row .= $out[$j][$i];
}
$this->out(" " . $row);
}
$this->out();
$this->out(__d('cake_console', "To run a command, type <info>cake shell_name [args]</info>"));
$this->out(__d('cake_console', "To get help on a specific command, type <info>cake shell_name --help</info>"), 2);
}
/**
* Generates the shell list sorted by where the shells are found.
*
* @return void
*/
protected function _asSorted($shellList) {
$grouped = array();
foreach ($shellList as $shell => $types) {
foreach ($types as $type) {
$type = Inflector::camelize($type);
if (empty($grouped[$type])) {
$grouped[$type] = array();
}
$grouped[$type][] = $shell;
}
}
if (!empty($grouped['App'])) {
sort($grouped['App'], SORT_STRING);
$this->out('[ App ]');
$this->out(' ' . implode(', ', $grouped['App']), 2);
unset($grouped['App']);
}
foreach ($grouped as $section => $shells) {
if ($section == 'CORE') {
continue;
}
sort($shells, SORT_STRING);
$this->out('[ ' . $section . ' ]');
$this->out(' ' . implode(', ', $shells), 2);
}
if (!empty($grouped['CORE'])) {
sort($grouped['CORE'], SORT_STRING);
$this->out('[ Core ]');
$this->out(' ' . implode(', ', $grouped['CORE']), 2);
}
$this->out();
}
/**
* Output as XML
*
* @return void
*/
protected function _asXml($shellList) {
$plugins = CakePlugin::loaded();
$shells = new SimpleXmlElement('<shells></shells>');
foreach ($shellList as $name => $location) {
$source = current($location);
$callable = $name;
if (in_array($source, $plugins)) {
$callable = Inflector::camelize($source) . '.' . $name;
}
$shell = $shells->addChild('shell');
$shell->addAttribute('name', $name);
$shell->addAttribute('call_as', $callable);
$shell->addAttribute('provider', $source);
$shell->addAttribute('help', $callable . ' -h');
}
$this->out($shells->saveXml());
}
/**
* get the option parser
*
* @return void
*/
public function getOptionParser() {
$parser = parent::getOptionParser();
return $parser->description(__d('cake_console', 'Get the list of available shells for this CakePHP application.'))
->addOption('xml', array(
'help' => __d('cake_console', 'Get the listing as XML.'),
'boolean' => true
))->addOption('sort', array(
'help' => __d('cake_console', 'Sorts the commands by where they are located.'),
'boolean' => true
));
}
}