Skip to content

Commit

Permalink
Fixed called class check in Widget::end() when widget configured us…
Browse files Browse the repository at this point in the history
…ing callable

See #20267
  • Loading branch information
jrajamaki committed Oct 19, 2024
1 parent 3c75ff1 commit 8a0992d
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)

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 = [];

Check failure on line 66 in framework/base/Widget.php

View workflow job for this annotation

GitHub Actions / PHP_CodeSniffer

Private member variable "resolvedClasses" must contain a leading underscore

/**
* 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);

Check warning on line 95 in framework/base/Widget.php

View check run for this annotation

Codecov / codecov/patch

framework/base/Widget.php#L95

Added line #L95 was not covered by tests

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();

Check warning on line 112 in framework/base/Widget.php

View check run for this annotation

Codecov / codecov/patch

framework/base/Widget.php#L112

Added line #L112 was not covered by tests

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 8a0992d

Please sign in to comment.