Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 343282b

Browse filesBrowse files
malarzmnicolas-grekas
authored andcommitted
[DI] Service locators can't be decorated
1 parent 418c78c commit 343282b
Copy full SHA for 343282b

File tree

2 files changed

+55
-3
lines changed
Filter options

2 files changed

+55
-3
lines changed

‎src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php
+3-3Lines 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(),

‎src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php
+52Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.