Skip to content

[DependencyInjection] remove ServiceSubscriberTrait deprecation layer #43783

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

Merged
merged 1 commit into from
Oct 27, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LegacyTestServiceSubscriberChild;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LegacyTestServiceSubscriberParent;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition2;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition3;
Expand Down Expand Up @@ -146,67 +144,6 @@ public function testExtraServiceSubscriber()
$container->compile();
}

/**
* @group legacy
*/
public function testServiceSubscriberTrait()
{
$container = new ContainerBuilder();

$container->register('foo', LegacyTestServiceSubscriberChild::class)
->addMethodCall('setContainer', [new Reference(PsrContainerInterface::class)])
->addTag('container.service_subscriber')
;

(new RegisterServiceSubscribersPass())->process($container);
(new ResolveServiceSubscribersPass())->process($container);

$foo = $container->getDefinition('foo');
$locator = $container->getDefinition((string) $foo->getMethodCalls()[0][1][0]);

$expected = [
LegacyTestServiceSubscriberChild::class.'::invalidDefinition' => new ServiceClosureArgument(new TypedReference('Symfony\Component\DependencyInjection\Tests\Fixtures\InvalidDefinition', 'Symfony\Component\DependencyInjection\Tests\Fixtures\InvalidDefinition', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
LegacyTestServiceSubscriberChild::class.'::testDefinition2' => new ServiceClosureArgument(new TypedReference(TestDefinition2::class, TestDefinition2::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
LegacyTestServiceSubscriberChild::class.'::testDefinition3' => new ServiceClosureArgument(new TypedReference(TestDefinition3::class, TestDefinition3::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
LegacyTestServiceSubscriberParent::class.'::testDefinition1' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class, TestDefinition1::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
];

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

/**
* @group legacy
*/
public function testServiceSubscriberTraitWithGetter()
{
$container = new ContainerBuilder();

$subscriber = new class() implements ServiceSubscriberInterface {
use ServiceSubscriberTrait;

public function getFoo(): \stdClass
{
}
};
$container->register('foo', \get_class($subscriber))
->addMethodCall('setContainer', [new Reference(PsrContainerInterface::class)])
->addTag('container.service_subscriber');

(new RegisterServiceSubscribersPass())->process($container);
(new ResolveServiceSubscribersPass())->process($container);

$foo = $container->getDefinition('foo');
$locator = $container->getDefinition((string) $foo->getMethodCalls()[0][1][0]);

$expected = [
\get_class($subscriber).'::getFoo' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'foo')),
];
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
}

/**
* @requires PHP 8
*/
public function testServiceSubscriberTraitWithSubscribedServiceAttribute()
{
if (!class_exists(SubscribedService::class)) {
Expand Down Expand Up @@ -237,9 +174,6 @@ public function testServiceSubscriberTraitWithSubscribedServiceAttribute()
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
}

/**
* @requires PHP 8
*/
public function testServiceSubscriberTraitWithSubscribedServiceAttributeOnStaticMethod()
{
if (!class_exists(SubscribedService::class)) {
Expand All @@ -260,9 +194,6 @@ public static function method(): TestDefinition1
$subscriber::getSubscribedServices();
}

/**
* @requires PHP 8
*/
public function testServiceSubscriberTraitWithSubscribedServiceAttributeOnMethodWithRequiredParameters()
{
if (!class_exists(SubscribedService::class)) {
Expand All @@ -283,9 +214,6 @@ public function method($param1, $param2 = null): TestDefinition1
$subscriber::getSubscribedServices();
}

/**
* @requires PHP 8
*/
public function testServiceSubscriberTraitWithSubscribedServiceAttributeOnMethodMissingReturnType()
{
if (!class_exists(SubscribedService::class)) {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

64 changes: 17 additions & 47 deletions src/Symfony/Contracts/Service/ServiceSubscriberTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,61 +37,31 @@ public static function getSubscribedServices(): array
}

$services = \is_callable(['parent', __FUNCTION__]) ? parent::getSubscribedServices() : [];
$attributeOptIn = false;

if (\PHP_VERSION_ID >= 80000) {
foreach ((new \ReflectionClass(self::class))->getMethods() as $method) {
if (self::class !== $method->getDeclaringClass()->name) {
continue;
}

if (!$attribute = $method->getAttributes(SubscribedService::class)[0] ?? null) {
continue;
}

if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
throw new \LogicException(sprintf('Cannot use "%s" on method "%s::%s()" (can only be used on non-static, non-abstract methods with no parameters).', SubscribedService::class, self::class, $method->name));
}

if (!$returnType = $method->getReturnType()) {
throw new \LogicException(sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class, $method->name, self::class));
}

$serviceId = $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType;

if ($returnType->allowsNull()) {
$serviceId = '?'.$serviceId;
}

$services[$attribute->newInstance()->key ?? self::class.'::'.$method->name] = $serviceId;
$attributeOptIn = true;
foreach ((new \ReflectionClass(self::class))->getMethods() as $method) {
if (self::class !== $method->getDeclaringClass()->name) {
continue;
}
}

if (!$attributeOptIn) {
foreach ((new \ReflectionClass(self::class))->getMethods() as $method) {
if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
continue;
}

if (self::class !== $method->getDeclaringClass()->name) {
continue;
}
if (!$attribute = $method->getAttributes(SubscribedService::class)[0] ?? null) {
continue;
}

if (!$returnType = $method->getReturnType()) {
continue;
}
if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
throw new \LogicException(sprintf('Cannot use "%s" on method "%s::%s()" (can only be used on non-static, non-abstract methods with no parameters).', SubscribedService::class, self::class, $method->name));
}

if ($returnType->isBuiltin()) {
continue;
}
if (!$returnType = $method->getReturnType()) {
throw new \LogicException(sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class, $method->name, self::class));
}

if (\PHP_VERSION_ID >= 80000) {
trigger_deprecation('symfony/service-contracts', '2.5', 'Using "%s" in "%s" without using the "%s" attribute on any method is deprecated.', ServiceSubscriberTrait::class, self::class, SubscribedService::class);
}
$serviceId = $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType;

$services[self::class.'::'.$method->name] = '?'.($returnType instanceof \ReflectionNamedType ? $returnType->getName() : $returnType);
if ($returnType->allowsNull()) {
$serviceId = '?'.$serviceId;
}

$services[$attribute->newInstance()->key ?? self::class.'::'.$method->name] = $serviceId;
}

return $services;
Expand Down
29 changes: 0 additions & 29 deletions src/Symfony/Contracts/Tests/Service/ServiceSubscriberTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,6 @@

class ServiceSubscriberTraitTest extends TestCase
{
/**
* @group legacy
*/
public function testLegacyMethodsOnParentsAndChildrenAreIgnoredInGetSubscribedServices()
{
$expected = [LegacyTestService::class.'::aService' => '?'.Service2::class];

$this->assertEquals($expected, LegacyChildTestService::getSubscribedServices());
}

/**
* @requires PHP 8
*/
public function testMethodsOnParentsAndChildrenAreIgnoredInGetSubscribedServices()
{
$expected = [
Expand Down Expand Up @@ -67,22 +54,6 @@ public function setContainer(ContainerInterface $container)
}
}

class LegacyTestService extends ParentTestService implements ServiceSubscriberInterface
{
use ServiceSubscriberTrait;

public function aService(): Service2
{
}
}

class LegacyChildTestService extends LegacyTestService
{
public function aChildService(): Service3
{
}
}

class TestService extends ParentTestService implements ServiceSubscriberInterface
{
use ServiceSubscriberTrait;
Expand Down