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),