10BC0 feature #60186 [DependencyInjection] Add "when" argument to #[AsAlias… · symfony/symfony@d5b5581 · GitHub
[go: up one dir, main page]

Skip to content

Commit d5b5581

Browse files
feature #60186 [DependencyInjection] Add "when" argument to #[AsAlias] (Zuruuh)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [DependencyInjection] Add "when" argument to #[AsAlias] | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #60118 | License | MIT Also I wonder if it would make sense to accept an array of environments in `#[AsAlias]`, since in practice if I want to use the same service in two envs I'd have to duplicate the whole declaration now ```php #[AsAlias(MyInterface::class, when: 'dev'] #[AsAlias(MyInterface::class, when: 'test'] class MyClass {} ``` Thoughts ? Commits ------- 187524d [DependencyInjection] Add "when" argument to #[AsAlias]
2 parents f2357a0 + 187524d commit d5b5581

File tree

7 files changed

+62
-7
lines changed
  • src/Symfony/Component/DependencyInjection
    • Attribute
  • Loader
  • Tests
  • 7 files changed

    +62
    -7
    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 = (array) $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
    D63E
    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: 14 additions & 2 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
    @@ -349,10 +352,10 @@ public function testRegisterThrowsWithBothWhenAndNotWhenAttribute()
    349352
    /**
    350353
    * @dataProvider provideResourcesWithAsAliasAttributes
    351354
    */
    352-
    public function testRegisterClassesWithAsAlias(string $resource, array $expectedAliases)
    355+
    public function testRegisterClassesWithAsAlias(string $resource, array $expectedAliases, ?string $env = null)
    353356
    {
    354357
    $container = new ContainerBuilder();
    355-
    $loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
    358+
    $loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'), $env);
    356359
    $loader->registerClasses(
    357360
    (new Definition())->setAutoconfigured(true),
    358361
    'Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\\',
    @@ -374,6 +377,15 @@ public static function provideResourcesWithAsAliasAttributes(): iterable
    374377
    AliasBarInterface::class => new Alias(WithAsAliasIdMultipleInterface::class),
    375378
    AliasFooInterface::class => new Alias(WithAsAliasIdMultipleInterface::class),
    376379
    ]];
    380+
    yield 'Dev-env specific' => ['PrototypeAsAlias/WithAsAlias*Env.php', [
    381+
    AliasFooInterface::class => new Alias(WithAsAliasDevEnv::class),
    382+
    AliasBarInterface::class => new Alias(WithAsAliasBothEnv::class),
    383+
    ], 'dev'];
    384+
    yield 'Prod-env specific' => ['PrototypeAsAlias/WithAsAlias*Env.php', [
    385+
    AliasFooInterface::class => new Alias(WithAsAliasProdEnv::class),
    386+
    AliasBarInterface::class => new Alias(WithAsAliasBothEnv::class),
    387+
    ], 'prod'];
    388+
    yield 'Test-env specific' => ['PrototypeAsAlias/WithAsAlias*Env.php', [], 'test'];
    377389
    }
    378390

    379391
    /**

    0 commit comments

    Comments
     (0)
    0