mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-02 21:41:19 +08:00
Fix #19030: Add DI container usage to yii\base\Widget::end()
This commit is contained in:
@ -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
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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('<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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user