Skip to content

Commit bcea4cb

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

File tree

6 files changed

+98
-1
lines changed

6 files changed

+98
-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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use ReflectionMethod;
16+
use Reflector;
1517
use Symfony\Component\Config\FileLocator;
1618
use Symfony\Component\DependencyInjection\Alias;
1719
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
@@ -24,6 +26,7 @@
2426
use Symfony\Component\DependencyInjection\Reference;
2527
use Symfony\Component\DependencyInjection\ServiceLocator;
2628
use Symfony\Component\DependencyInjection\Tests\Fixtures\Attribute\CustomAutoconfiguration;
29+
use Symfony\Component\DependencyInjection\Tests\Fixtures\Attribute\CustomMethodAttribute;
2730
use Symfony\Component\DependencyInjection\Tests\Fixtures\BarTagClass;
2831
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedClass;
2932
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedForDefaultPriorityClass;
@@ -36,6 +39,7 @@
3639
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService2;
3740
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService3;
3841
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService3Configurator;
42+
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService4;
3943
use Symfony\Contracts\Service\ServiceProviderInterface;
4044
use Symfony\Contracts\Service\ServiceSubscriberInterface;
4145

@@ -699,6 +703,39 @@ static function (Definition $definition, CustomAutoconfiguration $attribute) {
699703
], $collector->collectedTags);
700704
}
701705

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