Fix #19030: Add DI container usage to yii\base\Widget::end()

This commit is contained in:
Papp Péter
2021-11-30 21:58:24 +01:00
committed by GitHub
parent d52c1237ca
commit 0d899fa18b
3 changed files with 34 additions and 1 deletions

View File

@ -31,6 +31,7 @@ Yii Framework 2 Change Log
- Bug #18988: Fix default value of `yii\console\controllers\MessageController::$translator` (WinterSilence) - 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 #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 #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 2.0.43 August 09, 2021

View File

@ -104,7 +104,13 @@ class Widget extends Component implements ViewContextInterface
{ {
if (!empty(self::$stack)) { if (!empty(self::$stack)) {
$widget = array_pop(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 */ /* @var $widget Widget */
if ($widget->beforeRun()) { if ($widget->beforeRun()) {
$result = $widget->run(); $result = $widget->run();

View File

@ -7,8 +7,10 @@
namespace yiiunit\framework\base; namespace yiiunit\framework\base;
use Yii;
use yii\base\Widget; use yii\base\Widget;
use yii\base\WidgetEvent; use yii\base\WidgetEvent;
use yii\di\Container;
use yiiunit\TestCase; use yiiunit\TestCase;
/** /**
@ -46,6 +48,30 @@ class WidgetTest extends TestCase
$this->assertSame('<run-test>', $output); $this->assertSame('<run-test>', $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('<run-test>', $output);
}
/** /**
* @depends testBeginEnd * @depends testBeginEnd
*/ */