From d81170d8cf9183ad7f54d62eb69c0443a89c66d5 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Thu, 13 May 2021 01:53:02 +0200 Subject: [PATCH] Inherit "public" option when creating aliases with the auto_alias tag --- .../DependencyInjection/CHANGELOG.md | 2 ++ .../Compiler/AutoAliasServicePass.php | 9 ++++++- .../Compiler/AutoAliasServicePassTest.php | 26 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index 36a576e99d739..3aac93bd4cb54 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -18,6 +18,8 @@ CHANGELOG * Add support for `ConfigBuilder` in the `PhpFileLoader` * Add `ContainerConfigurator::env()` to get the current environment * Add `#[Target]` to tell how a dependency is used and hint named autowiring aliases + * Add inheriting of the `public` option when creating aliases with the `auto_alias` tag + * Deprecate aliases created with the `auto_alias` tag being `public` by default 5.2.0 ----- diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php index 03420683a205a..6809874f484b3 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/AutoAliasServicePass.php @@ -33,7 +33,14 @@ public function process(ContainerBuilder $container) $aliasId = $container->getParameterBag()->resolveValue($tag['format']); if ($container->hasDefinition($aliasId) || $container->hasAlias($aliasId)) { - $container->setAlias($serviceId, new Alias($aliasId, true)); + $definition = $container->getDefinition($serviceId); + + $public = ($definition->getChanges()['public'] ?? false) ? $definition->isPublic() : null; + if (null === $public) { + trigger_deprecation('symfony/dependency-injection', '5.4', 'Aliases created with the "auto_alias" tag will be private by default in the future, you can set them to public by using the "public" option.'); + } + + $container->setAlias($serviceId, new Alias($aliasId, $public ?? true)); } } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutoAliasServicePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutoAliasServicePassTest.php index 26a0ed1555022..db428d10f4f1e 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutoAliasServicePassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutoAliasServicePassTest.php @@ -63,6 +63,7 @@ public function testProcessWithExistingAlias() $container = new ContainerBuilder(); $container->register('example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault') + ->setPublic(false) ->addTag('auto_alias', ['format' => '%existing%.example']); $container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql'); @@ -76,6 +77,31 @@ public function testProcessWithExistingAlias() $this->assertSame('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql', $container->getDefinition('mysql.example')->getClass()); } + public function testProcessWithPublicAttribute() + { + $container = new ContainerBuilder(); + + $container->register('example-private', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault') + ->setPublic(false) + ->addTag('auto_alias', ['format' => '%existing%.example']); + + $container->register('example-public', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault') + ->setPublic(true) + ->addTag('auto_alias', ['format' => '%existing%.example']); + + $container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql'); + $container->setParameter('existing', 'mysql'); + + $pass = new AutoAliasServicePass(); + $pass->process($container); + + $this->assertTrue($container->hasAlias('example-private')); + $this->assertFalse($container->getAlias('example-private')->isPublic()); + + $this->assertTrue($container->hasAlias('example-public')); + $this->assertTrue($container->getAlias('example-public')->isPublic()); + } + public function testProcessWithManualAlias() { $container = new ContainerBuilder();