[DI] Service locators can't be decorated

This commit is contained in:
Maciej Malarz 2019-09-23 12:01:45 +02:00 committed by Nicolas Grekas
parent 418c78c3bd
commit 343282b9d4
2 changed files with 55 additions and 3 deletions

View File

@ -52,14 +52,14 @@ class PassConfig
new ValidateEnvPlaceholdersPass(),
new ResolveChildDefinitionsPass(),
new RegisterServiceSubscribersPass(),
new DecoratorServicePass(),
new ResolveParameterPlaceHoldersPass(false),
new ResolveFactoryClassPass(),
new ResolveNamedArgumentsPass(),
new AutowireRequiredMethodsPass(),
new ResolveBindingsPass(),
new ServiceLocatorTagPass(),
new CheckDefinitionValidityPass(),
new DecoratorServicePass(),
new ResolveFactoryClassPass(),
new AutowireRequiredMethodsPass(),
new AutowirePass(false),
new ResolveTaggedIteratorArgumentPass(),
new ResolveServiceSubscribersPass(),

View File

@ -24,6 +24,7 @@ use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\DependencyInjection\Tests\Fixtures\BarTagClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooTagClass;
use Symfony\Contracts\Service\ServiceProviderInterface;
/**
* This class tests the integration of the different compiler passes.
@ -142,6 +143,29 @@ class IntegrationTest extends TestCase
$this->assertInstanceOf(DecoratedServiceSubscriber::class, $container->get(ServiceSubscriberStub::class));
}
public function testCanDecorateServiceLocator()
{
$container = new ContainerBuilder();
$container->register('foo', 'stdClass')->setPublic(true);
$container->register(ServiceLocator::class)
->addTag('container.service_locator')
->setArguments([[new Reference('foo')]])
;
$container->register(DecoratedServiceLocator::class)
->setDecoratedService(ServiceLocator::class)
->setPublic(true)
->setArguments([new Reference(DecoratedServiceLocator::class.'.inner')])
;
$container->compile();
$this->assertInstanceOf(DecoratedServiceLocator::class, $container->get(DecoratedServiceLocator::class));
$this->assertSame($container->get('foo'), $container->get(DecoratedServiceLocator::class)->get('foo'));
}
/**
* @dataProvider getYamlCompileTests
*/
@ -416,6 +440,34 @@ class DecoratedServiceSubscriber
{
}
class DecoratedServiceLocator implements ServiceProviderInterface
{
/**
* @var ServiceLocator
*/
private $locator;
public function __construct(ServiceLocator $locator)
{
$this->locator = $locator;
}
public function get($id)
{
return $this->locator->get($id);
}
public function has($id): bool
{
return $this->locator->has($id);
}
public function getProvidedServices(): array
{
return $this->locator->getProvidedServices();
}
}
class IntegrationTestStub extends IntegrationTestStubParent
{
}