8000 only make services optional if return type is nullable · symfony/symfony@bfac6f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit bfac6f2

Browse files
committed
only make services optional if return type is nullable
1 parent 2fa213f commit bfac6f2

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterServiceSubscribersPassTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,11 @@ public function testServiceSubscriberTraitWithSubscribedServiceAttribute()
227227
$locator = $container->getDefinition((string) $foo->getMethodCalls()[0][1][0]);
228228

229229
$expected = [
230-
TestServiceSubscriberChild::class.'::invalidDefinition' => new ServiceClosureArgument(new TypedReference('Symfony\Component\DependencyInjection\Tests\Fixtures\InvalidDefinition', 'Symfony\Component\DependencyInjection\Tests\Fixtures\InvalidDefinition', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
230+
TestServiceSubscriberChild::class.'::invalidDefinition' => new ServiceClosureArgument(new TypedReference('Symfony\Component\DependencyInjection\Tests\Fixtures\InvalidDefinition', 'Symfony\Component\DependencyInjection\Tests\Fixtures\InvalidDefinition')),
231231
TestServiceSubscriberChild::class.'::testDefinition2' => new ServiceClosureArgument(new TypedReference(TestDefinition2::class, TestDefinition2::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
232-
TestServiceSubscriberChild::class.'::testDefinition4' => new ServiceClosureArgument(new TypedReference(TestDefinition3::class, TestDefinition3::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
233-
TestServiceSubscriberParent::class.'::testDefinition1' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class, TestDefinition1::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
234-
'custom_name' => new ServiceClosureArgument(new TypedReference(TestDefinition3::class, TestDefinition3::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'custom_name')),
232+
TestServiceSubscriberChild::class.'::testDefinition4' => new ServiceClosureArgument(new TypedReference(TestDefinition3::class, TestDefinition3::class)),
233+
TestServiceSubscriberParent::class.'::testDefinition1' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class, TestDefinition1::class)),
234+
'custom_name' => new ServiceClosureArgument(new TypedReference(TestDefinition3::class, TestDefinition3::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'custom_name')),
235235
];
236236

237237
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));

src/Symfony/Component/DependencyInjection/Tests/Fixtures/TestServiceSubscriberChild.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class TestServiceSubscriberChild extends TestServiceSubscriberParent
1111
use TestServiceSubscriberTrait;
1212

1313
#[SubscribedService]
14-
private function testDefinition2(): TestDefinition2
14+
private function testDefinition2(): ?TestDefinition2
1515
{
1616
return $this->container->get(__METHOD__);
1717
}

src/Symfony/Contracts/Service/ServiceSubscriberTrait.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ public static function getSubscribedServices(): array
5757
throw new \LogicException(sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class, $method->name, self::class));
5858
}
5959

60-
$services[$attribute->newInstance()->key ?? self::class.'::'.$method->name] = '?'.$returnType->getName();
60+
$serviceId = $returnType instanceof \ReflectionNamedType ? $returnType->getName() : $returnType;
61+
62+
if ($returnType->allowsNull()) {
63+
$serviceId = '?'.$serviceId;
64+
}
65+
66+
$services[$attribute->newInstance()->key ?? self::class.'::'.$method->name] = $serviceId;
6167
$attributeOptIn = true;
6268
}
6369
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ public function testLegacyMethodsOnParentsAndChildrenAreIgnoredInGetSubscribedSe
3737
*/
3838
public function testMethodsOnParentsAndChildrenAreIgnoredInGetSubscribedServices()
3939
{
40-
$expected = [TestService::class.'::aService' => '?'.Service2::class];
40+
$expected = [
41+
TestService::class.'::aService' => Service2::class,
42+
TestService::class.'::nullableService' => '?'.Service2::class,
43+
];
4144

4245
$this->assertEquals($expected, ChildTestService::getSubscribedServices());
4346
}
@@ -88,6 +91,11 @@ class TestService extends ParentTestService implements ServiceSubscriberInterfac
8891
public function aService(): Service2
8992
{
9093
}
94+
95+
#[SubscribedService]
96+
public function nullableService(): ?Service2
97+
{
98+
}
9199
}
92100

93101
class ChildTestService extends TestService

0 commit comments

Comments
 (0)
{"resolvedServerColorMode":"day"}
0