Skip to content

[DependencyInjection] don't move locator tag for service subscriber #48093

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
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 @@ -42,7 +42,7 @@ public function process(ContainerBuilder $container)

$tagsToKeep = $container->hasParameter('container.behavior_describing_tags')
? $container->getParameter('container.behavior_describing_tags')
: ['container.do_not_inline', 'container.service_locator', 'container.service_subscriber'];
: ['container.do_not_inline', 'container.service_locator', 'container.service_subscriber', 'container.service_subscriber.locator'];

foreach ($definitions as [$id, $definition]) {
$decoratedService = $definition->getDecoratedService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public function testProcessLeavesServiceSubscriberTagOnOriginalDefinition()
$container = new ContainerBuilder();
$container
->register('foo')
->setTags(['container.service_subscriber' => [], 'bar' => ['attr' => 'baz']])
->setTags(['container.service_subscriber' => [], 'container.service_subscriber.locator' => [], 'bar' => ['attr' => 'baz']])
;
$container
->register('baz')
Expand All @@ -259,7 +259,7 @@ public function testProcessLeavesServiceSubscriberTagOnOriginalDefinition()

$this->process($container);

$this->assertEquals(['container.service_subscriber' => []], $container->getDefinition('baz.inner')->getTags());
$this->assertEquals(['container.service_subscriber' => [], 'container.service_subscriber.locator' => []], $container->getDefinition('baz.inner')->getTags());
$this->assertEquals(['bar' => ['attr' => 'baz'], 'foobar' => ['attr' => 'bar']], $container->getDefinition('baz')->getTags());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\DependencyInjection\Tests\Compiler;

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
Expand Down Expand Up @@ -129,19 +130,41 @@ public function testProcessInlinesWhenThereAreMultipleReferencesButFromTheSameDe
$this->assertFalse($container->hasDefinition('c'), 'Service C was not inlined.');
}

public function testCanDecorateServiceSubscriber()
public function testCanDecorateServiceSubscriberUsingBinding()
{
$container = new ContainerBuilder();
$container->register(ServiceSubscriberStub::class)
->addTag('container.service_subscriber')
->setPublic(true);

$container->register(DecoratedServiceSubscriber::class)
->setProperty('inner', new Reference(DecoratedServiceSubscriber::class.'.inner'))
->setDecoratedService(ServiceSubscriberStub::class);

$container->compile();

$this->assertInstanceOf(DecoratedServiceSubscriber::class, $container->get(ServiceSubscriberStub::class));
$this->assertInstanceOf(ServiceSubscriberStub::class, $container->get(ServiceSubscriberStub::class)->inner);
$this->assertInstanceOf(ServiceLocator::class, $container->get(ServiceSubscriberStub::class)->inner->container);
}

public function testCanDecorateServiceSubscriberReplacingArgument()
{
$container = new ContainerBuilder();
$container->register(ServiceSubscriberStub::class)
->setArguments([new Reference(ContainerInterface::class)])
->addTag('container.service_subscriber')
->setPublic(true);

$container->register(DecoratedServiceSubscriber::class)
->setProperty('inner', new Reference(DecoratedServiceSubscriber::class.'.inner'))
->setDecoratedService(ServiceSubscriberStub::class);

$container->compile();

$this->assertInstanceOf(DecoratedServiceSubscriber::class, $container->get(ServiceSubscriberStub::class));
$this->assertInstanceOf(ServiceSubscriberStub::class, $container->get(ServiceSubscriberStub::class)->inner);
$this->assertInstanceOf(ServiceLocator::class, $container->get(ServiceSubscriberStub::class)->inner->container);
}

public function testCanDecorateServiceLocator()
Expand Down Expand Up @@ -515,6 +538,13 @@ public function testTaggedServiceLocatorWithDefaultIndex()

class ServiceSubscriberStub implements ServiceSubscriberInterface
{
public $container;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

public static function getSubscribedServices(): array
{
return [];
Expand All @@ -523,6 +553,7 @@ public static function getSubscribedServices(): array

class DecoratedServiceSubscriber
{
public $inner;
}

class DecoratedServiceLocator implements ServiceProviderInterface
Expand Down