8000 [DependencyInjection] Add "when" argument to #[AsAlias] · symfony/symfony@32ab3b0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 32ab3b0

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

File tree

6 files changed

+542
-420
lines changed

6 files changed

+542
-420
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ final class AsAlias
2222
/**
2323
* @param string|null $id The id of the alias
2424
* @param bool $public Whether to declare the alias public
25+
* @param string|null $when The environment under which the class will be registered as a service (i.e. "dev", "test", "prod")
2526
*/
2627
public function __construct(
2728
public ?string $id = null,
2829
public bool $public = false,
30+
public ?string $when = null,
2931
) {
3032
}
3133
}

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 503 additions & 416 deletions
Large diffs are not rendered by default.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,10 @@ public function registerClasses(Definition $prototype, string $namespace, string
227227
if (isset($this->aliases[$alias])) {
228228
throw new LogicException(\sprintf('The "%s" alias has already been defined with the #[AsAlias] attribute in "%s".', $alias, $this->aliases[$alias]));
229229
}
230-
$this->aliases[$alias] = new Alias($class, $public);
230+
231+
if (null === $attribute->when || $this->env === $attribute->when) {
232+
$this->aliases[$alias] = new Alias($class, $public);
233+
}
231234
}
232235
}
233236

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 WithAsAliasDevEnv
9+
{
10+
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@
3939
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\AliasBarInterface;
4040
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\AliasFooInterface;
4141
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAlias;
42+
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasDevEnv;
4243
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasIdMultipleInterface;
4344
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasInterface;
4445
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasMultiple;
46+
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasProdEnv;
4547
use Symfony\Component\DependencyInjection\Tests\Fixtures\Utils\NotAService;
4648

4749
class FileLoaderTest extends TestCase
@@ -174,7 +176,8 @@ public function testRegisterClassesWithExcludeAsArray()
174176
$loader->registerClasses(
175177
new Definition(),
176178
'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\',
177-
'Prototype/*', [
179+
'Prototype/*',
180+
[
178181
'Prototype/%sub_dir%',
179182
'Prototype/OtherDir/AnotherSub/DeeperBaz.php',
180183
]
@@ -348,11 +351,12 @@ public function testRegisterThrowsWithBothWhenAndNotWhenAttribute()
348351

349352
/**
350353
* @dataProvider provideResourcesWithAsAliasAttributes
354+
* @group debug
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,12 @@ 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/{WithAsAliasDevEnv, WithAsAliasProdEnv}.php', [
382+
AliasFooInterface::class => new Alias(WithAsAliasDevEnv::class),
383+
], 'dev'];
384+
yield 'Prod-env specific' => ['PrototypeAsAlias/{WithAsAliasDevEnv, WithAsAliasProdEnv}.php', [
385+
AliasFooInterface::class => new Alias(WithAsAliasProdEnv::class),
386+
], 'prod'];
377387
}
378388

379389
/**

0 commit comments

Comments
 (0)
0