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

Browse filesBrowse files
feature #47066 [DependencyInjection] Allow attribute autoconfiguration on static methods (alex-dev)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [DependencyInjection] Allow attribute autoconfiguration on static methods | Q | A | ------------ | --- | Branch? | 6.2 | Bug fix? | no | Tickets | #47060 | License | MIT A static method can be used to carry complex configuration payload for DI. A static method can be used to generate a Closure that can be hooked in the container (a static message handler with no dependency). Using simple attribute autoconfigurator to wire those case easily. Commits ------- 9908415 [DependencyInjection] Allow attribute autoconfiguration on static methods
2 parents 42c1e90 + 9908415 commit 7f37a0f
Copy full SHA for 7f37a0f

File tree

Expand file treeCollapse file tree

3 files changed

+46
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+46
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/AttributeAutoconfigurationPass.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
120120

121121
if ($this->methodAttributeConfigurators || $this->parameterAttributeConfigurators) {
122122
foreach ($classReflector->getMethods(\ReflectionMethod::IS_PUBLIC) as $methodReflector) {
123-
if ($methodReflector->isStatic() || $methodReflector->isConstructor() || $methodReflector->isDestructor()) {
123+
if ($methodReflector->isConstructor() || $methodReflector->isDestructor()) {
124124
continue;
125125
}
126126

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod;
4848
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithDefaultPriorityMethod;
4949
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithoutIndex;
50+
use Symfony\Component\DependencyInjection\Tests\Fixtures\StaticMethodTag;
5051
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedConsumerWithExclude;
5152
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService1;
5253
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService2;
@@ -1025,6 +1026,28 @@ static function (ChildDefinition $definition) {
10251026
self::assertTrue($service->hasBeenConfigured);
10261027
}
10271028

1029+
public function testAttributeAutoconfigurationOnStaticMethod()
1030+
{
1031+
$container = new ContainerBuilder();
1032+
$container->registerAttributeForAutoconfiguration(
1033+
CustomMethodAttribute::class,
1034+
static function (ChildDefinition $d, CustomMethodAttribute $a, \ReflectionMethod $_r) {
1035+
$d->addTag('custom_tag', ['attribute' => $a->someAttribute]);
1036+
}
1037+
);
1038+
1039+
$container->register('service', StaticMethodTag::class)
1040+
->setPublic(true)
1041+
->setAutoconfigured(true);
1042+
1043+
$container->compile();
1044+
1045+
$definition = $container->getDefinition('service');
1046+
self::assertEquals([['attribute' => 'static']], $definition->getTag('custom_tag'));
1047+
1048+
$container->get('service');
1049+
}
1050+
10281051
public function testTaggedIteratorAndLocatorWithExclude()
10291052
{
10301053
$container = new ContainerBuilder();
+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;
13+
14+
use Symfony\Component\DependencyInjection\Tests\Fixtures\Attribute\CustomMethodAttribute;
15+
16+
final class StaticMethodTag
17+
{
18+
#[CustomMethodAttribute('static')]
19+
public static function method(): void
20+
{
21+
}
22+
}

0 commit comments

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