diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index da783b578f..d8d2d33ba8 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -31,6 +31,7 @@ Yii Framework 2 Change Log - Bug #18988: Fix default value of `yii\console\controllers\MessageController::$translator` (WinterSilence) - Bug #18993: Load defaults by `attributes()` in `yii\db\ActiveRecord::loadDefaultValues()` (WinterSilence) - Bug #19021: Fix return type in PhpDoc `yii\db\Migration` functions `up()`, `down()`, `safeUp()` and `safeDown()` (WinterSilence, rhertogh) +- Bug #19030: Add DI container usage to `yii\base\Widget::end()` (papppeter) 2.0.43 August 09, 2021 diff --git a/framework/base/Widget.php b/framework/base/Widget.php index a43a5652bb..2f34a26ead 100644 --- a/framework/base/Widget.php +++ b/framework/base/Widget.php @@ -104,7 +104,13 @@ class Widget extends Component implements ViewContextInterface { if (!empty(self::$stack)) { $widget = array_pop(self::$stack); - if (get_class($widget) === get_called_class()) { + + $calledClass = get_called_class(); + if (Yii::$container->has($calledClass) && isset(Yii::$container->getDefinitions()[$calledClass]['class'])) { + $calledClass = Yii::$container->getDefinitions()[$calledClass]['class']; + } + + if (get_class($widget) === $calledClass) { /* @var $widget Widget */ if ($widget->beforeRun()) { $result = $widget->run(); diff --git a/tests/framework/base/WidgetTest.php b/tests/framework/base/WidgetTest.php index 33542a18b3..6b21debc8b 100644 --- a/tests/framework/base/WidgetTest.php +++ b/tests/framework/base/WidgetTest.php @@ -7,8 +7,10 @@ namespace yiiunit\framework\base; +use Yii; use yii\base\Widget; use yii\base\WidgetEvent; +use yii\di\Container; use yiiunit\TestCase; /** @@ -46,6 +48,30 @@ class WidgetTest extends TestCase $this->assertSame('', $output); } + /** + * @see https://github.com/yiisoft/yii2/issues/19030 + */ + public function testDependencyInjection() + { + Yii::$container = new Container(); + Yii::$container->setDefinitions([ + TestWidgetB::className() => [ + 'class' => TestWidget::className() + ] + ]); + + 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 */