-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Add #[Autoconfigure]
to help define autoconfiguration rules
#39804
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/Symfony/Component/DependencyInjection/Attribute/Autoconfigure.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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 how a base type should be autoconfigured. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)] | ||
class Autoconfigure | ||
{ | ||
public function __construct( | ||
public ?array $tags = null, | ||
public ?array $calls = null, | ||
public ?array $bind = null, | ||
public bool|string|null $lazy = null, | ||
public ?bool $public = null, | ||
public ?bool $shared = null, | ||
public ?bool $autowire = null, | ||
public ?array $properties = null, | ||
public array|string|null $configurator = null, | ||
) { | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Symfony/Component/DependencyInjection/Attribute/AutoconfigureTag.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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 how a base type should be tagged. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)] | ||
class AutoconfigureTag extends Autoconfigure | ||
{ | ||
public function __construct(string $name = null, array $attributes = []) | ||
{ | ||
parent::__construct( | ||
tags: [ | ||
[$name ?? 0 => $attributes], | ||
] | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/Symfony/Component/DependencyInjection/Compiler/RegisterAutoconfigureAttributesPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; | ||
|
||
/** | ||
* Reads #[Autoconfigure] attributes on definitions that are autoconfigured | ||
* and don't have the "container.ignore_attributes" tag. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
final class RegisterAutoconfigureAttributesPass implements CompilerPassInterface | ||
{ | ||
private $ignoreAttributesTag; | ||
private $registerForAutoconfiguration; | ||
|
||
public function __construct(string $ignoreAttributesTag = 'container.ignore_attributes') | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (80000 > \PHP_VERSION_ID) { | ||
return; | ||
} | ||
|
||
$this->ignoreAttributesTag = $ignoreAttributesTag; | ||
|
||
$parseDefinitions = new \ReflectionMethod(YamlFileLoader::class, 'parseDefinitions'); | ||
$parseDefinitions->setAccessible(true); | ||
$yamlLoader = $parseDefinitions->getDeclaringClass()->newInstanceWithoutConstructor(); | ||
|
||
$this->registerForAutoconfiguration = static function (ContainerBuilder $container, \ReflectionClass $class, \ReflectionAttribute $attribute) use ($parseDefinitions, $yamlLoader) { | ||
$attribute = (array) $attribute->newInstance(); | ||
|
||
foreach ($attribute['tags'] ?? [] as $i => $tag) { | ||
if (\is_array($tag) && [0] === array_keys($tag)) { | ||
$attribute['tags'][$i] = [$class->name => $tag[0]]; | ||
} | ||
} | ||
|
||
$parseDefinitions->invoke( | ||
$yamlLoader, | ||
[ | ||
'services' => [ | ||
'_instanceof' => [ | ||
$class->name => [$container->registerForAutoconfiguration($class->name)] + $attribute, | ||
], | ||
], | ||
], | ||
$class->getFileName() | ||
); | ||
}; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (80000 > \PHP_VERSION_ID) { | ||
return; | ||
} | ||
|
||
foreach ($container->getDefinitions() as $id => $definition) { | ||
if ($this->accept($definition) && null !== $class = $container->getReflectionClass($definition->getClass())) { | ||
$this->processClass($container, $class); | ||
} | ||
} | ||
} | ||
|
||
public function accept(Definition $definition): bool | ||
{ | ||
return 80000 <= \PHP_VERSION_ID && $definition->isAutoconfigured() && !$definition->hasTag($this->ignoreAttributesTag); | ||
} | ||
|
||
public function processClass(ContainerBuilder $container, \ReflectionClass $class) | ||
{ | ||
foreach ($class->getAttributes(Autoconfigure::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) { | ||
($this->registerForAutoconfiguration)($container, $class, $attribute); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
.../Component/DependencyInjection/Tests/Compiler/RegisterAutoconfigureAttributesPassTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Compiler; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\Argument\BoundArgument; | ||
use Symfony\Component\DependencyInjection\ChildDefinition; | ||
use Symfony\Component\DependencyInjection\Compiler\RegisterAutoconfigureAttributesPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutoconfigureAttributed; | ||
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutoconfiguredInterface; | ||
|
||
/** | ||
* @requires PHP 8 | ||
*/ | ||
class RegisterAutoconfigureAttributesPassTest extends TestCase | ||
{ | ||
public function testProcess() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->register('foo', AutoconfigureAttributed::class) | ||
->setAutoconfigured(true); | ||
|
||
(new RegisterAutoconfigureAttributesPass())->process($container); | ||
|
||
$argument = new BoundArgument(1, true, BoundArgument::INSTANCEOF_BINDING, realpath(__DIR__.'/../Fixtures/AutoconfigureAttributed.php')); | ||
$values = $argument->getValues(); | ||
--$values[1]; | ||
$argument->setValues($values); | ||
|
||
$expected = (new ChildDefinition('')) | ||
->setLazy(true) | ||
->setPublic(true) | ||
->setAutowired(true) | ||
->setShared(true) | ||
->setProperties(['bar' => 'baz']) | ||
->setConfigurator(new Reference('bla')) | ||
->addTag('a_tag') | ||
->addTag('another_tag', ['attr' => 234]) | ||
->addMethodCall('setBar', [2, 3]) | ||
->setBindings(['$bar' => $argument]) | ||
; | ||
$this->assertEquals([AutoconfigureAttributed::class => $expected], $container->getAutoconfiguredInstanceof()); | ||
} | ||
|
||
public function testIgnoreAttribute() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->register('foo', AutoconfigureAttributed::class) | ||
->addTag('container.ignore_attributes') | ||
->setAutoconfigured(true); | ||
|
||
(new RegisterAutoconfigureAttributesPass())->process($container); | ||
|
||
$this->assertSame([], $container->getAutoconfiguredInstanceof()); | ||
} | ||
|
||
public function testAutoconfiguredTag() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->register('foo', AutoconfiguredInterface::class) | ||
->setAutoconfigured(true); | ||
|
||
(new RegisterAutoconfigureAttributesPass())->process($container); | ||
|
||
$expected = (new ChildDefinition('')) | ||
->addTag(AutoconfiguredInterface::class, ['foo' => 123]) | ||
; | ||
$this->assertEquals([AutoconfiguredInterface::class => $expected], $container->getAutoconfiguredInstanceof()); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Symfony/Component/DependencyInjection/Tests/Fixtures/AutoconfigureAttributed.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; | ||
|
||
#[Autoconfigure( | ||
lazy: true, | ||
public: true, | ||
autowire: true, | ||
shared: true, | ||
properties: [ | ||
'bar' => 'baz', | ||
], | ||
configurator: '@bla', | ||
tags: [ | ||
'a_tag', | ||
['another_tag' => ['attr' => 234]], | ||
], | ||
calls: [ | ||
['setBar' => [2, 3]] | ||
], | ||
bind: [ | ||
'$bar' => 1, | ||
], | ||
)] | ||
class AutoconfigureAttributed | ||
{ | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Symfony/Component/DependencyInjection/Tests/Fixtures/AutoconfiguredInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag; | ||
|
||
#[AutoconfigureTag(attributes: ['foo' => 123])] | ||
interface AutoconfiguredInterface | ||
{ | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.