1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\console\controllers; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\Application; |
12
|
|
|
use yii\console\Controller; |
13
|
|
|
use yii\console\Exception; |
14
|
|
|
use yii\helpers\Console; |
15
|
|
|
use yii\helpers\Inflector; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Provides help information about console commands. |
19
|
|
|
* |
20
|
|
|
* This command displays the available command list in |
21
|
|
|
* the application or the detailed instructions about using |
22
|
|
|
* a specific command. |
23
|
|
|
* |
24
|
|
|
* This command can be used as follows on command line: |
25
|
|
|
* |
26
|
|
|
* ``` |
27
|
|
|
* yii help [command name] |
28
|
|
|
* ``` |
29
|
|
|
* |
30
|
|
|
* In the above, if the command name is not provided, all |
31
|
|
|
* available commands will be displayed. |
32
|
|
|
* |
33
|
|
|
* @property array $commands All available command names. This property is read-only. |
34
|
|
|
* |
35
|
|
|
* @author Qiang Xue <[email protected]> |
36
|
|
|
* @since 2.0 |
37
|
|
|
*/ |
38
|
|
|
class HelpController extends Controller |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* Displays available commands or the detailed information |
42
|
|
|
* about a particular command. |
43
|
|
|
* |
44
|
|
|
* @param string $command The name of the command to show help about. |
45
|
|
|
* If not provided, all available commands will be displayed. |
46
|
|
|
* @return int the exit status |
47
|
|
|
* @throws Exception if the command for help is unknown |
48
|
|
|
*/ |
49
|
3 |
|
public function actionIndex($command = null) |
50
|
|
|
{ |
51
|
3 |
|
if ($command !== null) { |
52
|
2 |
|
$result = Yii::$app->createController($command); |
53
|
2 |
|
if ($result === false) { |
54
|
|
|
$name = $this->ansiFormat($command, Console::FG_YELLOW); |
55
|
|
|
throw new Exception("No help for unknown command \"$name\"."); |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
list($controller, $actionID) = $result; |
59
|
|
|
|
60
|
2 |
|
$actions = $this->getActions($controller); |
61
|
2 |
|
if ($actionID !== '' || count($actions) === 1 && $actions[0] === $controller->defaultAction) { |
62
|
2 |
|
$this->getSubCommandHelp($controller, $actionID); |
63
|
|
|
} else { |
64
|
2 |
|
$this->getCommandHelp($controller); |
65
|
|
|
} |
66
|
|
|
} else { |
67
|
1 |
|
$this->getDefaultHelp(); |
68
|
|
|
} |
69
|
3 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* List all available controllers and actions in machine readable format. |
73
|
|
|
* This is used for shell completion. |
74
|
|
|
* @since 2.0.11 |
75
|
|
|
*/ |
76
|
2 |
|
public function actionList() |
77
|
|
|
{ |
78
|
2 |
|
$commands = $this->getCommandDescriptions(); |
79
|
2 |
|
foreach ($commands as $command => $description) { |
80
|
2 |
|
$result = Yii::$app->createController($command); |
81
|
2 |
|
if ($result === false || !($result[0] instanceof Controller)) { |
82
|
|
|
continue; |
83
|
|
|
} |
84
|
|
|
/** @var $controller Controller */ |
85
|
2 |
|
list($controller, $actionID) = $result; |
|
|
|
|
86
|
2 |
|
$actions = $this->getActions($controller); |
87
|
2 |
|
if (!empty($actions)) { |
88
|
2 |
|
$prefix = $controller->getUniqueId(); |
89
|
2 |
|
$this->stdout("$prefix\n"); |
90
|
2 |
|
foreach ($actions as $action) { |
91
|
2 |
|
$this->stdout("$prefix/$action\n"); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
2 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* List all available options for the $action in machine readable format. |
99
|
|
|
* This is used for shell completion. |
100
|
|
|
* |
101
|
|
|
* @param string $action route to action |
102
|
|
|
* @since 2.0.11 |
103
|
|
|
*/ |
104
|
1 |
|
public function actionListActionOptions($action) |
105
|
|
|
{ |
106
|
1 |
|
$result = Yii::$app->createController($action); |
107
|
|
|
|
108
|
1 |
|
if ($result === false || !($result[0] instanceof Controller)) { |
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** @var Controller $controller */ |
113
|
1 |
|
list($controller, $actionID) = $result; |
114
|
1 |
|
$action = $controller->createAction($actionID); |
115
|
1 |
|
if ($action === null) { |
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
$arguments = $controller->getActionArgsHelp($action); |
120
|
1 |
|
foreach ($arguments as $argument => $help) { |
121
|
1 |
|
$description = str_replace("\n", '', addcslashes($help['comment'], ':')) ?: $argument; |
122
|
1 |
|
$this->stdout($argument . ':' . $description . "\n"); |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
$this->stdout("\n"); |
126
|
1 |
|
$options = $controller->getActionOptionsHelp($action); |
127
|
1 |
|
foreach ($options as $argument => $help) { |
128
|
1 |
|
$description = str_replace("\n", '', addcslashes($help['comment'], ':')) ?: $argument; |
129
|
1 |
|
$this->stdout('--' . $argument . ':' . $description . "\n"); |
130
|
|
|
} |
131
|
1 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Displays usage information for $action. |
135
|
|
|
* |
136
|
|
|
* @param string $action route to action |
137
|
|
|
* @since 2.0.11 |
138
|
|
|
*/ |
139
|
1 |
|
public function actionUsage($action) |
140
|
|
|
{ |
141
|
1 |
|
$result = Yii::$app->createController($action); |
142
|
|
|
|
143
|
1 |
|
if ($result === false || !($result[0] instanceof Controller)) { |
144
|
|
|
return; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** @var Controller $controller */ |
148
|
1 |
|
list($controller, $actionID) = $result; |
149
|
1 |
|
$action = $controller->createAction($actionID); |
150
|
1 |
|
if ($action === null) { |
151
|
|
|
return; |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
$scriptName = $this->getScriptName(); |
155
|
1 |
|
if ($action->id === $controller->defaultAction) { |
156
|
|
|
$this->stdout($scriptName . ' ' . $this->ansiFormat($controller->getUniqueId(), Console::FG_YELLOW)); |
157
|
|
|
} else { |
158
|
1 |
|
$this->stdout($scriptName . ' ' . $this->ansiFormat($action->getUniqueId(), Console::FG_YELLOW)); |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
$args = $controller->getActionArgsHelp($action); |
162
|
1 |
|
foreach ($args as $name => $arg) { |
163
|
1 |
|
if ($arg['required']) { |
164
|
1 |
|
$this->stdout(' <' . $name . '>', Console::FG_CYAN); |
165
|
|
|
} else { |
166
|
1 |
|
$this->stdout(' [' . $name . ']', Console::FG_CYAN); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
1 |
|
$this->stdout("\n"); |
171
|
1 |
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Returns all available command names. |
175
|
|
|
* @return array all available command names |
176
|
|
|
*/ |
177
|
18 |
|
public function getCommands() |
178
|
|
|
{ |
179
|
18 |
|
$commands = $this->getModuleCommands(Yii::$app); |
180
|
18 |
|
sort($commands); |
181
|
18 |
|
return array_unique($commands); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Returns an array of commands an their descriptions. |
186
|
|
|
* @return array all available commands as keys and their description as values. |
187
|
|
|
*/ |
188
|
3 |
|
protected function getCommandDescriptions() |
189
|
|
|
{ |
190
|
3 |
|
$descriptions = []; |
191
|
3 |
|
foreach ($this->getCommands() as $command) { |
192
|
3 |
|
$description = ''; |
193
|
|
|
|
194
|
3 |
|
$result = Yii::$app->createController($command); |
195
|
3 |
|
if ($result !== false && $result[0] instanceof Controller) { |
196
|
3 |
|
list($controller, $actionID) = $result; |
|
|
|
|
197
|
|
|
/** @var Controller $controller */ |
198
|
3 |
|
$description = $controller->getHelpSummary(); |
199
|
|
|
} |
200
|
|
|
|
201
|
3 |
|
$descriptions[$command] = $description; |
202
|
|
|
} |
203
|
|
|
|
204
|
3 |
|
return $descriptions; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Returns all available actions of the specified controller. |
209
|
|
|
* @param Controller $controller the controller instance |
210
|
|
|
* @return array all available action IDs. |
211
|
|
|
*/ |
212
|
20 |
|
public function getActions($controller) |
213
|
|
|
{ |
214
|
20 |
|
$actions = array_keys($controller->actions()); |
215
|
20 |
|
$class = new \ReflectionClass($controller); |
216
|
20 |
|
foreach ($class->getMethods() as $method) { |
217
|
20 |
|
$name = $method->getName(); |
218
|
20 |
|
if ($name !== 'actions' && $method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0) { |
219
|
20 |
|
$actions[] = Inflector::camel2id(substr($name, 6), '-', true); |
220
|
|
|
} |
221
|
|
|
} |
222
|
20 |
|
sort($actions); |
223
|
|
|
|
224
|
20 |
|
return array_unique($actions); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Returns available commands of a specified module. |
229
|
|
|
* @param \yii\base\Module $module the module instance |
230
|
|
|
* @return array the available command names |
231
|
|
|
*/ |
232
|
18 |
|
protected function getModuleCommands($module) |
233
|
|
|
{ |
234
|
18 |
|
$prefix = $module instanceof Application ? '' : $module->getUniqueId() . '/'; |
235
|
|
|
|
236
|
18 |
|
$commands = []; |
237
|
18 |
|
foreach (array_keys($module->controllerMap) as $id) { |
238
|
18 |
|
$commands[] = $prefix . $id; |
239
|
|
|
} |
240
|
|
|
|
241
|
18 |
|
foreach ($module->getModules() as $id => $child) { |
242
|
1 |
|
if (($child = $module->getModule($id)) === null) { |
243
|
|
|
continue; |
244
|
|
|
} |
245
|
1 |
|
foreach ($this->getModuleCommands($child) as $command) { |
246
|
1 |
|
$commands[] = $command; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
18 |
|
$controllerPath = $module->getControllerPath(); |
251
|
18 |
|
if (is_dir($controllerPath)) { |
252
|
1 |
|
$files = scandir($controllerPath); |
253
|
1 |
|
foreach ($files as $file) { |
254
|
1 |
|
if (!empty($file) && substr_compare($file, 'Controller.php', -14, 14) === 0) { |
255
|
1 |
|
$controllerClass = $module->controllerNamespace . '\\' . substr(basename($file), 0, -4); |
256
|
1 |
|
if ($this->validateControllerClass($controllerClass)) { |
257
|
1 |
|
$commands[] = $prefix . Inflector::camel2id(substr(basename($file), 0, -14), '-', true); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
18 |
|
return $commands; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Validates if the given class is a valid console controller class. |
268
|
|
|
* @param string $controllerClass |
269
|
|
|
* @return bool |
270
|
|
|
*/ |
271
|
1 |
|
protected function validateControllerClass($controllerClass) |
272
|
|
|
{ |
273
|
1 |
|
if (class_exists($controllerClass)) { |
274
|
1 |
|
$class = new \ReflectionClass($controllerClass); |
275
|
1 |
|
return !$class->isAbstract() && $class->isSubclassOf('yii\console\Controller'); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return false; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Displays all available commands. |
283
|
|
|
*/ |
284
|
1 |
|
protected function getDefaultHelp() |
285
|
|
|
{ |
286
|
1 |
|
$commands = $this->getCommandDescriptions(); |
287
|
1 |
|
$this->stdout($this->getDefaultHelpHeader()); |
288
|
1 |
|
if (!empty($commands)) { |
289
|
1 |
|
$this->stdout("\nThe following commands are available:\n\n", Console::BOLD); |
290
|
1 |
|
$len = 0; |
291
|
1 |
|
foreach ($commands as $command => $description) { |
292
|
1 |
|
$result = Yii::$app->createController($command); |
293
|
1 |
|
if ($result !== false && $result[0] instanceof Controller) { |
294
|
|
|
/** @var $controller Controller */ |
295
|
1 |
|
list($controller, $actionID) = $result; |
|
|
|
|
296
|
1 |
|
$actions = $this->getActions($controller); |
297
|
1 |
|
if (!empty($actions)) { |
298
|
1 |
|
$prefix = $controller->getUniqueId(); |
299
|
1 |
|
foreach ($actions as $action) { |
300
|
1 |
|
$string = $prefix . '/' . $action; |
301
|
1 |
|
if ($action === $controller->defaultAction) { |
302
|
1 |
|
$string .= ' (default)'; |
303
|
|
|
} |
304
|
1 |
|
if (($l = strlen($string)) > $len) { |
305
|
1 |
|
$len = $l; |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
} elseif (($l = strlen($command)) > $len) { |
310
|
1 |
|
$len = $l; |
311
|
|
|
} |
312
|
|
|
} |
313
|
1 |
|
foreach ($commands as $command => $description) { |
314
|
1 |
|
$this->stdout('- ' . $this->ansiFormat($command, Console::FG_YELLOW)); |
315
|
1 |
|
$this->stdout(str_repeat(' ', $len + 4 - strlen($command))); |
316
|
1 |
|
$this->stdout(Console::wrapText($description, $len + 4 + 2), Console::BOLD); |
317
|
1 |
|
$this->stdout("\n"); |
318
|
|
|
|
319
|
1 |
|
$result = Yii::$app->createController($command); |
320
|
1 |
|
if ($result !== false && $result[0] instanceof Controller) { |
321
|
1 |
|
list($controller, $actionID) = $result; |
|
|
|
|
322
|
1 |
|
$actions = $this->getActions($controller); |
323
|
1 |
|
if (!empty($actions)) { |
324
|
1 |
|
$prefix = $controller->getUniqueId(); |
325
|
1 |
|
foreach ($actions as $action) { |
326
|
1 |
|
$string = ' ' . $prefix . '/' . $action; |
327
|
1 |
|
$this->stdout(' ' . $this->ansiFormat($string, Console::FG_GREEN)); |
328
|
1 |
|
if ($action === $controller->defaultAction) { |
329
|
1 |
|
$string .= ' (default)'; |
330
|
1 |
|
$this->stdout(' (default)', Console::FG_YELLOW); |
331
|
|
|
} |
332
|
1 |
|
$summary = $controller->getActionHelpSummary($controller->createAction($action)); |
333
|
1 |
|
if ($summary !== '') { |
334
|
1 |
|
$this->stdout(str_repeat(' ', $len + 4 - strlen($string))); |
335
|
1 |
|
$this->stdout(Console::wrapText($summary, $len + 4 + 2)); |
336
|
|
|
} |
337
|
1 |
|
$this->stdout("\n"); |
338
|
|
|
} |
339
|
|
|
} |
340
|
1 |
|
$this->stdout("\n"); |
341
|
|
|
} |
342
|
|
|
} |
343
|
1 |
|
$scriptName = $this->getScriptName(); |
344
|
1 |
|
$this->stdout("\nTo see the help of each command, enter:\n", Console::BOLD); |
345
|
1 |
|
$this->stdout("\n $scriptName " . $this->ansiFormat('help', Console::FG_YELLOW) . ' ' |
346
|
1 |
|
. $this->ansiFormat('<command-name>', Console::FG_CYAN) . "\n\n"); |
347
|
|
|
} else { |
348
|
|
|
$this->stdout("\nNo commands are found.\n\n", Console::BOLD); |
349
|
|
|
} |
350
|
1 |
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Displays the overall information of the command. |
354
|
|
|
* @param Controller $controller the controller instance |
355
|
|
|
*/ |
356
|
|
|
protected function getCommandHelp($controller) |
357
|
|
|
{ |
358
|
|
|
$controller->color = $this->color; |
359
|
|
|
|
360
|
|
|
$this->stdout("\nDESCRIPTION\n", Console::BOLD); |
361
|
|
|
$comment = $controller->getHelp(); |
362
|
|
|
if ($comment !== '') { |
363
|
|
|
$this->stdout("\n$comment\n\n"); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
$actions = $this->getActions($controller); |
367
|
|
|
if (!empty($actions)) { |
368
|
|
|
$this->stdout("\nSUB-COMMANDS\n\n", Console::BOLD); |
369
|
|
|
$prefix = $controller->getUniqueId(); |
370
|
|
|
|
371
|
|
|
$maxlen = 5; |
372
|
|
|
foreach ($actions as $action) { |
373
|
|
|
$len = strlen($prefix . '/' . $action) + 2 + ($action === $controller->defaultAction ? 10 : 0); |
374
|
|
|
if ($maxlen < $len) { |
375
|
|
|
$maxlen = $len; |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
foreach ($actions as $action) { |
379
|
|
|
$this->stdout('- ' . $this->ansiFormat($prefix . '/' . $action, Console::FG_YELLOW)); |
380
|
|
|
$len = strlen($prefix . '/' . $action) + 2; |
381
|
|
|
if ($action === $controller->defaultAction) { |
382
|
|
|
$this->stdout(' (default)', Console::FG_GREEN); |
383
|
|
|
$len += 10; |
384
|
|
|
} |
385
|
|
|
$summary = $controller->getActionHelpSummary($controller->createAction($action)); |
|
|
|
|
386
|
|
|
if ($summary !== '') { |
387
|
|
|
$this->stdout(str_repeat(' ', $maxlen - $len + 2) . Console::wrapText($summary, $maxlen + 2)); |
388
|
|
|
} |
389
|
|
|
$this->stdout("\n"); |
390
|
|
|
} |
391
|
|
|
$scriptName = $this->getScriptName(); |
392
|
|
|
$this->stdout("\nTo see the detailed information about individual sub-commands, enter:\n"); |
393
|
|
|
$this->stdout("\n $scriptName " . $this->ansiFormat('help', Console::FG_YELLOW) . ' ' |
394
|
|
|
. $this->ansiFormat('<sub-command>', Console::FG_CYAN) . "\n\n"); |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Displays the detailed information of a command action. |
400
|
|
|
* @param Controller $controller the controller instance |
401
|
|
|
* @param string $actionID action ID |
402
|
|
|
* @throws Exception if the action does not exist |
403
|
|
|
*/ |
404
|
2 |
|
protected function getSubCommandHelp($controller, $actionID) |
405
|
|
|
{ |
406
|
2 |
|
$action = $controller->createAction($actionID); |
407
|
2 |
|
if ($action === null) { |
408
|
|
|
$name = $this->ansiFormat(rtrim($controller->getUniqueId() . '/' . $actionID, '/'), Console::FG_YELLOW); |
409
|
|
|
throw new Exception("No help for unknown sub-command \"$name\"."); |
410
|
|
|
} |
411
|
|
|
|
412
|
2 |
|
$description = $controller->getActionHelp($action); |
413
|
2 |
|
if ($description !== '') { |
414
|
2 |
|
$this->stdout("\nDESCRIPTION\n", Console::BOLD); |
415
|
2 |
|
$this->stdout("\n$description\n\n"); |
416
|
|
|
} |
417
|
|
|
|
418
|
2 |
|
$this->stdout("\nUSAGE\n\n", Console::BOLD); |
419
|
2 |
|
$scriptName = $this->getScriptName(); |
420
|
2 |
|
if ($action->id === $controller->defaultAction) { |
421
|
2 |
|
$this->stdout($scriptName . ' ' . $this->ansiFormat($controller->getUniqueId(), Console::FG_YELLOW)); |
422
|
|
|
} else { |
423
|
|
|
$this->stdout($scriptName . ' ' . $this->ansiFormat($action->getUniqueId(), Console::FG_YELLOW)); |
424
|
|
|
} |
425
|
|
|
|
426
|
2 |
|
$args = $controller->getActionArgsHelp($action); |
427
|
2 |
|
foreach ($args as $name => $arg) { |
428
|
2 |
|
if ($arg['required']) { |
429
|
|
|
$this->stdout(' <' . $name . '>', Console::FG_CYAN); |
430
|
|
|
} else { |
431
|
2 |
|
$this->stdout(' [' . $name . ']', Console::FG_CYAN); |
432
|
|
|
} |
433
|
|
|
} |
434
|
|
|
|
435
|
2 |
|
$options = $controller->getActionOptionsHelp($action); |
436
|
2 |
|
$options[\yii\console\Application::OPTION_APPCONFIG] = [ |
437
|
|
|
'type' => 'string', |
438
|
|
|
'default' => null, |
439
|
|
|
'comment' => "custom application configuration file path.\nIf not set, default application configuration is used.", |
440
|
|
|
]; |
441
|
2 |
|
ksort($options); |
442
|
|
|
|
443
|
2 |
|
if (!empty($options)) { |
444
|
2 |
|
$this->stdout(' [...options...]', Console::FG_RED); |
445
|
|
|
} |
446
|
2 |
|
$this->stdout("\n\n"); |
447
|
|
|
|
448
|
2 |
|
if (!empty($args)) { |
449
|
2 |
|
foreach ($args as $name => $arg) { |
450
|
2 |
|
$this->stdout($this->formatOptionHelp( |
451
|
2 |
|
'- ' . $this->ansiFormat($name, Console::FG_CYAN), |
452
|
2 |
|
$arg['required'], |
453
|
2 |
|
$arg['type'], |
454
|
2 |
|
$arg['default'], |
455
|
2 |
|
$arg['comment']) . "\n\n"); |
456
|
|
|
} |
457
|
|
|
} |
458
|
|
|
|
459
|
2 |
|
if (!empty($options)) { |
460
|
2 |
|
$this->stdout("\nOPTIONS\n\n", Console::BOLD); |
461
|
2 |
|
foreach ($options as $name => $option) { |
462
|
2 |
|
$this->stdout($this->formatOptionHelp( |
463
|
2 |
|
$this->ansiFormat('--' . $name . $this->formatOptionAliases($controller, $name), Console::FG_RED, empty($option['required']) ? Console::FG_RED : Console::BOLD), |
464
|
2 |
|
!empty($option['required']), |
465
|
2 |
|
$option['type'], |
466
|
2 |
|
$option['default'], |
467
|
2 |
|
$option['comment']) . "\n\n"); |
468
|
|
|
} |
469
|
|
|
} |
470
|
2 |
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* Generates a well-formed string for an argument or option. |
474
|
|
|
* @param string $name the name of the argument or option |
475
|
|
|
* @param bool $required whether the argument is required |
476
|
|
|
* @param string $type the type of the option or argument |
477
|
|
|
* @param mixed $defaultValue the default value of the option or argument |
478
|
|
|
* @param string $comment comment about the option or argument |
479
|
|
|
* @return string the formatted string for the argument or option |
480
|
|
|
*/ |
481
|
2 |
|
protected function formatOptionHelp($name, $required, $type, $defaultValue, $comment) |
482
|
|
|
{ |
483
|
2 |
|
$comment = trim($comment); |
484
|
2 |
|
$type = trim($type); |
485
|
2 |
|
if (strncmp($type, 'bool', 4) === 0) { |
486
|
2 |
|
$type = 'boolean, 0 or 1'; |
487
|
|
|
} |
488
|
|
|
|
489
|
2 |
|
if ($defaultValue !== null && !is_array($defaultValue)) { |
490
|
2 |
|
if ($type === null) { |
491
|
|
|
$type = gettype($defaultValue); |
492
|
|
|
} |
493
|
2 |
|
if (is_bool($defaultValue)) { |
494
|
|
|
// show as integer to avoid confusion |
495
|
2 |
|
$defaultValue = (int) $defaultValue; |
496
|
|
|
} |
497
|
2 |
|
if (is_string($defaultValue)) { |
498
|
1 |
|
$defaultValue = "'" . $defaultValue . "'"; |
499
|
|
|
} else { |
500
|
2 |
|
$defaultValue = var_export($defaultValue, true); |
501
|
|
|
} |
502
|
2 |
|
$doc = "$type (defaults to $defaultValue)"; |
503
|
|
|
} else { |
504
|
2 |
|
$doc = $type; |
505
|
|
|
} |
506
|
|
|
|
507
|
2 |
|
if ($doc === '') { |
508
|
|
|
$doc = $comment; |
509
|
2 |
|
} elseif ($comment !== '') { |
510
|
2 |
|
$doc .= "\n" . preg_replace('/^/m', ' ', $comment); |
511
|
|
|
} |
512
|
|
|
|
513
|
2 |
|
$name = $required ? "$name (required)" : $name; |
514
|
|
|
|
515
|
2 |
|
return $doc === '' ? $name : "$name: $doc"; |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* @param Controller $controller the controller instance |
520
|
|
|
* @param string $option the option name |
521
|
|
|
* @return string the formatted string for the alias argument or option |
522
|
|
|
* @since 2.0.8 |
523
|
|
|
*/ |
524
|
2 |
|
protected function formatOptionAliases($controller, $option) |
525
|
|
|
{ |
526
|
2 |
|
$aliases = $controller->optionAliases(); |
527
|
2 |
|
foreach ($aliases as $name => $value) { |
528
|
2 |
|
if ($value === $option) { |
529
|
2 |
|
return ', -' . $name; |
530
|
|
|
} |
531
|
|
|
} |
532
|
|
|
|
533
|
2 |
|
return ''; |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
/** |
537
|
|
|
* @return string the name of the cli script currently running. |
538
|
|
|
*/ |
539
|
4 |
|
protected function getScriptName() |
540
|
|
|
{ |
541
|
4 |
|
return basename(Yii::$app->request->scriptFile); |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* Return a default help header. |
546
|
|
|
* @return string default help header. |
547
|
|
|
* @since 2.0.11 |
548
|
|
|
*/ |
549
|
1 |
|
protected function getDefaultHelpHeader() |
550
|
|
|
{ |
551
|
1 |
|
return "\nThis is Yii version " . \Yii::getVersion() . ".\n"; |
552
|
|
|
} |
553
|
|
|
} |
554
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.