Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commiteb9db6c

Browse files
bug#49441 [Contracts] Fix setting $container before calling parent::setContainer in ServiceSubscriberTrait (edsrzf)
This PR was squashed before being merged into the 5.4 branch.Discussion----------[Contracts] Fix setting $container before calling parent::setContainer in ServiceSubscriberTrait| Q | A| ------------- | ---| Branch? | 5.4| Bug fix? | yes| New feature? | no| Deprecations? | no| Tickets |Fix#49382| License | MITThis changes the `setContainer` method of `ServiceSubscriberTrait` so that it calls `parent::setContainer` first, before updating `$this->container`. This is so that the parent method can work correctly if it relies on the old container value.This is particularly relevant for `AbstractController`, since it returns the old container value. When used with `ServiceSubscriberTrait`, it returned the _new_ container rather than the old one. This, in turn, caused incorrect behavior in framework bundle's `ControllerResolver`, as described in#49382.Commits-------bccb074 [Contracts] Fix setting $container before calling parent::setContainer in ServiceSubscriberTrait
2 parentsdee9e76 +bccb074 commiteb9db6c

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

‎src/Symfony/Contracts/Service/ServiceSubscriberTrait.php‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,13 @@ public static function getSubscribedServices(): array
9898
*/
9999
publicfunctionsetContainer(ContainerInterface$container)
100100
{
101-
$this->container =$container;
102-
101+
$ret =null;
103102
if (method_exists(get_parent_class(self::class) ?:'',__FUNCTION__)) {
104-
returnparent::setContainer($container);
103+
$ret =parent::setContainer($container);
105104
}
106105

107-
returnnull;
106+
$this->container =$container;
107+
108+
return$ret;
108109
}
109110
}

‎src/Symfony/Contracts/Tests/Service/ServiceSubscriberTraitTest.php‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ public function testParentNotCalledIfNoParent()
8181
$this->assertSame([],$service::getSubscribedServices());
8282
}
8383

84+
publicfunctiontestSetContainerCalledFirstOnParent()
85+
{
86+
$container1 =newclass([])implements ContainerInterface {
87+
use ServiceLocatorTrait;
88+
};
89+
$container2 =clone$container1;
90+
91+
$testService =newTestService2();
92+
$this->assertNull($testService->setContainer($container1));
93+
$this->assertSame($container1,$testService->setContainer($container2));
94+
}
95+
8496
/**
8597
* @requires PHP 8
8698
*
@@ -161,3 +173,22 @@ public static function __callStatic($method, $args)
161173
class Service3
162174
{
163175
}
176+
177+
class ParentTestService2
178+
{
179+
/** @var ContainerInterface */
180+
protected$container;
181+
182+
publicfunctionsetContainer(ContainerInterface$container)
183+
{
184+
$previous =$this->container;
185+
$this->container =$container;
186+
187+
return$previous;
188+
}
189+
}
190+
191+
class TestService2extends ParentTestService2implements ServiceSubscriberInterface
192+
{
193+
use ServiceSubscriberTrait;
194+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp