-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Add AsAlias attribute #49411
New issue
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Attribute; | ||
|
||
/** | ||
* An attribute to tell under which alias a service should be registered or to use the implemented interface if no parameter is given. | ||
* | ||
* @author Alan Poulain <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)] | ||
final class AsAlias | ||
{ | ||
public function __construct( | ||
public ?string $id = null, | ||
public bool $public = false, | ||
) { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias; | ||
|
||
interface AliasBarInterface | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias; | ||
|
||
interface AliasFooInterface | ||
{ | ||
} |
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)] | ||
class WithAsAlias | ||
{ | ||
} |
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)] | ||
class WithAsAliasDuplicate | ||
{ | ||
} |
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)] | ||
class WithAsAliasIdMultipleInterface implements AliasFooInterface, AliasBarInterface | ||
{ | ||
} |
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] | ||
class WithAsAliasInterface implements AliasFooInterface | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\AsAlias; | ||
|
||
#[AsAlias(id: AliasFooInterface::class, public: true)] | ||
#[AsAlias(id: 'some-alias')] | ||
class WithAsAliasMultiple | ||
{ | ||
} |
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] | ||
class WithAsAliasMultipleInterface implements AliasFooInterface, AliasBarInterface | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,10 +14,12 @@ | |
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\Config\Loader\LoaderResolver; | ||
use Symfony\Component\DependencyInjection\Alias; | ||
use Symfony\Component\DependencyInjection\ChildDefinition; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
use Symfony\Component\DependencyInjection\Loader\FileLoader; | ||
use Symfony\Component\DependencyInjection\Loader\IniFileLoader; | ||
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; | ||
|
@@ -32,6 +34,12 @@ | |
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\OtherDir\Baz; | ||
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\Bar; | ||
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\BarInterface; | ||
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\WithAsAliasIdMultipleInterface; | ||
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasInterface; | ||
use Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasMultiple; | ||
|
||
class FileLoaderTest extends TestCase | ||
{ | ||
|
@@ -274,6 +282,73 @@ public function testRegisterClassesWithWhenEnv(?string $env, bool $expected) | |
|
||
$this->assertSame($expected, $container->has(Foo::class)); | ||
} | ||
|
||
/** | ||
* @dataProvider provideResourcesWithAsAliasAttributes | ||
*/ | ||
public function testRegisterClassesWithAsAlias(string $resource, array $expectedAliases) | ||
{ | ||
$container = new ContainerBuilder(); | ||
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures')); | ||
$loader->registerClasses( | ||
(new Definition())->setAutoconfigured(true), | ||
'Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\\', | ||
$resource | ||
); | ||
|
||
$this->assertEquals($expectedAliases, $container->getAliases()); | ||
} | ||
|
||
public static function provideResourcesWithAsAliasAttributes(): iterable | ||
{ | ||
yield 'Private' => ['PrototypeAsAlias/{WithAsAlias,AliasFooInterface}.php', [AliasFooInterface::class => new Alias(WithAsAlias::class)]]; | ||
yield 'Interface' => ['PrototypeAsAlias/{WithAsAliasInterface,AliasFooInterface}.php', [AliasFooInterface::class => new Alias(WithAsAliasInterface::class)]]; | ||
yield 'Multiple' => ['PrototypeAsAlias/{WithAsAliasMultiple,AliasFooInterface}.php', [ | ||
AliasFooInterface::class => new Alias(WithAsAliasMultiple::class, true), | ||
'some-alias' => new Alias(WithAsAliasMultiple::class), | ||
]]; | ||
yield 'Multiple with id' => ['PrototypeAsAlias/{WithAsAliasIdMultipleInterface,AliasBarInterface,AliasFooInterface}.php', [ | ||
AliasBarInterface::class => new Alias(WithAsAliasIdMultipleInterface::class), | ||
AliasFooInterface::class => new Alias(WithAsAliasIdMultipleInterface::class), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. I think it's the right behavior, don't you think so? The other possibility would be to prevent the singly implemented interface registration if there is an |
||
]]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideResourcesWithDuplicatedAsAliasAttributes | ||
*/ | ||
public function testRegisterClassesWithDuplicatedAsAlias(string $resource, string $expectedExceptionMessage) | ||
{ | ||
$this->expectException(LogicException::class); | ||
$this->expectExceptionMessage($expectedExceptionMessage); | ||
|
||
$container = new ContainerBuilder(); | ||
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures')); | ||
$loader->registerClasses( | ||
(new Definition())->setAutoconfigured(true), | ||
'Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\\', | ||
$resource | ||
); | ||
} | ||
|
||
public static function provideResourcesWithDuplicatedAsAliasAttributes(): iterable | ||
{ | ||
yield 'Duplicated' => ['PrototypeAsAlias/{WithAsAlias,WithAsAliasDuplicate,AliasFooInterface}.php', 'The "Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\AliasFooInterface" alias has already been defined with the #[AsAlias] attribute in "Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAlias".']; | ||
yield 'Interface duplicated' => ['PrototypeAsAlias/{WithAsAliasInterface,WithAsAlias,AliasFooInterface}.php', 'The "Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\AliasFooInterface" alias has already been defined with the #[AsAlias] attribute in "Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAlias".']; | ||
} | ||
|
||
public function testRegisterClassesWithAsAliasAndImplementingMultipleInterfaces() | ||
{ | ||
$this->expectException(LogicException::class); | ||
$this->expectExceptionMessage('Alias cannot be automatically determined for class "Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\WithAsAliasMultipleInterface". 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].'); | ||
|
||
$container = new ContainerBuilder(); | ||
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures')); | ||
$loader->registerClasses( | ||
(new Definition())->setAutoconfigured(true), | ||
'Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\\', | ||
'PrototypeAsAlias/{WithAsAliasMultipleInterface,AliasBarInterface,AliasFooInterface}.php' | ||
); | ||
} | ||
} | ||
|
||
class TestFileLoader extends FileLoader | ||
|
Uh oh!
There was an error while loading. Please reload this page.