8000 [DependencyInjection] Add "when" argument to #[AsAlias] by Zuruuh · Pull Request #60186 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Add "when" argument to #[AsAlias] #60186

New issue
10000

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/Symfony/Component/DependencyInjection/Attribute/AsAlias.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>
*/
public array $when = [];

/**
* @param string|null $id The id of the alias
* @param bool $public Whether to declare the alias public
* @param string|list<string> $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;
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
10000
10 changes: 7 additions & 3 deletions src/Symfony/Component/DependencyInjection/Loader/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias;

use Symfony\Component\DependencyInjection\Attribute\AsAlias;

#[AsAlias(id: AliasBarInterface::class, when: ['dev', 'prod'])]
class WithAsAliasBothEnv
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias;

use Symfony\Component\DependencyInjection\Attribute\AsAlias;

#[AsAlias(id: AliasFooInterface::class, when: 'dev')]
class WithAsAliasDevEnv
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias;

use Symfony\Component\DependencyInjection\Attribute\AsAlias;

#[AsAlias(id: AliasFooInterface::class, when: 'prod')]
class WithAsAliasProdEnv
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasBothEnv;
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses\MissingParent;
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo;
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\FooInterface;
Expand All @@ -39,9 +40,11 @@
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\AliasBarInterface;
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\AliasFooInterface;
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAlias;
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasDevEnv;
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasIdMultipleInterface;
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasInterface;
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasMultiple;
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasProdEnv;
use Symfony\Component\DependencyInjection\Tests\Fixtures\Utils\NotAService;

class FileLoaderTest extends TestCase
Expand Down Expand Up @@ -349,10 +352,10 @@ public function testRegisterThrowsWithBothWhenAndNotWhenAttribute()
/**
* @dataProvider provideResourcesWithAsAliasAttributes
*/
public function testRegisterClassesWithAsAlias(string $resource, array $expectedAliases)
public function testRegisterClassesWithAsAlias(string $resource, array $expectedAliases, ?string $env = null)
{
$container = new ContainerBuilder();
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'), $env);
$loader->registerClasses(
(new Definition())->setAutoconfigured(true),
'Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\\',
Expand All @@ -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'];
}

/**
Expand Down
Loading
0