Skip to content

Commit 2020177

Browse files
committed
[DependencyInjection] Add "when" argument to #[AsAlias]
1 parent 5b65bd3 commit 2020177

File tree

7 files changed

+64
-8
lines changed

7 files changed

+64
-8
lines changed

src/Symfony/Component/DependencyInjection/Attribute/AsAlias.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,20 @@
2020
final class AsAlias
2121
{
2222
/**
23-
* @param string|null $id The id of the alias
24-
* @param bool $public Whether to declare the alias public
23+
* @var list<string>
24+
*/
25+
public array $when = [];
26+
27+
/**
28+
* @param string|null $id The id of the alias
29+
* @param bool $public Whether to declare the alias public
30+
* @param string|list<string> $when The environments under which the class will be registered as a service (i.e. "dev", "test", "prod")
2531
*/
2632
public function __construct(
2733
public ?string $id = null,
2834
public bool $public = false,
35+
string|array $when = [],
2936
) {
37+
$this->when = is_array($when) ? $when : [$when];
3038
}
3139
}

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111
for auto-configuration of classes excluded from the service container
1212
* Accept multiple auto-configuration callbacks for the same attribute class
1313
* Leverage native lazy objects when possible for lazy services
14+
* Add `when` argument to `#[AsAlias]`
1415

1516
7.2
1617
---

src/Symfony/Component/DependencyInjection/Loader/FileLoader.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,14 @@ public function registerClasses(Definition $prototype, string $namespace, string
224224
if (null === $alias) {
225225
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));
226226
}
227-
if (isset($this->aliases[$alias])) {
228-
throw new LogicException(\sprintf('The "%s" alias has already been defined with the #[AsAlias] attribute in "%s".', $alias, $this->aliases[$alias]));
227+
228+
if ([] === $attribute->when || \in_array($this->env, $attribute->when, true)) {
229+
if (isset($this->aliases[$alias])) {
230+
throw new LogicException(\sprintf('The "%s" alias has already been defined with the #[AsAlias] attribute in "%s".', $alias, $this->aliases[$alias]));
231+
}
232+
233+
$this->aliases[$alias] = new Alias($class, $public);
229234
}
230-
$this->aliases[$alias] = new Alias($class, $public);
231235
}
232236
}
233237

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias;
4+
5+
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
6+
7+
#[AsAlias(id: AliasBarInterface::class, when: ['dev', 'prod'])]
8+
class WithAsAliasBothEnv
9+
{
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias;
4+
5+
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
6+
7+
#[AsAlias(id: AliasFooInterface::class, when: 'dev')]
8+
class WithAsAliasDevEnv
9+
{
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias;
4+
5+
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
6+
7+
#[AsAlias(id: AliasFooInterface::class, when: 'prod')]
8+
class WithAsAliasProdEnv
9+
{
10+
}

src/Symfony/Component/DependencyInjection/Tests/Loader/FileLoaderTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2727
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
2828
use Symfony\Component\DependencyInjection\Reference;
29+
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasBothEnv;
2930
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses\MissingParent;
3031
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo;
3132
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\FooInterface;
@@ -39,9 +40,11 @@
3940
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\AliasBarInterface;
4041
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\AliasFooInterface;
4142
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAlias;
43+
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasDevEnv;
4244
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasIdMultipleInterface;
4345
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasInterface;
4446
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasMultiple;
47+
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasProdEnv;
4548
use Symfony\Component\DependencyInjection\Tests\Fixtures\Utils\NotAService;
4649

4750
class FileLoaderTest extends TestCase
@@ -174,7 +177,8 @@ public function testRegisterClassesWithExcludeAsArray()
174177
$loader->registerClasses(
175178
new Definition(),
176179
'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\',
177-
'Prototype/*', [
180+
'Prototype/*',
181+
[
178182
'Prototype/%sub_dir%',
179183
'Prototype/OtherDir/AnotherSub/DeeperBaz.php',
180184
]
@@ -349,10 +353,10 @@ public function testRegisterThrowsWithBothWhenAndNotWhenAttribute()
349353
/**
350354
* @dataProvider provideResourcesWithAsAliasAttributes
351355
*/
352-
public function testRegisterClassesWithAsAlias(string $resource, array $expectedAliases)
356+
public function testRegisterClassesWithAsAlias(string $resource, array $expectedAliases, ?string $env = null)
353357
{
354358
$container = new ContainerBuilder();
355-
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
359+
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'), $env);
356360
$loader->registerClasses(
357361
(new Definition())->setAutoconfigured(true),
358362
'Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\\',
@@ -374,6 +378,15 @@ public static function provideResourcesWithAsAliasAttributes(): iterable
374378
AliasBarInterface::class => new Alias(WithAsAliasIdMultipleInterface::class),
375379
AliasFooInterface::class => new Alias(WithAsAliasIdMultipleInterface::class),
376380
]];
381+
yield 'Dev-env specific' => ['PrototypeAsAlias/WithAsAlias*Env.php', [
382+
AliasFooInterface::class => new Alias(WithAsAliasDevEnv::class),
383+
AliasBarInterface::class => new Alias(WithAsAliasBothEnv::class),
384+
], 'dev'];
385+
yield 'Prod-env specific' => ['PrototypeAsAlias/WithAsAlias*Env.php', [
386+
AliasFooInterface::class => new Alias(WithAsAliasProdEnv::class),
387+
AliasBarInterface::class => new Alias(WithAsAliasBothEnv::class),
388+
], 'prod'];
389+
yield 'Test-env specific' => ['PrototypeAsAlias/WithAsAlias*Env.php', [], 'test'];
377390
}
378391

379392
/**

0 commit comments

Comments
 (0)