|
|
|
@ -542,60 +542,69 @@ class Controller extends \yii\base\Controller
|
|
|
|
|
* The returned value should be an array. The keys are the argument names, and the values are |
|
|
|
|
* the corresponding help information. Each value must be an array of the following structure: |
|
|
|
|
* |
|
|
|
|
* - required: boolean, whether this argument is required. |
|
|
|
|
* - type: string, the PHP type of this argument. |
|
|
|
|
* - default: string, the default value of this argument |
|
|
|
|
* - comment: string, the comment of this argument |
|
|
|
|
* - required: bool, whether this argument is required |
|
|
|
|
* - type: string|null, the PHP type(s) of this argument |
|
|
|
|
* - default: mixed, the default value of this argument |
|
|
|
|
* - comment: string, the description of this argument |
|
|
|
|
* |
|
|
|
|
* The default implementation will return the help information extracted from the doc-comment of |
|
|
|
|
* the parameters corresponding to the action method. |
|
|
|
|
* The default implementation will return the help information extracted from the Reflection or |
|
|
|
|
* DocBlock of the parameters corresponding to the action method. |
|
|
|
|
* |
|
|
|
|
* @param Action $action |
|
|
|
|
* @param Action $action the action instance |
|
|
|
|
* @return array the help information of the action arguments |
|
|
|
|
*/ |
|
|
|
|
public function getActionArgsHelp($action) |
|
|
|
|
{ |
|
|
|
|
$method = $this->getActionMethodReflection($action); |
|
|
|
|
|
|
|
|
|
$tags = $this->parseDocCommentTags($method); |
|
|
|
|
$params = isset($tags['param']) ? (array) $tags['param'] : []; |
|
|
|
|
$tags['param'] = isset($tags['param']) ? (array) $tags['param'] : []; |
|
|
|
|
$phpDocParams = []; |
|
|
|
|
foreach ($tags['param'] as $i => $tag) { |
|
|
|
|
if (preg_match('/^(?<type>\S+)(\s+\$(?<name>\w+))?(?<comment>.*)/us', $tag, $matches) === 1) { |
|
|
|
|
$key = empty($matches['name']) ? $i : $matches['name']; |
|
|
|
|
$phpDocParams[$key] = ['type' => $matches['type'], 'comment' => $matches['comment']]; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
unset($tags); |
|
|
|
|
|
|
|
|
|
$args = []; |
|
|
|
|
|
|
|
|
|
/** @var \ReflectionParameter $reflection */ |
|
|
|
|
foreach ($method->getParameters() as $i => $reflection) { |
|
|
|
|
if (PHP_VERSION_ID >= 80000) { |
|
|
|
|
$class = $reflection->getType(); |
|
|
|
|
} else { |
|
|
|
|
$class = $reflection->getClass(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ($class !== null) { |
|
|
|
|
continue; |
|
|
|
|
/** @var \ReflectionParameter $parameter */ |
|
|
|
|
foreach ($method->getParameters() as $i => $parameter) { |
|
|
|
|
$type = null; |
|
|
|
|
$comment = ''; |
|
|
|
|
if (PHP_MAJOR_VERSION > 5 && $parameter->hasType()) { |
|
|
|
|
$reflectionType = $parameter->getType(); |
|
|
|
|
if (PHP_VERSION_ID >= 70100) { |
|
|
|
|
$types = method_exists($reflectionType, 'getTypes') ? $reflectionType->getTypes() : [$reflectionType]; |
|
|
|
|
foreach ($types as $key => $reflectionType) { |
|
|
|
|
$types[$key] = $reflectionType->getName(); |
|
|
|
|
} |
|
|
|
|
$type = implode('|', $types); |
|
|
|
|
} else { |
|
|
|
|
$type = (string) $reflectionType; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
$name = $reflection->getName(); |
|
|
|
|
$tag = isset($params[$i]) ? $params[$i] : ''; |
|
|
|
|
if (preg_match('/^(\S+)\s+(\$\w+\s+)?(.*)/s', $tag, $matches)) { |
|
|
|
|
$type = $matches[1]; |
|
|
|
|
$comment = $matches[3]; |
|
|
|
|
} else { |
|
|
|
|
$type = null; |
|
|
|
|
$comment = $tag; |
|
|
|
|
// find PhpDoc tag by property name or position |
|
|
|
|
$key = isset($phpDocParams[$parameter->name]) ? $parameter->name : (isset($phpDocParams[$i]) ? $i : null); |
|
|
|
|
if ($key !== null) { |
|
|
|
|
$comment = $phpDocParams[$key]['comment']; |
|
|
|
|
if ($type === null && !empty($phpDocParams[$key]['type'])) { |
|
|
|
|
$type = $phpDocParams[$key]['type']; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if ($reflection->isDefaultValueAvailable()) { |
|
|
|
|
$args[$name] = [ |
|
|
|
|
'required' => false, |
|
|
|
|
'type' => $type, |
|
|
|
|
'default' => $reflection->getDefaultValue(), |
|
|
|
|
'comment' => $comment, |
|
|
|
|
]; |
|
|
|
|
} else { |
|
|
|
|
$args[$name] = [ |
|
|
|
|
'required' => true, |
|
|
|
|
'type' => $type, |
|
|
|
|
'default' => null, |
|
|
|
|
'comment' => $comment, |
|
|
|
|
]; |
|
|
|
|
// if type still not detected, then using type of default value |
|
|
|
|
if ($type === null && $parameter->isDefaultValueAvailable() && $parameter->getDefaultValue() !== null) { |
|
|
|
|
$type = gettype($parameter->getDefaultValue()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$args[$parameter->name] = [ |
|
|
|
|
'required' => !$parameter->isOptional(), |
|
|
|
|
'type' => $type, |
|
|
|
|
'default' => $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null, |
|
|
|
|
'comment' => $comment, |
|
|
|
|
]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return $args; |
|
|
|
|