Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DependencyInjection] Inherit "public" option when creating aliases with the auto_alias tag #41209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
Expand Down