Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 08e4c09

Browse files
feature #60819 [DependencyInjection] Allow extending #[AsAlias] attribute (ruudk)
This PR was squashed before being merged into the 7.4 branch. Discussion ---------- [DependencyInjection] Allow extending `#[AsAlias]` attribute | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | | License | MIT This allows people to extend the `#[AsAlias]` attribute with something like this: ```php namespace App\Symfony; use Symfony\Component\DependencyInjection\Attribute\AsAlias; final class AsProductionAlias extends AsAlias { /** * `@param` string|null $id The id of the alias * `@param` bool $public Whether to declare the alias public */ public function __construct( public ?string $id = null, public bool $public = false, ) { parent::__construct($id, $public, ['prod']); } } ``` This is similar to a previous change I did for the `#[When]` attribute, that was accepted: - #47196 Commits ------- 90fd3ff [DependencyInjection] Allow extending `#[AsAlias]` attribute
2 parents 371b671 + 90fd3ff commit 08e4c09

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @author Alan Poulain <[email protected]>
1818
*/
1919
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
20-
final class AsAlias
20+
class AsAlias
2121
{
2222
/**
2323
* @var list<string>

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.4
5+
---
6+
7+
* Allow `#[AsAlias]` to be extended
8+
49
7.3
510
---
611

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public function registerClasses(Definition $prototype, string $namespace, string
216216
}
217217
$r = $this->container->getReflectionClass($class);
218218
$defaultAlias = 1 === \count($interfaces) ? $interfaces[0] : null;
219-
foreach ($r->getAttributes(AsAlias::class) as $attr) {
219+
foreach ($r->getAttributes(AsAlias::class, \ReflectionAttribute::IS_INSTANCEOF) as $attr) {
220220
/** @var AsAlias $attribute */
221221
$attribute = $attr->newInstance();
222222
$alias = $attribute->id ?? $defaultAlias;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias;
4+
5+
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
6+
7+
#[AsProductionAlias(id: AliasFooInterface::class)]
8+
class WithCustomAsAlias
9+
{
10+
}
11+
12+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
13+
final class AsProductionAlias extends AsAlias
14+
{
15+
/**
16+
* @param string|null $id The id of the alias
17+
* @param bool $public Whether to declare the alias public
18+
*/
19+
public function __construct(
20+
public ?string $id = null,
21+
public bool $public = false,
22+
) {
23+
parent::__construct($id, $public, ['prod']);
24+
}
25+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasInterface;
4646
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasMultiple;
4747
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasProdEnv;
48+
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithCustomAsAlias;
4849
use Symfony\Component\DependencyInjection\Tests\Fixtures\Utils\NotAService;
4950

5051
class FileLoaderTest extends TestCase
@@ -368,6 +369,8 @@ public function testRegisterClassesWithAsAlias(string $resource, array $expected
368369
public static function provideResourcesWithAsAliasAttributes(): iterable
369370
{
370371
yield 'Private' => ['PrototypeAsAlias/{WithAsAlias,AliasFooInterface}.php', [AliasFooInterface::class => new Alias(WithAsAlias::class)]];
372+
yield 'PrivateCustomAlias' => ['PrototypeAsAlias/{WithCustomAsAlias,AliasFooInterface}.php', [AliasFooInterface::class => new Alias(WithCustomAsAlias::class)], 'prod'];
373+
yield 'PrivateCustomAliasNoMatch' => ['PrototypeAsAlias/{WithCustomAsAlias,AliasFooInterface}.php', [], 'dev'];
371374
yield 'Interface' => ['PrototypeAsAlias/{WithAsAliasInterface,AliasFooInterface}.php', [AliasFooInterface::class => new Alias(WithAsAliasInterface::class)]];
372375
yield 'Multiple' => ['PrototypeAsAlias/{WithAsAliasMultiple,AliasFooInterface}.php', [
373376
AliasFooInterface::class => new Alias(WithAsAliasMultiple::class, true),

0 commit comments

Comments
 (0)