Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed called class check in Widget::end() when widget configured using callable #20270

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
*/
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 @@
/* @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 @@
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
Loading