Skip to content

[DependencyInjection] Introduce #[AsAlias] attribute #49198

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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -686,6 +687,10 @@ public function load(array $configs, ContainerBuilder $container)
$definition->addTag('messenger.message_handler', $tagAttributes);
});

$container->registerAttributeForAutoconfiguration(AsAlias::class, static function (ChildDefinition $definition, AsAlias $attribute, \ReflectionClass $reflector): void {
$definition->addTag(AsAlias::class, ['alias' => $attribute->alias]);
});

if (!$container->getParameter('kernel.debug')) {
// remove tagged iterator argument for resource checkers
$container->getDefinition('config_cache_factory')->setArguments([]);
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/DependencyInjection/Attribute/AsAlias.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Attribute;

#[\Attribute(\Attribute::TARGET_CLASS)]
class AsAlias
{
public function __construct(
public string $alias,
) {
}
}
32 changes: 32 additions & 0 deletions src/Symfony/Component/DependencyInjection/Compiler/AliasPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Attribute\AsAlias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;

final class AliasPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
foreach ($container->findTaggedServiceIds(AsAlias::class) as $serviceId => $tags) {
foreach ($tags as $tag) {
if (false === isset($tag['alias'])) {
throw new InvalidArgumentException(sprintf('The "alias" attribute is mandatory for the "%s" tag.', AsAlias::class));
}

$container->setAlias($tag['alias'], $serviceId);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function __construct()
new CheckCircularReferencesPass(),
new CheckReferenceValidityPass(),
new CheckArgumentsValidityPass(false),
new AliasPass(),
]];

$this->removingPasses = [[
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\AliasPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class AsAliasAttributeTest extends TestCase
{
public function testProcessWithMissingAliasAttribute()
{
$container = new ContainerBuilder();

$container->register('foo', \DateTimeImmutable::class)
->addTag(AsAlias::class, []);

self::expectException(\InvalidArgumentException::class);

(new AliasPass())->process($container);
}

public function testProcess()
{
$container = new ContainerBuilder();

$container->register(HelloWorldService::class)
->setAutowired(true)
->setAutoconfigured(true);

$container->registerAttributeForAutoconfiguration(AsAlias::class, static function (ChildDefinition $definition, AsAlias $attribute, \ReflectionClass $reflector): void {
$definition->addTag(
AsAlias::class,
[
'alias' => $attribute->alias,
]
);
});

$container->getCompilerPassConfig()->setRemovingPasses([]);
$container->compile();

self::assertTrue($container->hasAlias('bar'));
self::assertSame(HelloWorldService::class, (string) $container->getAlias('bar'));
}
}

#[AsAlias(alias: 'bar')]
class HelloWorldService
{
}