From 8a0992d5c012f2042ce418d1edf0e225cdbcaa06 Mon Sep 17 00:00:00 2001 From: Janne Date: Sat, 19 Oct 2024 20:37:49 +0300 Subject: [PATCH 1/2] Fixed called class check in `Widget::end()` when widget configured using callable See #20267 --- framework/CHANGELOG.md | 1 + framework/base/Widget.php | 10 ++++++---- tests/framework/base/WidgetTest.php | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 294810f9fc7..81392001ce7 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -9,6 +9,7 @@ Yii Framework 2 Change Log - Enh #20247: Support for variadic console controller action methods (brandonkelly) - Bug #20256: Add support for dropping views in MSSQL server when running migrate/fresh (ambrozt) - Enh #20248: Add support for attaching behaviors in configurations with Closure (timkelty) +- Enh #20267: Fixed called class check in `Widget::end()` when widget configured using callable (rob006, jrajamaki) 2.0.51 July 18, 2024 -------------------- diff --git a/framework/base/Widget.php b/framework/base/Widget.php index 450caac9c9b..1267643df62 100644 --- a/framework/base/Widget.php +++ b/framework/base/Widget.php @@ -60,6 +60,10 @@ class Widget extends Component implements ViewContextInterface */ public static $stack = []; + /** + * @var string[] used widget classes that have been resolved to their actual class name. + */ + private static $resolvedClasses = []; /** * Initializes the object. @@ -88,6 +92,7 @@ public static function begin($config = []) /* @var $widget Widget */ $widget = Yii::createObject($config); self::$stack[] = $widget; + self::$resolvedClasses[get_called_class()] = get_class($widget); return $widget; } @@ -104,10 +109,7 @@ public static function end() if (!empty(self::$stack)) { $widget = array_pop(self::$stack); - $calledClass = get_called_class(); - if (Yii::$container->has($calledClass) && isset(Yii::$container->getDefinitions()[$calledClass]['class'])) { - $calledClass = Yii::$container->getDefinitions()[$calledClass]['class']; - } + $calledClass = self::$resolvedClasses[get_called_class()] ?? get_called_class(); if (get_class($widget) === $calledClass) { /* @var $widget Widget */ diff --git a/tests/framework/base/WidgetTest.php b/tests/framework/base/WidgetTest.php index 067e9480125..c67cbedd885 100644 --- a/tests/framework/base/WidgetTest.php +++ b/tests/framework/base/WidgetTest.php @@ -72,6 +72,27 @@ public function testDependencyInjection() $this->assertSame('', $output); } + public function testDependencyInjectionWithCallableConfiguration() + { + Yii::$container = new Container(); + Yii::$container->setDefinitions([ + TestWidgetB::className() => function () { + return new TestWidget(['id' => 'test']); + } + ]); + + ob_start(); + ob_implicit_flush(false); + + $widget = TestWidgetB::begin(['id' => 'test']); + $this->assertTrue($widget instanceof TestWidget); + TestWidgetB::end(); + + $output = ob_get_clean(); + + $this->assertSame('', $output); + } + /** * @depends testBeginEnd */ From 02ef942c5335266903947565e093671984da63e0 Mon Sep 17 00:00:00 2001 From: Janne Date: Sat, 19 Oct 2024 20:48:31 +0300 Subject: [PATCH 2/2] Added missing underscore in front of private property #20267 --- framework/base/Widget.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/framework/base/Widget.php b/framework/base/Widget.php index 1267643df62..b1340188449 100644 --- a/framework/base/Widget.php +++ b/framework/base/Widget.php @@ -63,7 +63,7 @@ class Widget extends Component implements ViewContextInterface /** * @var string[] used widget classes that have been resolved to their actual class name. */ - private static $resolvedClasses = []; + private static $_resolvedClasses = []; /** * Initializes the object. @@ -92,7 +92,7 @@ public static function begin($config = []) /* @var $widget Widget */ $widget = Yii::createObject($config); self::$stack[] = $widget; - self::$resolvedClasses[get_called_class()] = get_class($widget); + self::$_resolvedClasses[get_called_class()] = get_class($widget); return $widget; } @@ -109,7 +109,7 @@ public static function end() if (!empty(self::$stack)) { $widget = array_pop(self::$stack); - $calledClass = self::$resolvedClasses[get_called_class()] ?? get_called_class(); + $calledClass = self::$_resolvedClasses[get_called_class()] ?? get_called_class(); if (get_class($widget) === $calledClass) { /* @var $widget Widget */