Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd5b5581

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 | MITAlso 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 parentsf2357a0 +187524d commitd5b5581

File tree

7 files changed

+62
-7
lines changed

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
finalclass 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+
publicarray$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
publicfunction__construct(
2733
public ?string$id =null,
2834
publicbool$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
thrownewLogicException(\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-
thrownewLogicException(\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+
thrownewLogicException(\sprintf('The "%s" alias has already been defined with the #[AsAlias] attribute in "%s".',$alias,$this->aliases[$alias]));
231+
}
232+
233+
$this->aliases[$alias] =newAlias($class,$public);
229234
}
230-
$this->aliases[$alias] =newAlias($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+
namespaceSymfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias;
4+
5+
useSymfony\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+
namespaceSymfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias;
4+
5+
useSymfony\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+
namespaceSymfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias;
4+
5+
useSymfony\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
useSymfony\Component\DependencyInjection\Loader\XmlFileLoader;
2727
useSymfony\Component\DependencyInjection\Loader\YamlFileLoader;
2828
useSymfony\Component\DependencyInjection\Reference;
29+
useSymfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasBothEnv;
2930
useSymfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses\MissingParent;
3031
useSymfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo;
3132
useSymfony\Component\DependencyInjection\Tests\Fixtures\Prototype\FooInterface;
@@ -39,9 +40,11 @@
3940
useSymfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\AliasBarInterface;
4041
useSymfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\AliasFooInterface;
4142
useSymfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAlias;
43+
useSymfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasDevEnv;
4244
useSymfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasIdMultipleInterface;
4345
useSymfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasInterface;
4446
useSymfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasMultiple;
47+
useSymfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasProdEnv;
4548
useSymfony\Component\DependencyInjection\Tests\Fixtures\Utils\NotAService;
4649

4750
class FileLoaderTestextends TestCase
@@ -349,10 +352,10 @@ public function testRegisterThrowsWithBothWhenAndNotWhenAttribute()
349352
/**
350353
* @dataProvider provideResourcesWithAsAliasAttributes
351354
*/
352-
publicfunctiontestRegisterClassesWithAsAlias(string$resource,array$expectedAliases)
355+
publicfunctiontestRegisterClassesWithAsAlias(string$resource,array$expectedAliases, ?string$env =null)
353356
{
354357
$container =newContainerBuilder();
355-
$loader =newTestFileLoader($container,newFileLocator(self::$fixturesPath.'/Fixtures'));
358+
$loader =newTestFileLoader($container,newFileLocator(self::$fixturesPath.'/Fixtures'),$env);
356359
$loader->registerClasses(
357360
(newDefinition())->setAutoconfigured(true),
358361
'Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\\',
@@ -374,6 +377,15 @@ public static function provideResourcesWithAsAliasAttributes(): iterable
374377
AliasBarInterface::class =>newAlias(WithAsAliasIdMultipleInterface::class),
375378
AliasFooInterface::class =>newAlias(WithAsAliasIdMultipleInterface::class),
376379
]];
380+
yield'Dev-env specific' => ['PrototypeAsAlias/WithAsAlias*Env.php', [
381+
AliasFooInterface::class =>newAlias(WithAsAliasDevEnv::class),
382+
AliasBarInterface::class =>newAlias(WithAsAliasBothEnv::class),
383+
],'dev'];
384+
yield'Prod-env specific' => ['PrototypeAsAlias/WithAsAlias*Env.php', [
385+
AliasFooInterface::class =>newAlias(WithAsAliasProdEnv::class),
386+
AliasBarInterface::class =>newAlias(WithAsAliasBothEnv::class),
387+
],'prod'];
388+
yield'Test-env specific' => ['PrototypeAsAlias/WithAsAlias*Env.php', [],'test'];
377389
}
378390

379391
/**

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp