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

Skip to content

[DependencyInjection] Allow extending #[AsAlias] attribute #60819

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

Merged
merged 1 commit into from
Jun 18, 2025
Merged
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 @@ -17,7 +17,7 @@
* @author Alan Poulain <[email protected]>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class AsAlias
class AsAlias
{
/**
* @var list<string>
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.4
---

* Allow `#[AsAlias]` to be extended

7.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public function registerClasses(Definition $prototype, string $namespace, string
}
$r = $this->container->getReflectionClass($class);
$defaultAlias = 1 === \count($interfaces) ? $interfaces[0] : null;
foreach ($r->getAttributes(AsAlias::class) as $attr) {
foreach ($r->getAttributes(AsAlias::class, \ReflectionAttribute::IS_INSTANCEOF) as $attr) {
/** @var AsAlias $attribute */
$attribute = $attr->newInstance();
$alias = $attribute->id ?? $defaultAlias;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias;

use Symfony\Component\DependencyInjection\Attribute\AsAlias;

#[AsProductionAlias(id: AliasFooInterface::class)]
class WithCustomAsAlias
{
}

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class AsProductionAlias extends AsAlias
{
/**
* @param string|null $id The id of the alias
* @param bool $public Whether to declare the alias public
*/
public function __construct(
public ?string $id = null,
public bool $public = false,
) {
parent::__construct($id, $public, ['prod']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasInterface;
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasMultiple;
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasProdEnv;
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithCustomAsAlias;
use Symfony\Component\DependencyInjection\Tests\Fixtures\Utils\NotAService;

class FileLoaderTest extends TestCase
Expand Down Expand Up @@ -368,6 +369,8 @@ public function testRegisterClassesWithAsAlias(string $resource, array $expected
public static function provideResourcesWithAsAliasAttributes(): iterable
{
yield 'Private' => ['PrototypeAsAlias/{WithAsAlias,AliasFooInterface}.php', [AliasFooInterface::class => new Alias(WithAsAlias::class)]];
yield 'PrivateCustomAlias' => ['PrototypeAsAlias/{WithCustomAsAlias,AliasFooInterface}.php', [AliasFooInterface::class => new Alias(WithCustomAsAlias::class)], 'prod'];
yield 'PrivateCustomAliasNoMatch' => ['PrototypeAsAlias/{WithCustomAsAlias,AliasFooInterface}.php', [], 'dev'];
yield 'Interface' => ['PrototypeAsAlias/{WithAsAliasInterface,AliasFooInterface}.php', [AliasFooInterface::class => new Alias(WithAsAliasInterface::class)]];
yield 'Multiple' => ['PrototypeAsAlias/{WithAsAliasMultiple,AliasFooInterface}.php', [
AliasFooInterface::class => new Alias(WithAsAliasMultiple::class, true),
Expand Down
Loading