Skip to content

Commit 1b06b14

Browse files
committed
[DependencyInjection] Autoconfigurable attributes on methods
Based on the implementation of symfony#39897 I made it possible to autoconfigure method attributes.
1 parent a1c21c0 commit 1b06b14

File tree

6 files changed

+97
-1
lines changed

6 files changed

+97
-1
lines changed

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+
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

Lines 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

Lines 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

Lines 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;
@@ -36,6 +38,7 @@
3638
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService2;
3739
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService3;
3840
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService3Configurator;
41+
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService4;
3942
use Symfony\Contracts\Service\ServiceProviderInterface;
4043
use Symfony\Contracts\Service\ServiceSubscriberInterface;
4144

@@ -699,6 +702,39 @@ static function (Definition $definition, CustomAutoconfiguration $attribute) {
699702
], $collector->collectedTags);
700703
}
701704

705+
/**
706+
* @requires PHP 8
707+
*/
708+
public function testTagsViaAttributeOnMethods()
709+
{
710+
$container = new ContainerBuilder();
711+
$container->registerAttributeForAutoconfiguration(
712+
CustomMethodAttribute::class,
713+
static function (ChildDefinition $definition, CustomMethodAttribute $attribute, ReflectionMethod $reflector) {
714+
$tagAttributes = get_object_vars($attribute);
715+
$tagAttributes['method'] = $reflector->getName();
716+
717+
$definition->addTag('app.custom_tag', $tagAttributes);
718+
}
719+
);
720+
721+
$container->register(TaggedService4::class)
722+
->setPublic(true)
723+
->setAutoconfigured(true);
724+
725+
$collector = new TagCollector();
726+
$container->addCompilerPass($collector);
727+
728+
$container->compile();
729+
730+
self::assertSame([
731+
TaggedService4::class => [
732+
['someAttribute' => 'baz', 'priority' => 0, 'method' => 'fooAction'],
733+
['someAttribute' => 'foo', 'priority' => 0, 'method' => 'barAction'],
734+
],
735+
], $collector->collectedTags);
736+
}
737+
702738
/**
703739
* @requires PHP 8
704740
*/
Lines 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+
}
Lines 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy