diff --git a/src/Symfony/Component/DependencyInjection/Attribute/AsAlias.php b/src/Symfony/Component/DependencyInjection/Attribute/AsAlias.php index 2f03e5fcdf4e2..0839afa48ff44 100644 --- a/src/Symfony/Component/DependencyInjection/Attribute/AsAlias.php +++ b/src/Symfony/Component/DependencyInjection/Attribute/AsAlias.php @@ -20,12 +20,20 @@ final class AsAlias { /** - * @param string|null $id The id of the alias - * @param bool $public Whether to declare the alias public + * @var list + */ + public array $when = []; + + /** + * @param string|null $id The id of the alias + * @param bool $public Whether to declare the alias public + * @param string|list $when The environments under which the class will be registered as a service (i.e. "dev", "test", "prod") */ public function __construct( public ?string $id = null, public bool $public = false, + string|array $when = [], ) { + $this->when = (array) $when; } } diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index 07521bc863e42..df3486a9dc867 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -11,6 +11,7 @@ CHANGELOG for auto-configuration of classes excluded from the service container * Accept multiple auto-configuration callbacks for the same attribute class * Leverage native lazy objects when possible for lazy services + * Add `when` argument to `#[AsAlias]` 7.2 --- diff --git a/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php index 9e17bc424a2a9..bc38767bcb546 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php @@ -224,10 +224,14 @@ public function registerClasses(Definition $prototype, string $namespace, string if (null === $alias) { throw new LogicException(\sprintf('Alias cannot be automatically determined for class "%s". If you have used the #[AsAlias] attribute with a class implementing multiple interfaces, add the interface you want to alias to the first parameter of #[AsAlias].', $class)); } - if (isset($this->aliases[$alias])) { - throw new LogicException(\sprintf('The "%s" alias has already been defined with the #[AsAlias] attribute in "%s".', $alias, $this->aliases[$alias])); + + if (!$attribute->when || \in_array($this->env, $attribute->when, true)) { + if (isset($this->aliases[$alias])) { + throw new LogicException(\sprintf('The "%s" alias has already been defined with the #[AsAlias] attribute in "%s".', $alias, $this->aliases[$alias])); + } + + $this->aliases[$alias] = new Alias($class, $public); } - $this->aliases[$alias] = new Alias($class, $public); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/PrototypeAsAlias/WithAsAliasBothEnv.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/PrototypeAsAlias/WithAsAliasBothEnv.php new file mode 100644 index 0000000000000..252842be35ff2 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/PrototypeAsAlias/WithAsAliasBothEnv.php @@ -0,0 +1,10 @@ +registerClasses( (new Definition())->setAutoconfigured(true), 'Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\\', @@ -374,6 +377,15 @@ public static function provideResourcesWithAsAliasAttributes(): iterable AliasBarInterface::class => new Alias(WithAsAliasIdMultipleInterface::class), AliasFooInterface::class => new Alias(WithAsAliasIdMultipleInterface::class), ]]; + yield 'Dev-env specific' => ['PrototypeAsAlias/WithAsAlias*Env.php', [ + AliasFooInterface::class => new Alias(WithAsAliasDevEnv::class), + AliasBarInterface::class => new Alias(WithAsAliasBothEnv::class), + ], 'dev']; + yield 'Prod-env specific' => ['PrototypeAsAlias/WithAsAlias*Env.php', [ + AliasFooInterface::class => new Alias(WithAsAliasProdEnv::class), + AliasBarInterface::class => new Alias(WithAsAliasBothEnv::class), + ], 'prod']; + yield 'Test-env specific' => ['PrototypeAsAlias/WithAsAlias*Env.php', [], 'test']; } /**