Skip to content

Commit

Permalink
Merge pull request #20270 from jrajamaki/20267-widget-end-resolved-cl…
Browse files Browse the repository at this point in the history
…asses

Fix: called class check in `Widget::end()` when widget configured using callable. Closes #20267
  • Loading branch information
mtangoo authored Oct 31, 2024
2 parents e4d5d73 + 363f90a commit 5ec5a4b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- Enh #20268: Minor optimisation in `\yii\helpers\BaseArrayHelper::map` (chriscpty)

2.0.51 July 18, 2024
Expand Down
10 changes: 6 additions & 4 deletions framework/base/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 */
Expand Down
21 changes: 21 additions & 0 deletions tests/framework/base/WidgetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@ public function testDependencyInjection()
$this->assertSame('<run-test>', $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('<run-test>', $output);
}

/**
* @depends testBeginEnd
*/
Expand Down

0 comments on commit 5ec5a4b

Please sign in to comment.