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; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\Action; |
12
|
|
|
use yii\base\InlineAction; |
13
|
|
|
use yii\base\InvalidRouteException; |
14
|
|
|
use yii\helpers\Console; |
15
|
|
|
use yii\helpers\Inflector; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Controller is the base class of console command classes. |
19
|
|
|
* |
20
|
|
|
* A console controller consists of one or several actions known as sub-commands. |
21
|
|
|
* Users call a console command by specifying the corresponding route which identifies a controller action. |
22
|
|
|
* The `yii` program is used when calling a console command, like the following: |
23
|
|
|
* |
24
|
|
|
* ``` |
25
|
|
|
* yii <route> [--param1=value1 --param2 ...] |
26
|
|
|
* ``` |
27
|
|
|
* |
28
|
|
|
* where `<route>` is a route to a controller action and the params will be populated as properties of a command. |
29
|
|
|
* See [[options()]] for details. |
30
|
|
|
* |
31
|
|
|
* @property string $help This property is read-only. |
32
|
|
|
* @property string $helpSummary This property is read-only. |
33
|
|
|
* @property array $passedOptionValues The properties corresponding to the passed options. This property is |
34
|
|
|
* read-only. |
35
|
|
|
* @property array $passedOptions The names of the options passed during execution. This property is |
36
|
|
|
* read-only. |
37
|
|
|
* @property Request $request |
38
|
|
|
* @property Response $response |
39
|
|
|
* |
40
|
|
|
* @author Qiang Xue <[email protected]> |
41
|
|
|
* @since 2.0 |
42
|
|
|
*/ |
43
|
|
|
class Controller extends \yii\base\Controller |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* @deprecated since 2.0.13. Use [[ExitCode::OK]] instead. |
47
|
|
|
*/ |
48
|
|
|
const EXIT_CODE_NORMAL = 0; |
49
|
|
|
/** |
50
|
|
|
* @deprecated since 2.0.13. Use [[ExitCode::UNSPECIFIED_ERROR]] instead. |
51
|
|
|
*/ |
52
|
|
|
const EXIT_CODE_ERROR = 1; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var bool whether to run the command interactively. |
56
|
|
|
*/ |
57
|
|
|
public $interactive = true; |
58
|
|
|
/** |
59
|
|
|
* @var bool whether to enable ANSI color in the output. |
60
|
|
|
* If not set, ANSI color will only be enabled for terminals that support it. |
61
|
|
|
*/ |
62
|
|
|
public $color; |
63
|
|
|
/** |
64
|
|
|
* @var bool whether to display help information about current command. |
65
|
|
|
* @since 2.0.10 |
66
|
|
|
*/ |
67
|
|
|
public $help; |
68
|
|
|
/** |
69
|
|
|
* @var bool if TRUE - script finish with `ExitCode::OK` in case of exception. |
70
|
|
|
* FALSE - `ExitCode::UNSPECIFIED_ERROR`. |
71
|
|
|
* Default: `YII_ENV_TEST` |
72
|
|
|
* @since 2.0.36 |
73
|
|
|
*/ |
74
|
|
|
public $silentExitOnException; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var array the options passed during execution. |
78
|
|
|
*/ |
79
|
|
|
private $_passedOptions = []; |
80
|
|
|
|
81
|
|
|
|
82
|
212 |
|
public function beforeAction($action) |
83
|
|
|
{ |
84
|
212 |
|
$silentExit = $this->silentExitOnException !== null ? $this->silentExitOnException : YII_ENV_TEST; |
85
|
212 |
|
Yii::$app->errorHandler->silentExitOnException = $silentExit; |
86
|
|
|
|
87
|
212 |
|
return parent::beforeAction($action); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns a value indicating whether ANSI color is enabled. |
92
|
|
|
* |
93
|
|
|
* ANSI color is enabled only if [[color]] is set true or is not set |
94
|
|
|
* and the terminal supports ANSI color. |
95
|
|
|
* |
96
|
|
|
* @param resource $stream the stream to check. |
97
|
|
|
* @return bool Whether to enable ANSI style in output. |
98
|
|
|
*/ |
99
|
7 |
|
public function isColorEnabled($stream = \STDOUT) |
100
|
|
|
{ |
101
|
7 |
|
return $this->color === null ? Console::streamSupportsAnsiColors($stream) : $this->color; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Runs an action with the specified action ID and parameters. |
106
|
|
|
* If the action ID is empty, the method will use [[defaultAction]]. |
107
|
|
|
* @param string $id the ID of the action to be executed. |
108
|
|
|
* @param array $params the parameters (name-value pairs) to be passed to the action. |
109
|
|
|
* @return int the status of the action execution. 0 means normal, other values mean abnormal. |
110
|
|
|
* @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully. |
111
|
|
|
* @throws Exception if there are unknown options or missing arguments |
112
|
|
|
* @see createAction |
113
|
|
|
*/ |
114
|
212 |
|
public function runAction($id, $params = []) |
115
|
|
|
{ |
116
|
212 |
|
if (!empty($params)) { |
117
|
|
|
// populate options here so that they are available in beforeAction(). |
118
|
200 |
|
$options = $this->options($id === '' ? $this->defaultAction : $id); |
119
|
200 |
|
if (isset($params['_aliases'])) { |
120
|
1 |
|
$optionAliases = $this->optionAliases(); |
121
|
1 |
|
foreach ($params['_aliases'] as $name => $value) { |
122
|
1 |
|
if (array_key_exists($name, $optionAliases)) { |
123
|
1 |
|
$params[$optionAliases[$name]] = $value; |
124
|
|
|
} else { |
125
|
|
|
$message = Yii::t('yii', 'Unknown alias: -{name}', ['name' => $name]); |
126
|
|
|
if (!empty($optionAliases)) { |
127
|
|
|
$aliasesAvailable = []; |
128
|
|
|
foreach ($optionAliases as $alias => $option) { |
129
|
|
|
$aliasesAvailable[] = '-' . $alias . ' (--' . $option . ')'; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$message .= '. ' . Yii::t('yii', 'Aliases available: {aliases}', [ |
133
|
|
|
'aliases' => implode(', ', $aliasesAvailable) |
134
|
|
|
]); |
135
|
|
|
} |
136
|
1 |
|
throw new Exception($message); |
137
|
|
|
} |
138
|
|
|
} |
139
|
1 |
|
unset($params['_aliases']); |
140
|
|
|
} |
141
|
200 |
|
foreach ($params as $name => $value) { |
142
|
|
|
// Allow camelCase options to be entered in kebab-case |
143
|
200 |
|
if (!in_array($name, $options, true) && strpos($name, '-') !== false) { |
144
|
1 |
|
$kebabName = $name; |
145
|
1 |
|
$altName = lcfirst(Inflector::id2camel($kebabName)); |
146
|
1 |
|
if (in_array($altName, $options, true)) { |
147
|
1 |
|
$name = $altName; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
200 |
|
if (in_array($name, $options, true)) { |
152
|
51 |
|
$default = $this->$name; |
153
|
51 |
|
if (is_array($default)) { |
154
|
51 |
|
$this->$name = preg_split('/\s*,\s*(?![^()]*\))/', $value); |
155
|
11 |
|
} elseif ($default !== null) { |
156
|
10 |
|
settype($value, gettype($default)); |
157
|
10 |
|
$this->$name = $value; |
158
|
|
|
} else { |
159
|
1 |
|
$this->$name = $value; |
160
|
|
|
} |
161
|
51 |
|
$this->_passedOptions[] = $name; |
162
|
51 |
|
unset($params[$name]); |
163
|
51 |
|
if (isset($kebabName)) { |
164
|
51 |
|
unset($params[$kebabName]); |
165
|
|
|
} |
166
|
194 |
|
} elseif (!is_int($name)) { |
167
|
|
|
$message = Yii::t('yii', 'Unknown option: --{name}', ['name' => $name]); |
168
|
|
|
if (!empty($options)) { |
169
|
|
|
$message .= '. ' . Yii::t('yii', 'Options available: {options}', ['options' => '--' . implode(', --', $options)]); |
170
|
|
|
} |
171
|
|
|
|
172
|
200 |
|
throw new Exception($message); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
212 |
|
if ($this->help) { |
177
|
2 |
|
$route = $this->getUniqueId() . '/' . $id; |
178
|
2 |
|
return Yii::$app->runAction('help', [$route]); |
179
|
|
|
} |
180
|
|
|
|
181
|
212 |
|
return parent::runAction($id, $params); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Binds the parameters to the action. |
186
|
|
|
* This method is invoked by [[Action]] when it begins to run with the given parameters. |
187
|
|
|
* This method will first bind the parameters with the [[options()|options]] |
188
|
|
|
* available to the action. It then validates the given arguments. |
189
|
|
|
* @param Action $action the action to be bound with parameters |
190
|
|
|
* @param array $params the parameters to be bound to the action |
191
|
|
|
* @return array the valid parameters that the action can run with. |
192
|
|
|
* @throws Exception if there are unknown options or missing arguments |
193
|
|
|
*/ |
194
|
226 |
|
public function bindActionParams($action, $params) |
195
|
|
|
{ |
196
|
226 |
|
if ($action instanceof InlineAction) { |
197
|
226 |
|
$method = new \ReflectionMethod($this, $action->actionMethod); |
198
|
|
|
} else { |
199
|
|
|
$method = new \ReflectionMethod($action, 'run'); |
200
|
|
|
} |
201
|
|
|
|
202
|
226 |
|
$args = []; |
203
|
226 |
|
$missing = []; |
204
|
226 |
|
$actionParams = []; |
205
|
226 |
|
$requestedParams = []; |
206
|
226 |
|
foreach ($method->getParameters() as $i => $param) { |
207
|
220 |
|
$name = $param->getName(); |
208
|
220 |
|
$key = null; |
209
|
220 |
|
if (array_key_exists($i, $params)) { |
210
|
194 |
|
$key = $i; |
211
|
57 |
|
} elseif (array_key_exists($name, $params)) { |
212
|
7 |
|
$key = $name; |
213
|
|
|
} |
214
|
|
|
|
215
|
220 |
|
if ($key !== null) { |
216
|
201 |
|
if ($param->isArray()) { |
217
|
1 |
|
$params[$key] = $params[$key] === '' ? [] : preg_split('/\s*,\s*/', $params[$key]); |
218
|
|
|
} |
219
|
201 |
|
$args[] = $actionParams[$key] = $params[$key]; |
220
|
201 |
|
unset($params[$key]); |
221
|
53 |
|
} elseif (PHP_VERSION_ID >= 70100 && ($type = $param->getType()) !== null && !$type->isBuiltin()) { |
222
|
|
|
try { |
223
|
5 |
|
$this->bindInjectedParams($type, $name, $args, $requestedParams); |
224
|
2 |
|
} catch (\yii\base\Exception $e) { |
225
|
5 |
|
throw new Exception($e->getMessage()); |
226
|
|
|
} |
227
|
48 |
|
} elseif ($param->isDefaultValueAvailable()) { |
228
|
48 |
|
$args[] = $actionParams[$i] = $param->getDefaultValue(); |
229
|
|
|
} else { |
230
|
220 |
|
$missing[] = $name; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
224 |
|
if (!empty($missing)) { |
235
|
1 |
|
throw new Exception(Yii::t('yii', 'Missing required arguments: {params}', ['params' => implode(', ', $missing)])); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
// We use a different array here, specifically one that doesn't contain service instances but descriptions instead. |
239
|
224 |
|
if (\Yii::$app->requestedParams === null) { |
240
|
224 |
|
\Yii::$app->requestedParams = array_merge($actionParams, $requestedParams); |
241
|
|
|
} |
242
|
|
|
|
243
|
224 |
|
return $args; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Formats a string with ANSI codes. |
248
|
|
|
* |
249
|
|
|
* You may pass additional parameters using the constants defined in [[\yii\helpers\Console]]. |
250
|
|
|
* |
251
|
|
|
* Example: |
252
|
|
|
* |
253
|
|
|
* ``` |
254
|
|
|
* echo $this->ansiFormat('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
255
|
|
|
* ``` |
256
|
|
|
* |
257
|
|
|
* @param string $string the string to be formatted |
258
|
|
|
* @return string |
259
|
|
|
*/ |
260
|
7 |
|
public function ansiFormat($string) |
261
|
|
|
{ |
262
|
7 |
|
if ($this->isColorEnabled()) { |
263
|
1 |
|
$args = func_get_args(); |
264
|
1 |
|
array_shift($args); |
265
|
1 |
|
$string = Console::ansiFormat($string, $args); |
266
|
|
|
} |
267
|
|
|
|
268
|
7 |
|
return $string; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Prints a string to STDOUT. |
273
|
|
|
* |
274
|
|
|
* You may optionally format the string with ANSI codes by |
275
|
|
|
* passing additional parameters using the constants defined in [[\yii\helpers\Console]]. |
276
|
|
|
* |
277
|
|
|
* Example: |
278
|
|
|
* |
279
|
|
|
* ``` |
280
|
|
|
* $this->stdout('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
281
|
|
|
* ``` |
282
|
|
|
* |
283
|
|
|
* @param string $string the string to print |
284
|
|
|
* @param int ...$args additional parameters to decorate the output |
285
|
|
|
* @return int|bool Number of bytes printed or false on error |
286
|
|
|
*/ |
287
|
|
|
public function stdout($string) |
288
|
|
|
{ |
289
|
|
|
if ($this->isColorEnabled()) { |
290
|
|
|
$args = func_get_args(); |
291
|
|
|
array_shift($args); |
292
|
|
|
$string = Console::ansiFormat($string, $args); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return Console::stdout($string); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Prints a string to STDERR. |
300
|
|
|
* |
301
|
|
|
* You may optionally format the string with ANSI codes by |
302
|
|
|
* passing additional parameters using the constants defined in [[\yii\helpers\Console]]. |
303
|
|
|
* |
304
|
|
|
* Example: |
305
|
|
|
* |
306
|
|
|
* ``` |
307
|
|
|
* $this->stderr('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); |
308
|
|
|
* ``` |
309
|
|
|
* |
310
|
|
|
* @param string $string the string to print |
311
|
|
|
* @return int|bool Number of bytes printed or false on error |
312
|
|
|
*/ |
313
|
|
|
public function stderr($string) |
314
|
|
|
{ |
315
|
|
|
if ($this->isColorEnabled(\STDERR)) { |
316
|
|
|
$args = func_get_args(); |
317
|
|
|
array_shift($args); |
318
|
|
|
$string = Console::ansiFormat($string, $args); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
return fwrite(\STDERR, $string); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Prompts the user for input and validates it. |
326
|
|
|
* |
327
|
|
|
* @param string $text prompt string |
328
|
|
|
* @param array $options the options to validate the input: |
329
|
|
|
* |
330
|
|
|
* - required: whether it is required or not |
331
|
|
|
* - default: default value if no input is inserted by the user |
332
|
|
|
* - pattern: regular expression pattern to validate user input |
333
|
|
|
* - validator: a callable function to validate input. The function must accept two parameters: |
334
|
|
|
* - $input: the user input to validate |
335
|
|
|
* - $error: the error value passed by reference if validation failed. |
336
|
|
|
* |
337
|
|
|
* An example of how to use the prompt method with a validator function. |
338
|
|
|
* |
339
|
|
|
* ```php |
340
|
|
|
* $code = $this->prompt('Enter 4-Chars-Pin', ['required' => true, 'validator' => function($input, &$error) { |
341
|
|
|
* if (strlen($input) !== 4) { |
342
|
|
|
* $error = 'The Pin must be exactly 4 chars!'; |
343
|
|
|
* return false; |
344
|
|
|
* } |
345
|
|
|
* return true; |
346
|
|
|
* }]); |
347
|
|
|
* ``` |
348
|
|
|
* |
349
|
|
|
* @return string the user input |
350
|
|
|
*/ |
351
|
|
|
public function prompt($text, $options = []) |
352
|
|
|
{ |
353
|
|
|
if ($this->interactive) { |
354
|
|
|
return Console::prompt($text, $options); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
return isset($options['default']) ? $options['default'] : ''; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Asks user to confirm by typing y or n. |
362
|
|
|
* |
363
|
|
|
* A typical usage looks like the following: |
364
|
|
|
* |
365
|
|
|
* ```php |
366
|
|
|
* if ($this->confirm("Are you sure?")) { |
367
|
|
|
* echo "user typed yes\n"; |
368
|
|
|
* } else { |
369
|
|
|
* echo "user typed no\n"; |
370
|
|
|
* } |
371
|
|
|
* ``` |
372
|
|
|
* |
373
|
|
|
* @param string $message to echo out before waiting for user input |
374
|
|
|
* @param bool $default this value is returned if no selection is made. |
375
|
|
|
* @return bool whether user confirmed. |
376
|
|
|
* Will return true if [[interactive]] is false. |
377
|
|
|
*/ |
378
|
145 |
|
public function confirm($message, $default = false) |
379
|
|
|
{ |
380
|
145 |
|
if ($this->interactive) { |
381
|
|
|
return Console::confirm($message, $default); |
382
|
|
|
} |
383
|
|
|
|
384
|
145 |
|
return true; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Gives the user an option to choose from. Giving '?' as an input will show |
389
|
|
|
* a list of options to choose from and their explanations. |
390
|
|
|
* |
391
|
|
|
* @param string $prompt the prompt message |
392
|
|
|
* @param array $options Key-value array of options to choose from |
393
|
|
|
* |
394
|
|
|
* @return string An option character the user chose |
395
|
|
|
*/ |
396
|
|
|
public function select($prompt, $options = []) |
397
|
|
|
{ |
398
|
|
|
return Console::select($prompt, $options); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Returns the names of valid options for the action (id) |
403
|
|
|
* An option requires the existence of a public member variable whose |
404
|
|
|
* name is the option name. |
405
|
|
|
* Child classes may override this method to specify possible options. |
406
|
|
|
* |
407
|
|
|
* Note that the values setting via options are not available |
408
|
|
|
* until [[beforeAction()]] is being called. |
409
|
|
|
* |
410
|
|
|
* @param string $actionID the action id of the current request |
411
|
|
|
* @return string[] the names of the options valid for the action |
412
|
|
|
*/ |
413
|
203 |
|
public function options($actionID) |
|
|
|
|
414
|
|
|
{ |
415
|
|
|
// $actionId might be used in subclasses to provide options specific to action id |
416
|
203 |
|
return ['color', 'interactive', 'help', 'silentExitOnException']; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Returns option alias names. |
421
|
|
|
* Child classes may override this method to specify alias options. |
422
|
|
|
* |
423
|
|
|
* @return array the options alias names valid for the action |
424
|
|
|
* where the keys is alias name for option and value is option name. |
425
|
|
|
* |
426
|
|
|
* @since 2.0.8 |
427
|
|
|
* @see options() |
428
|
|
|
*/ |
429
|
2 |
|
public function optionAliases() |
430
|
|
|
{ |
431
|
|
|
return [ |
432
|
2 |
|
'h' => 'help', |
433
|
|
|
]; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Returns properties corresponding to the options for the action id |
438
|
|
|
* Child classes may override this method to specify possible properties. |
439
|
|
|
* |
440
|
|
|
* @param string $actionID the action id of the current request |
441
|
|
|
* @return array properties corresponding to the options for the action |
442
|
|
|
*/ |
443
|
65 |
|
public function getOptionValues($actionID) |
|
|
|
|
444
|
|
|
{ |
445
|
|
|
// $actionId might be used in subclasses to provide properties specific to action id |
446
|
65 |
|
$properties = []; |
447
|
65 |
|
foreach ($this->options($this->action->id) as $property) { |
448
|
65 |
|
$properties[$property] = $this->$property; |
449
|
|
|
} |
450
|
|
|
|
451
|
65 |
|
return $properties; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Returns the names of valid options passed during execution. |
456
|
|
|
* |
457
|
|
|
* @return array the names of the options passed during execution |
458
|
|
|
*/ |
459
|
|
|
public function getPassedOptions() |
460
|
|
|
{ |
461
|
|
|
return $this->_passedOptions; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Returns the properties corresponding to the passed options. |
466
|
|
|
* |
467
|
|
|
* @return array the properties corresponding to the passed options |
468
|
|
|
*/ |
469
|
59 |
|
public function getPassedOptionValues() |
470
|
|
|
{ |
471
|
59 |
|
$properties = []; |
472
|
59 |
|
foreach ($this->_passedOptions as $property) { |
473
|
|
|
$properties[$property] = $this->$property; |
474
|
|
|
} |
475
|
|
|
|
476
|
59 |
|
return $properties; |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* Returns one-line short summary describing this controller. |
481
|
|
|
* |
482
|
|
|
* You may override this method to return customized summary. |
483
|
|
|
* The default implementation returns first line from the PHPDoc comment. |
484
|
|
|
* |
485
|
|
|
* @return string |
486
|
|
|
*/ |
487
|
5 |
|
public function getHelpSummary() |
488
|
|
|
{ |
489
|
5 |
|
return $this->parseDocCommentSummary(new \ReflectionClass($this)); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* Returns help information for this controller. |
494
|
|
|
* |
495
|
|
|
* You may override this method to return customized help. |
496
|
|
|
* The default implementation returns help information retrieved from the PHPDoc comment. |
497
|
|
|
* @return string |
498
|
|
|
*/ |
499
|
|
|
public function getHelp() |
500
|
|
|
{ |
501
|
|
|
return $this->parseDocCommentDetail(new \ReflectionClass($this)); |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
/** |
505
|
|
|
* Returns a one-line short summary describing the specified action. |
506
|
|
|
* @param Action $action action to get summary for |
507
|
|
|
* @return string a one-line short summary describing the specified action. |
508
|
|
|
*/ |
509
|
3 |
|
public function getActionHelpSummary($action) |
510
|
|
|
{ |
511
|
3 |
|
if ($action === null) { |
512
|
1 |
|
return $this->ansiFormat(Yii::t('yii', 'Action not found.'), Console::FG_RED); |
513
|
|
|
} |
514
|
|
|
|
515
|
2 |
|
return $this->parseDocCommentSummary($this->getActionMethodReflection($action)); |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* Returns the detailed help information for the specified action. |
520
|
|
|
* @param Action $action action to get help for |
521
|
|
|
* @return string the detailed help information for the specified action. |
522
|
|
|
*/ |
523
|
3 |
|
public function getActionHelp($action) |
524
|
|
|
{ |
525
|
3 |
|
return $this->parseDocCommentDetail($this->getActionMethodReflection($action)); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* Returns the help information for the anonymous arguments for the action. |
530
|
|
|
* |
531
|
|
|
* The returned value should be an array. The keys are the argument names, and the values are |
532
|
|
|
* the corresponding help information. Each value must be an array of the following structure: |
533
|
|
|
* |
534
|
|
|
* - required: boolean, whether this argument is required. |
535
|
|
|
* - type: string, the PHP type of this argument. |
536
|
|
|
* - default: string, the default value of this argument |
537
|
|
|
* - comment: string, the comment of this argument |
538
|
|
|
* |
539
|
|
|
* The default implementation will return the help information extracted from the doc-comment of |
540
|
|
|
* the parameters corresponding to the action method. |
541
|
|
|
* |
542
|
|
|
* @param Action $action |
543
|
|
|
* @return array the help information of the action arguments |
544
|
|
|
*/ |
545
|
6 |
|
public function getActionArgsHelp($action) |
546
|
|
|
{ |
547
|
6 |
|
$method = $this->getActionMethodReflection($action); |
548
|
6 |
|
$tags = $this->parseDocCommentTags($method); |
549
|
6 |
|
$params = isset($tags['param']) ? (array) $tags['param'] : []; |
550
|
|
|
|
551
|
6 |
|
$args = []; |
552
|
|
|
|
553
|
|
|
/** @var \ReflectionParameter $reflection */ |
554
|
6 |
|
foreach ($method->getParameters() as $i => $reflection) { |
555
|
6 |
|
if ($reflection->getClass() !== null) { |
556
|
1 |
|
continue; |
557
|
|
|
} |
558
|
6 |
|
$name = $reflection->getName(); |
559
|
6 |
|
$tag = isset($params[$i]) ? $params[$i] : ''; |
560
|
6 |
|
if (preg_match('/^(\S+)\s+(\$\w+\s+)?(.*)/s', $tag, $matches)) { |
561
|
4 |
|
$type = $matches[1]; |
562
|
4 |
|
$comment = $matches[3]; |
563
|
|
|
} else { |
564
|
2 |
|
$type = null; |
565
|
2 |
|
$comment = $tag; |
566
|
|
|
} |
567
|
6 |
|
if ($reflection->isDefaultValueAvailable()) { |
568
|
3 |
|
$args[$name] = [ |
569
|
3 |
|
'required' => false, |
570
|
3 |
|
'type' => $type, |
571
|
3 |
|
'default' => $reflection->getDefaultValue(), |
572
|
3 |
|
'comment' => $comment, |
573
|
|
|
]; |
574
|
|
|
} else { |
575
|
4 |
|
$args[$name] = [ |
576
|
4 |
|
'required' => true, |
577
|
4 |
|
'type' => $type, |
578
|
|
|
'default' => null, |
579
|
6 |
|
'comment' => $comment, |
580
|
|
|
]; |
581
|
|
|
} |
582
|
|
|
} |
583
|
|
|
|
584
|
6 |
|
return $args; |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* Returns the help information for the options for the action. |
589
|
|
|
* |
590
|
|
|
* The returned value should be an array. The keys are the option names, and the values are |
591
|
|
|
* the corresponding help information. Each value must be an array of the following structure: |
592
|
|
|
* |
593
|
|
|
* - type: string, the PHP type of this argument. |
594
|
|
|
* - default: string, the default value of this argument |
595
|
|
|
* - comment: string, the comment of this argument |
596
|
|
|
* |
597
|
|
|
* The default implementation will return the help information extracted from the doc-comment of |
598
|
|
|
* the properties corresponding to the action options. |
599
|
|
|
* |
600
|
|
|
* @param Action $action |
601
|
|
|
* @return array the help information of the action options |
602
|
|
|
*/ |
603
|
4 |
|
public function getActionOptionsHelp($action) |
604
|
|
|
{ |
605
|
4 |
|
$optionNames = $this->options($action->id); |
606
|
4 |
|
if (empty($optionNames)) { |
607
|
|
|
return []; |
608
|
|
|
} |
609
|
|
|
|
610
|
4 |
|
$class = new \ReflectionClass($this); |
611
|
4 |
|
$options = []; |
612
|
4 |
|
foreach ($class->getProperties() as $property) { |
613
|
4 |
|
$name = $property->getName(); |
614
|
4 |
|
if (!in_array($name, $optionNames, true)) { |
615
|
4 |
|
continue; |
616
|
|
|
} |
617
|
4 |
|
$defaultValue = $property->getValue($this); |
618
|
4 |
|
$tags = $this->parseDocCommentTags($property); |
619
|
|
|
|
620
|
|
|
// Display camelCase options in kebab-case |
621
|
4 |
|
$name = Inflector::camel2id($name, '-', true); |
622
|
|
|
|
623
|
4 |
|
if (isset($tags['var']) || isset($tags['property'])) { |
624
|
4 |
|
$doc = isset($tags['var']) ? $tags['var'] : $tags['property']; |
625
|
4 |
|
if (is_array($doc)) { |
626
|
|
|
$doc = reset($doc); |
627
|
|
|
} |
628
|
4 |
|
if (preg_match('/^(\S+)(.*)/s', $doc, $matches)) { |
629
|
4 |
|
$type = $matches[1]; |
630
|
4 |
|
$comment = $matches[2]; |
631
|
|
|
} else { |
632
|
|
|
$type = null; |
633
|
|
|
$comment = $doc; |
634
|
|
|
} |
635
|
4 |
|
$options[$name] = [ |
636
|
4 |
|
'type' => $type, |
637
|
4 |
|
'default' => $defaultValue, |
638
|
4 |
|
'comment' => $comment, |
639
|
|
|
]; |
640
|
|
|
} else { |
641
|
1 |
|
$options[$name] = [ |
642
|
1 |
|
'type' => null, |
643
|
1 |
|
'default' => $defaultValue, |
644
|
4 |
|
'comment' => '', |
645
|
|
|
]; |
646
|
|
|
} |
647
|
|
|
} |
648
|
|
|
|
649
|
4 |
|
return $options; |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
private $_reflections = []; |
653
|
|
|
|
654
|
|
|
/** |
655
|
|
|
* @param Action $action |
656
|
|
|
* @return \ReflectionMethod |
657
|
|
|
*/ |
658
|
8 |
|
protected function getActionMethodReflection($action) |
659
|
|
|
{ |
660
|
8 |
|
if (!isset($this->_reflections[$action->id])) { |
661
|
8 |
|
if ($action instanceof InlineAction) { |
662
|
8 |
|
$this->_reflections[$action->id] = new \ReflectionMethod($this, $action->actionMethod); |
663
|
|
|
} else { |
664
|
|
|
$this->_reflections[$action->id] = new \ReflectionMethod($action, 'run'); |
665
|
|
|
} |
666
|
|
|
} |
667
|
|
|
|
668
|
8 |
|
return $this->_reflections[$action->id]; |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
/** |
672
|
|
|
* Parses the comment block into tags. |
673
|
|
|
* @param \Reflector $reflection the comment block |
674
|
|
|
* @return array the parsed tags |
675
|
|
|
*/ |
676
|
6 |
|
protected function parseDocCommentTags($reflection) |
677
|
|
|
{ |
678
|
6 |
|
$comment = $reflection->getDocComment(); |
|
|
|
|
679
|
6 |
|
$comment = "@description \n" . strtr(trim(preg_replace('/^\s*\**( |\t)?/m', '', trim($comment, '/'))), "\r", ''); |
680
|
6 |
|
$parts = preg_split('/^\s*@/m', $comment, -1, PREG_SPLIT_NO_EMPTY); |
681
|
6 |
|
$tags = []; |
682
|
6 |
|
foreach ($parts as $part) { |
683
|
6 |
|
if (preg_match('/^(\w+)(.*)/ms', trim($part), $matches)) { |
684
|
6 |
|
$name = $matches[1]; |
685
|
6 |
|
if (!isset($tags[$name])) { |
686
|
6 |
|
$tags[$name] = trim($matches[2]); |
687
|
|
|
} elseif (is_array($tags[$name])) { |
688
|
|
|
$tags[$name][] = trim($matches[2]); |
689
|
|
|
} else { |
690
|
6 |
|
$tags[$name] = [$tags[$name], trim($matches[2])]; |
691
|
|
|
} |
692
|
|
|
} |
693
|
|
|
} |
694
|
|
|
|
695
|
6 |
|
return $tags; |
696
|
|
|
} |
697
|
|
|
|
698
|
|
|
/** |
699
|
|
|
* Returns the first line of docblock. |
700
|
|
|
* |
701
|
|
|
* @param \Reflector $reflection |
702
|
|
|
* @return string |
703
|
|
|
*/ |
704
|
5 |
|
protected function parseDocCommentSummary($reflection) |
705
|
|
|
{ |
706
|
5 |
|
$docLines = preg_split('~\R~u', $reflection->getDocComment()); |
707
|
5 |
|
if (isset($docLines[1])) { |
708
|
5 |
|
return trim($docLines[1], "\t *"); |
709
|
|
|
} |
710
|
|
|
|
711
|
2 |
|
return ''; |
712
|
|
|
} |
713
|
|
|
|
714
|
|
|
/** |
715
|
|
|
* Returns full description from the docblock. |
716
|
|
|
* |
717
|
|
|
* @param \Reflector $reflection |
718
|
|
|
* @return string |
719
|
|
|
*/ |
720
|
3 |
|
protected function parseDocCommentDetail($reflection) |
721
|
|
|
{ |
722
|
3 |
|
$comment = strtr(trim(preg_replace('/^\s*\**( |\t)?/m', '', trim($reflection->getDocComment(), '/'))), "\r", ''); |
723
|
3 |
|
if (preg_match('/^\s*@\w+/m', $comment, $matches, PREG_OFFSET_CAPTURE)) { |
724
|
2 |
|
$comment = trim(substr($comment, 0, $matches[0][1])); |
725
|
|
|
} |
726
|
3 |
|
if ($comment !== '') { |
727
|
2 |
|
return rtrim(Console::renderColoredString(Console::markdownToAnsi($comment))); |
728
|
|
|
} |
729
|
|
|
|
730
|
1 |
|
return ''; |
731
|
|
|
} |
732
|
|
|
} |
733
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.