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