Skip to content

[DependencyInjection] Autoconfigurable attributes #39897

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\EventDispatcher\Attribute\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Finder\Finder;
Expand Down Expand Up @@ -549,6 +550,10 @@ public function load(array $configs, ContainerBuilder $container)
$container->registerForAutoconfiguration(LoggerAwareInterface::class)
->addMethodCall('setLogger', [new Reference('logger')]);

$container->registerAttributeForAutoconfiguration(EventListener::class, static function (ChildDefinition $definition, EventListener $attribute): void {
$definition->addTag('kernel.event_listener', get_object_vars($attribute));
});

if (!$container->getParameter('kernel.debug')) {
// remove tagged iterator argument for resource checkers
$container->getDefinition('config_cache_factory')->setArguments([]);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Add `ServicesConfigurator::remove()` in the PHP-DSL
* Add `%env(not:...)%` processor to negate boolean values
* Add support for loading autoconfiguration rules via the `#[Autoconfigure]` and `#[AutoconfigureTag]` attributes on PHP 8
* Add autoconfigurable attributes

5.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @author Alexander M. Turek <me@derrabus.de>
*/
final class AttributeAutoconfigurationPass implements CompilerPassInterface
{
private $ignoreAttributesTag;

public function __construct(string $ignoreAttributesTag = 'container.ignore_attributes')
{
$this->ignoreAttributesTag = $ignoreAttributesTag;
}

public function process(ContainerBuilder $container): void
{
if (80000 > \PHP_VERSION_ID) {
return;
}

$autoconfiguredAttributes = $container->getAutoconfiguredAttributes();

foreach ($container->getDefinitions() as $id => $definition) {
if (!$definition->isAutoconfigured()
|| $definition->isAbstract()
|| $definition->hasTag($this->ignoreAttributesTag)
|| !($reflector = $container->getReflectionClass($definition->getClass(), false))
) {
continue;
}

$instanceof = $definition->getInstanceofConditionals();
$conditionals = $instanceof[$reflector->getName()] ?? new ChildDefinition('');
foreach ($reflector->getAttributes() as $attribute) {
if ($configurator = $autoconfiguredAttributes[$attribute->getName()] ?? null) {
$configurator($conditionals, $attribute->newInstance(), $reflector);
}
}
$instanceof[$reflector->getName()] = $conditionals;
$definition->setInstanceofConditionals($instanceof);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function __construct()
100 => [
new ResolveClassPass(),
new RegisterAutoconfigureAttributesPass(),
new AttributeAutoconfigurationPass(),
new ResolveInstanceofConditionalsPass(),
new RegisterEnvVarProcessorsPass(),
],
Expand Down
31 changes: 31 additions & 0 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ class ContainerBuilder extends Container implements TaggedContainerInterface

private $autoconfiguredInstanceof = [];

/**
* @var callable[]
*/
private $autoconfiguredAttributes = [];

private $removedIds = [];

private $removedBindingIds = [];
Expand Down Expand Up @@ -671,6 +676,14 @@ public function merge(self $container)

$this->autoconfiguredInstanceof[$interface] = $childDefinition;
}

foreach ($container->getAutoconfiguredAttributes() as $attribute => $configurator) {
if (isset($this->autoconfiguredAttributes[$attribute])) {
throw new InvalidArgumentException(sprintf('"%s" has already been autoconfigured and merge() does not support merging autoconfiguration for the same attribute.', $attribute));
}

$this->autoconfiguredAttributes[$attribute] = $configurator;
}
}

/**
Expand Down Expand Up @@ -1309,6 +1322,16 @@ public function registerForAutoconfiguration(string $interface)
return $this->autoconfiguredInstanceof[$interface];
}

/**
* Registers an attribute that will be used for autoconfiguring annotated classes.
*
* The configurator will receive a Definition instance and an instance of the attribute, in that order.
*/
public function registerAttributeForAutoconfiguration(string $attributeClass, callable $configurator): void
{
$this->autoconfiguredAttributes[$attributeClass] = $configurator;
}

/**
* Registers an autowiring alias that only binds to a specific argument name.
*
Expand Down Expand Up @@ -1338,6 +1361,14 @@ public function getAutoconfiguredInstanceof()
return $this->autoconfiguredInstanceof;
}

/**
* @return callable[]
*/
public function getAutoconfiguredAttributes(): array
{
return $this->autoconfiguredAttributes;
}

/**
* Resolves env parameter placeholders in a string or an array.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\DependencyInjection\Tests\Fixtures\Attribute\CustomAutoconfiguration;
use Symfony\Component\DependencyInjection\Tests\Fixtures\BarTagClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedForDefaultPriorityClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooTagClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService1;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService2;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService3;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService3Configurator;
use Symfony\Contracts\Service\ServiceProviderInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

Expand Down Expand Up @@ -506,6 +514,109 @@ public function testTaggedServiceLocatorWithDefaultIndex()
];
$this->assertSame($expected, ['baz' => $serviceLocator->get('baz')]);
}

/**
* @requires PHP 8
*/
public function testTagsViaAttribute()
{
$container = new ContainerBuilder();
$container->registerAttributeForAutoconfiguration(
CustomAutoconfiguration::class,
static function (ChildDefinition $definition, CustomAutoconfiguration $attribute, \ReflectionClass $reflector) {
$definition->addTag('app.custom_tag', get_object_vars($attribute) + ['class' => $reflector->getName()]);
}
);

$container->register('one', TaggedService1::class)
->setPublic(true)
->setAutoconfigured(true);
$container->register('two', TaggedService2::class)
->addTag('app.custom_tag', ['info' => 'This tag is not autoconfigured'])
->setPublic(true)
->setAutoconfigured(true);

$collector = new TagCollector();
$container->addCompilerPass($collector);

$container->compile();

self::assertSame([
'one' => [
['someAttribute' => 'one', 'priority' => 0, 'class' => TaggedService1::class],
['someAttribute' => 'two', 'priority' => 0, 'class' => TaggedService1::class],
],
'two' => [
['info' => 'This tag is not autoconfigured'],
['someAttribute' => 'prio 100', 'priority' => 100, 'class' => TaggedService2::class],
],
], $collector->collectedTags);
}

/**
* @requires PHP 8
*/
public function testAttributesAreIgnored()
{
$container = new ContainerBuilder();
$container->registerAttributeForAutoconfiguration(
CustomAutoconfiguration::class,
static function (Definition $definition, CustomAutoconfiguration $attribute) {
$definition->addTag('app.custom_tag', get_object_vars($attribute));
}
);

$container->register('one', TaggedService1::class)
->setPublic(true)
->addTag('container.ignore_attributes')
->setAutoconfigured(true);
$container->register('two', TaggedService2::class)
->setPublic(true)
->setAutoconfigured(true);

$collector = new TagCollector();
$container->addCompilerPass($collector);

$container->compile();

self::assertSame([
'two' => [
['someAttribute' => 'prio 100', 'priority' => 100],
],
], $collector->collectedTags);
}

/**
* @requires PHP 8
*/
public function testAutoconfigureViaAttribute()
{
$container = new ContainerBuilder();
$container->registerAttributeForAutoconfiguration(
CustomAutoconfiguration::class,
static function (ChildDefinition $definition) {
$definition
->addMethodCall('doSomething', [1, 2, 3])
->setBindings(['string $foo' => 'bar'])
->setConfigurator(new Reference('my_configurator'))
;
}
);

$container->register('my_configurator', TaggedService3Configurator::class);
$container->register('three', TaggedService3::class)
->setPublic(true)
->setAutoconfigured(true);

$container->compile();

/** @var TaggedService3 $service */
$service = $container->get('three');

self::assertSame('bar', $service->foo);
self::assertSame(6, $service->sum);
self::assertTrue($service->hasBeenConfigured);
}
}

class ServiceSubscriberStub implements ServiceSubscriberInterface
Expand Down Expand Up @@ -566,3 +677,13 @@ public function setSunshine($type)
{
}
}

final class TagCollector implements CompilerPassInterface
{
public $collectedTags;

public function process(ContainerBuilder $container): void
{
$this->collectedTags = $container->findTaggedServiceIds('app.custom_tag');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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\Fixtures\Attribute;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class CustomAutoconfiguration
{
public function __construct(
public string $someAttribute,
public int $priority = 0,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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\Fixtures;

use Symfony\Component\DependencyInjection\Tests\Fixtures\Attribute\CustomAutoconfiguration;

#[CustomAutoconfiguration(someAttribute: 'one')]
#[CustomAutoconfiguration(someAttribute: 'two')]
final class TaggedService1
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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\Fixtures;

use Symfony\Component\DependencyInjection\Tests\Fixtures\Attribute\CustomAutoconfiguration;

#[CustomAutoconfiguration(someAttribute: 'prio 100', priority: 100)]
final class TaggedService2
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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\Fixtures;

use Symfony\Component\DependencyInjection\Tests\Fixtures\Attribute\CustomAutoconfiguration;

#[CustomAutoconfiguration(someAttribute: 'three')]
final class TaggedService3
{
public int $sum = 0;
public bool $hasBeenConfigured = false;

public function __construct(
public string $foo,
) {
}

public function doSomething(int $a, int $b, int $c): void
{
$this->sum = $a + $b + $c;
}
}
Loading