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