mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-01 03:26:36 +08:00
Fix #17722: Add action injection support
This commit is contained in:
@ -182,19 +182,35 @@ class Controller extends \yii\base\Controller
|
||||
$method = new \ReflectionMethod($action, 'run');
|
||||
}
|
||||
|
||||
$args = array_values($params);
|
||||
|
||||
$args = [];
|
||||
$missing = [];
|
||||
$actionParams = [];
|
||||
$requestedParams = [];
|
||||
foreach ($method->getParameters() as $i => $param) {
|
||||
if ($param->isArray() && isset($args[$i])) {
|
||||
$args[$i] = $args[$i] === '' ? [] : preg_split('/\s*,\s*/', $args[$i]);
|
||||
$name = $param->getName();
|
||||
$key = null;
|
||||
if (array_key_exists($i, $params)) {
|
||||
$key = $i;
|
||||
} elseif (array_key_exists($name, $params)) {
|
||||
$key = $name;
|
||||
}
|
||||
if (!isset($args[$i])) {
|
||||
if ($param->isDefaultValueAvailable()) {
|
||||
$args[$i] = $param->getDefaultValue();
|
||||
} else {
|
||||
$missing[] = $param->getName();
|
||||
|
||||
if ($key !== null) {
|
||||
if ($param->isArray()) {
|
||||
$params[$key] = $params[$key] === '' ? [] : preg_split('/\s*,\s*/', $params[$key]);
|
||||
}
|
||||
$args[] = $actionParams[$key] = $params[$key];
|
||||
unset($params[$key]);
|
||||
} elseif (PHP_VERSION_ID >= 70100 && ($type = $param->getType()) !== null && !$type->isBuiltin()) {
|
||||
try {
|
||||
$this->bindInjectedParams($type, $name, $args, $requestedParams);
|
||||
} catch (\yii\base\Exception $e) {
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
} elseif ($param->isDefaultValueAvailable()) {
|
||||
$args[] = $actionParams[$i] = $param->getDefaultValue();
|
||||
} else {
|
||||
$missing[] = $name;
|
||||
}
|
||||
}
|
||||
|
||||
@ -202,6 +218,11 @@ class Controller extends \yii\base\Controller
|
||||
throw new Exception(Yii::t('yii', 'Missing required arguments: {params}', ['params' => implode(', ', $missing)]));
|
||||
}
|
||||
|
||||
// We use a different array here, specifically one that doesn't contain service instances but descriptions instead.
|
||||
if (\Yii::$app->requestedParams === null) {
|
||||
\Yii::$app->requestedParams = array_merge($actionParams, $requestedParams);
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user