From 6d315e8e2481c33a12a4ac2d669d8b11555dbdea Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Sat, 17 Aug 2024 15:26:44 -0400 Subject: [PATCH 1/3] Add support for variadic CLI arguments --- framework/console/Controller.php | 28 +++++++++++++++------- tests/framework/console/ControllerTest.php | 6 +++++ tests/framework/console/FakeController.php | 5 ++++ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/framework/console/Controller.php b/framework/console/Controller.php index 908e50fcdb4..3007c0b4aae 100644 --- a/framework/console/Controller.php +++ b/framework/console/Controller.php @@ -198,6 +198,7 @@ public function bindActionParams($action, $params) $method = new \ReflectionMethod($action, 'run'); } + $paramKeys = array_keys($params); $args = []; $missing = []; $actionParams = []; @@ -212,16 +213,27 @@ public function bindActionParams($action, $params) } if ($key !== null) { - if (PHP_VERSION_ID >= 80000) { - $isArray = ($type = $param->getType()) instanceof \ReflectionNamedType && $type->getName() === 'array'; + if ($param->isVariadic()) { + for ($j = array_search($key, $paramKeys); $j < count($paramKeys); $j++) { + $jKey = $paramKeys[$j]; + if ($j !== $key && !is_int($j)) { + break; + } + $args[] = $actionParams[$key][] = $params[$jKey]; + unset($params[$jKey]); + } } else { - $isArray = $param->isArray(); - } - if ($isArray) { - $params[$key] = $params[$key] === '' ? [] : preg_split('/\s*,\s*/', $params[$key]); + if (PHP_VERSION_ID >= 80000) { + $isArray = ($type = $param->getType()) instanceof \ReflectionNamedType && $type->getName() === 'array'; + } else { + $isArray = $param->isArray(); + } + if ($isArray) { + $params[$key] = $params[$key] === '' ? [] : preg_split('/\s*,\s*/', $params[$key]); + } + $args[] = $actionParams[$key] = $params[$key]; + unset($params[$key]); } - $args[] = $actionParams[$key] = $params[$key]; - unset($params[$key]); } elseif ( PHP_VERSION_ID >= 70100 && ($type = $param->getType()) !== null diff --git a/tests/framework/console/ControllerTest.php b/tests/framework/console/ControllerTest.php index 2103715b51c..b3dd0350baa 100644 --- a/tests/framework/console/ControllerTest.php +++ b/tests/framework/console/ControllerTest.php @@ -91,6 +91,12 @@ public function testBindActionParams() $this->assertEquals('from params', $fromParam); $this->assertEquals('notdefault', $other); + $params = ['a', 'b', 'c1', 'c2', 'c3']; + [$a, $b, $c] = $controller->run('variadic', $params); + $this->assertEquals('a', $a); + $this->assertEquals('b', $b); + $this->assertEquals(['c1', 'c2', 'c3'], $c); + $params = ['avaliable']; $message = Yii::t('yii', 'Missing required arguments: {params}', ['params' => implode(', ', ['missing'])]); $this->expectException('yii\console\Exception'); diff --git a/tests/framework/console/FakeController.php b/tests/framework/console/FakeController.php index 22158049d29..cc268c4b188 100644 --- a/tests/framework/console/FakeController.php +++ b/tests/framework/console/FakeController.php @@ -104,4 +104,9 @@ public function actionResponse($status = 0) $response->exitStatus = (int) $status; return $response; } + + public function actionVariadic($foo, $bar, ...$baz) + { + return [$foo, $bar, $baz]; + } } From f72be266a4ebf1656e77d0229f3456dda68f81a1 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Sun, 18 Aug 2024 05:50:37 -0400 Subject: [PATCH 2/3] Release note [ci skip] --- framework/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index c100209e75a..55f9605adfc 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -6,6 +6,7 @@ Yii Framework 2 Change Log - Bug #20232: Fix regression introduced in `GHSA-cjcc-p67m-7qxm` while attaching behavior defined by `__class` array key (erickskrauch) - Bug #20231: Fix regression introduced in #20167 in `yii\validators\FileValidator` (bizley) +- Enh #20247: Support for variadic console controller action methods (brandonkelly) 2.0.51 July 18, 2024 -------------------- From e45c6a4d6768e580f147a87c5144c3c3d53792d7 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Sun, 18 Aug 2024 05:53:15 -0400 Subject: [PATCH 3/3] Fix condition --- framework/console/Controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/console/Controller.php b/framework/console/Controller.php index 3007c0b4aae..7029abd5462 100644 --- a/framework/console/Controller.php +++ b/framework/console/Controller.php @@ -216,7 +216,7 @@ public function bindActionParams($action, $params) if ($param->isVariadic()) { for ($j = array_search($key, $paramKeys); $j < count($paramKeys); $j++) { $jKey = $paramKeys[$j]; - if ($j !== $key && !is_int($j)) { + if ($jKey !== $key && !is_int($jKey)) { break; } $args[] = $actionParams[$key][] = $params[$jKey];