8000 bug #60268 [Contracts] Fix `ServiceSubscriberTrait` for nullable serv… · symfony/symfony@b59a25a · GitHub
[go: up one dir, main page]

Skip to content

Commit b59a25a

Browse files
committed
bug #60268 [Contracts] Fix ServiceSubscriberTrait for nullable service (StevenRenaux)
This PR was merged into the 6.4 branch. Discussion ---------- [Contracts] Fix `ServiceSubscriberTrait` for nullable service | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT Used in a bundle context. If you use the following example, you will get an exception: `ServiceNotFoundException: The service "twig" in the container provided to "App\Service\Dependency" has a dependency on a non-existent service "Twig\Environment".` The `nullable` argument of the `SubscribedService` attribute is ignored. ```php // src/Service/TwigAware.php namespace App\Service; use Twig\Environment; use Symfony\Contracts\Service\Attribute\SubscribedService; trait TwigAware { #[SubscribedService('twig', nullable: true)] private function twig(): Environment { if (!$this->container->has('twig') ) { throw new \LogicException(\sprintf('Twig is required to use "%s" method. Try to run "composer require symfony/twig-bundle".', __METHOD__)); } return $environment; } } // src/Service/MyService.php namespace App\Service; use Symfony\Contracts\Service\ServiceSubscriberInterface; use Symfony\Contracts\Service\ServiceSubscriberTrait; class MyService implements ServiceSubscriberInterface { use ServiceSubscriberTrait, TwigAware; public function doWithTwig(): void { // $this->twig() ... } } ``` Related to #60265 Commits ------- 95b0f9b Fix ServiceMethodsSubscriberTrait for nullable service
2 parents 1e8b68a + 95b0f9b commit b59a25a

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/Symfony/Contracts/Service/ServiceSubscriberTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static function getSubscribedServices(): array
5151
$attribute = $attribute->newInstance();
5252
$attribute->key ??= self::class.'::'.$method->name;
5353
$attribute->type ??= $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType;
54-
$attribute->nullable = $returnType->allowsNull();
54+
$attribute->nullable = $attribute->nullable ?: $returnType->allowsNull();
5555

5656
if ($attribute->attributes) {
5757
$services[] = $attribute;

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public function testMethodsOnParentsAndChildrenAreIgnoredInGetSubscribedServices
2727
{
2828
$expected = [
2929
TestService::class.'::aService' => Service2::class,
30-
TestService::class.'::nullableService' => '?'.Service2::class,
30+
TestService::class.'::nullableInAttribute' => '?'.Service2::class,
31+
TestService::class.'::nullableReturnType' => '?'.Service2::class,
3132
new SubscribedService(TestService::class.'::withAttribute', Service2::class, true, new Required()),
3233
];
3334

@@ -103,8 +104,18 @@ public function aService(): Service2
103104
{
104105
}
105106

107+
#[SubscribedService(nullable: true)]
108+
public function nullableInAttribute(): Service2
109+
{
110+
if (!$this->container->has(__METHOD__)) {
111+
throw new \LogicException();
112+
}
113+
114+
return $this->container->get(__METHOD__);
115+
}
116+
106117
#[SubscribedService]
107-
public function nullableService(): ?Service2
118+
public function nullableReturnType(): ?Service2
108119
{
109120
}
110121

0 commit comments

Comments
 (0)
0