From 90fd3ffeb79a350b8373b3eae08c94318498a64e Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Wed, 18 Jun 2025 12:27:01 +0200 Subject: [PATCH] [DependencyInjection] Allow extending `#[AsAlias]` attribute --- .../DependencyInjection/Attribute/AsAlias.php | 2 +- .../DependencyInjection/CHANGELOG.md | 5 ++++ .../DependencyInjection/Loader/FileLoader.php | 2 +- .../PrototypeAsAlias/WithCustomAsAlias.php | 25 +++++++++++++++++++ .../Tests/Loader/FileLoaderTest.php | 3 +++ 5 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/PrototypeAsAlias/WithCustomAsAlias.php diff --git a/src/Symfony/Component/DependencyInjection/Attribute/AsAlias.php b/src/Symfony/Component/DependencyInjection/Attribute/AsAlias.php index 0839afa48ff44..c74b0923dfedd 100644 --- a/src/Symfony/Component/DependencyInjection/Attribute/AsAlias.php +++ b/src/Symfony/Component/DependencyInjection/Attribute/AsAlias.php @@ -17,7 +17,7 @@ * @author Alan Poulain */ #[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)] -final class AsAlias +class AsAlias { /** * @var list diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index df3486a9dc867..5c6c41cfdf27b 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.4 +--- + + * Allow `#[AsAlias]` to be extended + 7.3 --- diff --git a/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php index bc38767bcb546..3cf23cf98eab4 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php @@ -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; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/PrototypeAsAlias/WithCustomAsAlias.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/PrototypeAsAlias/WithCustomAsAlias.php new file mode 100644 index 0000000000000..4f141909890d2 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/PrototypeAsAlias/WithCustomAsAlias.php @@ -0,0 +1,25 @@ + ['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),