From 333de8ae7ddb23faeb363b73fe2c9e984bb61de1 Mon Sep 17 00:00:00 2001 From: lav45 Date: Tue, 25 Feb 2025 18:02:24 +0300 Subject: [PATCH 1/2] Fix #17720 for \yii\di\Instance::ensure() --- framework/di/Instance.php | 11 +++++++++-- tests/framework/di/InstanceTest.php | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/framework/di/Instance.php b/framework/di/Instance.php index 39bd801b354..edecbe9be80 100644 --- a/framework/di/Instance.php +++ b/framework/di/Instance.php @@ -118,11 +118,18 @@ public static function of($id, $optional = false) public static function ensure($reference, $type = null, $container = null) { if (is_array($reference)) { - $class = isset($reference['class']) ? $reference['class'] : $type; if (!$container instanceof Container) { $container = Yii::$container; } - unset($reference['class']); + if (isset($reference['__class'])) { + $class = $reference['__class']; + unset($reference['__class'], $type['class']); + } elseif (isset($reference['class'])) { + $class = $reference['class']; + unset($reference['class']); + } else { + $class = $type; + } $component = $container->get($class, [], $reference); if ($type === null || $component instanceof $type) { return $component; diff --git a/tests/framework/di/InstanceTest.php b/tests/framework/di/InstanceTest.php index 78044062ed3..2254689403a 100644 --- a/tests/framework/di/InstanceTest.php +++ b/tests/framework/di/InstanceTest.php @@ -97,6 +97,7 @@ public function testEnsure_MinimalSettings() $this->assertInstanceOf(Connection::className(), Instance::ensure('db')); $this->assertInstanceOf(Connection::className(), Instance::ensure(new Connection())); $this->assertInstanceOf(Connection::className(), Instance::ensure(['class' => 'yii\db\Connection', 'dsn' => 'test'])); + $this->assertInstanceOf(Connection::className(), Instance::ensure(['__class' => 'yii\db\Connection', 'dsn' => 'test'])); Yii::$container = new Container(); } From 285b87fd7304480298cdf91691a60a062621bda9 Mon Sep 17 00:00:00 2001 From: lav45 Date: Thu, 27 Feb 2025 12:45:11 +0300 Subject: [PATCH 2/2] Update CHANGELOG.md --- framework/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 4869591ce6f..89f5154fea7 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -34,6 +34,7 @@ Yii Framework 2 Change Log - Chg #20276: Removed autogenerated migration phpdoc (userator) - New #20185: Add `BackedEnum` support to `AttributeTypecastBehavior` (briedis) - New #20279: Add to the `\yii\web\Request` CSRF validation by custom HTTP header (olegbaturin) +- New #20332: Added support for the '__class' key in `\yii\di\Instance:eunsure(['__class' => ...])` (LAV45) 2.0.51 July 18, 2024