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

Appearance settings

Commit 5cc150e

Browse filesBrowse files
committed
[DependencyInjection] Autoconfigurable attributes on methods
Based on the implementation of symfony#39897 I made it possible to autoconfigure method attributes.
1 parent d4c6592 commit 5cc150e
Copy full SHA for 5cc150e

File tree

6 files changed

+97
-1
lines changed
Filter options

6 files changed

+97
-1
lines changed

‎src/Symfony/Component/DependencyInjection/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.4
5+
---
6+
7+
* Add support for autoconfigurable attributes on methods
8+
49
5.3
510
---
611

‎src/Symfony/Component/DependencyInjection/Compiler/AttributeAutoconfigurationPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/AttributeAutoconfigurationPass.php
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ protected function processValue($value, bool $isRoot = false)
4848
$configurator($conditionals, $attribute->newInstance(), $reflector);
4949
}
5050
}
51+
foreach ($reflector->getMethods() as $method) {
52+
foreach ($method->getAttributes() as $attribute) {
53+
if ($configurator = $autoconfiguredAttributes[$attribute->getName()] ?? null) {
54+
$configurator($conditionals, $attribute->newInstance(), $method);
55+
}
56+
}
57+
}
5158
if (!isset($instanceof[$reflector->getName()]) && new ChildDefinition('') != $conditionals) {
5259
$instanceof[$reflector->getName()] = $conditionals;
5360
$value->setInstanceofConditionals($instanceof);

‎src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/ContainerBuilder.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,7 @@ public function registerForAutoconfiguration(string $interface)
13091309
/**
13101310
* Registers an attribute that will be used for autoconfiguring annotated classes.
13111311
*
1312-
* The configurator will receive a ChildDefinition instance, an instance of the attribute and the corresponding \ReflectionClass, in that order.
1312+
* The configurator will receive a ChildDefinition instance, an instance of the attribute and the corresponding \ReflectionClass or \ReflectionMethod, in that order.
13131313
*/
13141314
public function registerAttributeForAutoconfiguration(string $attributeClass, callable $configurator): void
13151315
{

‎src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use ReflectionMethod;
1516
use Symfony\Component\Config\FileLocator;
1617
use Symfony\Component\DependencyInjection\Alias;
1718
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
@@ -24,6 +25,7 @@
2425
use Symfony\Component\DependencyInjection\Reference;
2526
use Symfony\Component\DependencyInjection\ServiceLocator;
2627
use Symfony\Component\DependencyInjection\Tests\Fixtures\Attribute\CustomAutoconfiguration;
28+
use Symfony\Component\DependencyInjection\Tests\Fixtures\Attribute\CustomMethodAttribute;
2729
use Symfony\Component\DependencyInjection\Tests\Fixtures\BarTagClass;
2830
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedClass;
2931
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedForDefaultPriorityClass;
@@ -37,6 +39,7 @@
3739
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService2;
3840
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService3;
3941
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService3Configurator;
42+
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService4;
4043
use Symfony\Contracts\Service\ServiceProviderInterface;
4144
use Symfony\Contracts\Service\ServiceSubscriberInterface;
4245

@@ -729,6 +732,39 @@ static function (Definition $definition, CustomAutoconfiguration $attribute) {
729732
], $collector->collectedTags);
730733
}
731734

735+
/**
736+
* @requires PHP 8
737+
*/
738+
public function testTagsViaAttributeOnMethods()
739+
{
740+
$container = new ContainerBuilder();
741+
$container->registerAttributeForAutoconfiguration(
742+
CustomMethodAttribute::class,
743+
static function (ChildDefinition $definition, CustomMethodAttribute $attribute, ReflectionMethod $reflector) {
744+
$tagAttributes = get_object_vars($attribute);
745+
$tagAttributes['method'] = $reflector->getName();
746+
747+
$definition->addTag('app.custom_tag', $tagAttributes);
748+
}
749+
);
750+
751+
$container->register(TaggedService4::class)
752+
->setPublic(true)
753+
->setAutoconfigured(true);
754+
755+
$collector = new TagCollector();
756+
$container->addCompilerPass($collector);
757+
758+
$container->compile();
759+
760+
self::assertSame([
761+
TaggedService4::class => [
762+
['someAttribute' => 'baz', 'priority' => 0, 'method' => 'fooAction'],
763+
['someAttribute' => 'foo', 'priority' => 0, 'method' => 'barAction'],
764+
],
765+
], $collector->collectedTags);
766+
}
767+
732768
/**
733769
* @requires PHP 8
734770
*/
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Attribute;
13+
14+
#[\Attribute(\Attribute::TARGET_METHOD)]
15+
final class CustomMethodAttribute
16+
{
17+
public function __construct(
18+
public string $someAttribute,
19+
public int $priority = 0,
20+
) {
21+
}
22+
}
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
13+
14+
use Symfony\Component\DependencyInjection\Tests\Fixtures\Attribute\CustomAutoconfiguration;
15+
use Symfony\Component\DependencyInjection\Tests\Fixtures\Attribute\CustomMethodAttribute;
16+
17+
final class TaggedService4
18+
{
19+
#[CustomMethodAttribute(someAttribute: 'baz')]
20+
public function fooAction() {}
21+
22+
#[CustomMethodAttribute(someAttribute: 'foo')]
23+
public function barAction() {}
24+
25+
public function someOtherMethod() {}
26+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.