[FrameworkBundle] acces public-deprecated services via the private container to remove false-positive deprecations

This commit is contained in:
Nicolas Grekas 2020-12-07 14:43:04 +01:00
parent 7c31fa0537
commit d502ba9e5d
3 changed files with 19 additions and 3 deletions

View File

@ -22,6 +22,13 @@ use Symfony\Component\DependencyInjection\Reference;
*/ */
class TestServiceContainerWeakRefPass implements CompilerPassInterface class TestServiceContainerWeakRefPass implements CompilerPassInterface
{ {
private $privateTagName;
public function __construct(string $privateTagName = 'container.private')
{
$this->privateTagName = $privateTagName;
}
public function process(ContainerBuilder $container) public function process(ContainerBuilder $container)
{ {
if (!$container->hasDefinition('test.private_services_locator')) { if (!$container->hasDefinition('test.private_services_locator')) {
@ -33,7 +40,7 @@ class TestServiceContainerWeakRefPass implements CompilerPassInterface
$hasErrors = method_exists(Definition::class, 'hasErrors') ? 'hasErrors' : 'getErrors'; $hasErrors = method_exists(Definition::class, 'hasErrors') ? 'hasErrors' : 'getErrors';
foreach ($definitions as $id => $definition) { foreach ($definitions as $id => $definition) {
if ($id && '.' !== $id[0] && (!$definition->isPublic() || $definition->isPrivate()) && !$definition->$hasErrors() && !$definition->isAbstract()) { if ($id && '.' !== $id[0] && (!$definition->isPublic() || $definition->isPrivate() || $definition->hasTag($this->privateTagName)) && !$definition->$hasErrors() && !$definition->isAbstract()) {
$privateServices[$id] = new Reference($id, ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE); $privateServices[$id] = new Reference($id, ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE);
} }
} }

View File

@ -36,6 +36,12 @@ class TestServiceContainerRefPassesTest extends TestCase
->setPublic(true) ->setPublic(true)
->addArgument(new Reference('Test\private_used_shared_service')) ->addArgument(new Reference('Test\private_used_shared_service'))
->addArgument(new Reference('Test\private_used_non_shared_service')) ->addArgument(new Reference('Test\private_used_non_shared_service'))
->addArgument(new Reference('Test\soon_private_service'))
;
$container->register('Test\soon_private_service')
->setPublic(true)
->addTag('container.private', ['package' => 'foo/bar', 'version' => '1.42'])
; ;
$container->register('Test\private_used_shared_service'); $container->register('Test\private_used_shared_service');
@ -48,6 +54,7 @@ class TestServiceContainerRefPassesTest extends TestCase
$expected = [ $expected = [
'Test\private_used_shared_service' => new ServiceClosureArgument(new Reference('Test\private_used_shared_service')), 'Test\private_used_shared_service' => new ServiceClosureArgument(new Reference('Test\private_used_shared_service')),
'Test\private_used_non_shared_service' => new ServiceClosureArgument(new Reference('Test\private_used_non_shared_service')), 'Test\private_used_non_shared_service' => new ServiceClosureArgument(new Reference('Test\private_used_non_shared_service')),
'Test\soon_private_service' => new ServiceClosureArgument(new Reference('.container.private.Test\soon_private_service')),
'Psr\Container\ContainerInterface' => new ServiceClosureArgument(new Reference('service_container')), 'Psr\Container\ContainerInterface' => new ServiceClosureArgument(new Reference('service_container')),
'Symfony\Component\DependencyInjection\ContainerInterface' => new ServiceClosureArgument(new Reference('service_container')), 'Symfony\Component\DependencyInjection\ContainerInterface' => new ServiceClosureArgument(new Reference('service_container')),
]; ];

View File

@ -29,12 +29,14 @@ class AddConsoleCommandPass implements CompilerPassInterface
private $commandLoaderServiceId; private $commandLoaderServiceId;
private $commandTag; private $commandTag;
private $noPreloadTag; private $noPreloadTag;
private $privateTagName;
public function __construct(string $commandLoaderServiceId = 'console.command_loader', string $commandTag = 'console.command', string $noPreloadTag = 'container.no_preload') public function __construct(string $commandLoaderServiceId = 'console.command_loader', string $commandTag = 'console.command', string $noPreloadTag = 'container.no_preload', string $privateTagName = 'container.private')
{ {
$this->commandLoaderServiceId = $commandLoaderServiceId; $this->commandLoaderServiceId = $commandLoaderServiceId;
$this->commandTag = $commandTag; $this->commandTag = $commandTag;
$this->noPreloadTag = $noPreloadTag; $this->noPreloadTag = $noPreloadTag;
$this->privateTagName = $privateTagName;
} }
public function process(ContainerBuilder $container) public function process(ContainerBuilder $container)
@ -62,7 +64,7 @@ class AddConsoleCommandPass implements CompilerPassInterface
} }
if (null === $commandName) { if (null === $commandName) {
if (!$definition->isPublic() || $definition->isPrivate()) { if (!$definition->isPublic() || $definition->isPrivate() || $definition->hasTag($this->privateTagName)) {
$commandId = 'console.command.public_alias.'.$id; $commandId = 'console.command.public_alias.'.$id;
$container->setAlias($commandId, $id)->setPublic(true); $container->setAlias($commandId, $id)->setPublic(true);
$id = $commandId; $id = $commandId;