Skip to content

Commit 27b81b7

Browse files
bug #33670 [DI] Service locators can't be decorated (malarzm)
This PR was squashed before being merged into the 4.3 branch. Discussion ---------- [DI] Service locators can't be decorated | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | n/a | License | MIT | Doc PR | n/a This popped up while I was trying to update my work project as we have decorated the `messenger.receiver_locator` service. Not sure if this is a regression in DI or a change in messenger that caused the issue thus I'm not marking this as a BC break. Exception while trying to compile the container: ``` Invalid definition for service "Symfony\Component\DependencyInjection\Tests\Compiler\DecoratedServiceLocator": an array of references is expected as first argument when the "container.service_locator" tag is set. ``` Expected result: service locator can be decorated. Commits ------- 343282b9d4 [DI] Service locators can't be decorated
2 parents 6fd0f2c + b949c35 commit 27b81b7

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

Compiler/PassConfig.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ public function __construct()
5252
new ValidateEnvPlaceholdersPass(),
5353
new ResolveChildDefinitionsPass(),
5454
new RegisterServiceSubscribersPass(),
55-
new DecoratorServicePass(),
5655
new ResolveParameterPlaceHoldersPass(false),
57-
new ResolveFactoryClassPass(),
5856
new ResolveNamedArgumentsPass(),
59-
new AutowireRequiredMethodsPass(),
6057
new ResolveBindingsPass(),
6158
new ServiceLocatorTagPass(),
6259
new CheckDefinitionValidityPass(),
60+
new DecoratorServicePass(),
61+
new ResolveFactoryClassPass(),
62+
new AutowireRequiredMethodsPass(),
6363
new AutowirePass(false),
6464
new ResolveTaggedIteratorArgumentPass(),
6565
new ResolveServiceSubscribersPass(),

Tests/Compiler/IntegrationTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\DependencyInjection\Tests\Fixtures\BarTagClass;
2525
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedClass;
2626
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooTagClass;
27+
use Symfony\Contracts\Service\ServiceProviderInterface;
2728

2829
/**
2930
* This class tests the integration of the different compiler passes.
@@ -142,6 +143,29 @@ public function testCanDecorateServiceSubscriber()
142143
$this->assertInstanceOf(DecoratedServiceSubscriber::class, $container->get(ServiceSubscriberStub::class));
143144
}
144145

146+
public function testCanDecorateServiceLocator()
147+
{
148+
$container = new ContainerBuilder();
149+
150+
$container->register('foo', 'stdClass')->setPublic(true);
151+
152+
$container->register(ServiceLocator::class)
153+
->addTag('container.service_locator')
154+
->setArguments([[new Reference('foo')]])
155+
;
156+
157+
$container->register(DecoratedServiceLocator::class)
158+
->setDecoratedService(ServiceLocator::class)
159+
->setPublic(true)
160+
->setArguments([new Reference(DecoratedServiceLocator::class.'.inner')])
161+
;
162+
163+
$container->compile();
164+
165+
$this->assertInstanceOf(DecoratedServiceLocator::class, $container->get(DecoratedServiceLocator::class));
166+
$this->assertSame($container->get('foo'), $container->get(DecoratedServiceLocator::class)->get('foo'));
167+
}
168+
145169
/**
146170
* @dataProvider getYamlCompileTests
147171
*/
@@ -416,6 +440,34 @@ class DecoratedServiceSubscriber
416440
{
417441
}
418442

443+
class DecoratedServiceLocator implements ServiceProviderInterface
444+
{
445+
/**
446+
* @var ServiceLocator
447+
*/
448+
private $locator;
449+
450+
public function __construct(ServiceLocator $locator)
451+
{
452+
$this->locator = $locator;
453+
}
454+
455+
public function get($id)
456+
{
457+
return $this->locator->get($id);
458+
}
459+
460+
public function has($id): bool
461+
{
462+
return $this->locator->has($id);
463+
}
464+
465+
public function getProvidedServices(): array
466+
{
467+
return $this->locator->getProvidedServices();
468+
}
469+
}
470+
419471
class IntegrationTestStub extends IntegrationTestStubParent
420472
{
421473
}

0 commit comments

Comments
 (0)