Skip to content

Commit 4693727

Browse files
bug symfony#44385 [DependencyInjection] Skip parameter attribute configurators in AttributeAutoconfigurationPass if we can't get the constructor reflector (fancyweb)
This PR was merged into the 5.4 branch. Discussion ---------- [DependencyInjection] Skip parameter attribute configurators in AttributeAutoconfigurationPass if we can't get the constructor reflector | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | symfony#44342 | License | MIT | Doc PR | - `$required = false` on `getConstructor()` doesn't mean it doesn't throw. I have looked at our other usages, and we catch the exception when we want to ignore it. I'd still like to return null when the definition class is null though (on 4.4). Commits ------- b2fe0e0 [DependencyInjection] Skip parameter attribute configurators in AttributeAutoconfigurationPass if we can't get the constructor reflector
2 parents 9811dca + b2fe0e0 commit 4693727

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Definition;
1717
use Symfony\Component\DependencyInjection\Exception\LogicException;
18+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1819

1920
/**
2021
* @author Alexander M. Turek <[email protected]>
@@ -99,11 +100,19 @@ protected function processValue($value, bool $isRoot = false)
99100
}
100101
}
101102

102-
if ($this->parameterAttributeConfigurators && $constructorReflector = $this->getConstructor($value, false)) {
103-
foreach ($constructorReflector->getParameters() as $parameterReflector) {
104-
foreach ($parameterReflector->getAttributes() as $attribute) {
105-
if ($configurator = $this->parameterAttributeConfigurators[$attribute->getName()] ?? null) {
106-
$configurator($conditionals, $attribute->newInstance(), $parameterReflector);
103+
if ($this->parameterAttributeConfigurators) {
104+
try {
105+
$constructorReflector = $this->getConstructor($value, false);
106+
} catch (RuntimeException $e) {
107+
$constructorReflector = null;
108+
}
109+
110+
if ($constructorReflector) {
111+
foreach ($constructorReflector->getParameters() as $parameterReflector) {
112+
foreach ($parameterReflector->getAttributes() as $attribute) {
113+
if ($configurator = $this->parameterAttributeConfigurators[$attribute->getName()] ?? null) {
114+
$configurator($conditionals, $attribute->newInstance(), $parameterReflector);
115+
}
107116
}
108117
}
109118
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,11 @@ static function (ChildDefinition $definition, CustomParameterAttribute $attribut
976976
->setPublic(true)
977977
->setAutoconfigured(true);
978978

979+
$container->register('failing_factory', \stdClass::class);
980+
$container->register('ccc', TaggedService4::class)
981+
->setFactory([new Reference('failing_factory'), 'create'])
982+
->setAutoconfigured(true);
983+
979984
$collector = new TagCollector();
980985
$container->addCompilerPass($collector);
981986

@@ -996,6 +1001,17 @@ static function (ChildDefinition $definition, CustomParameterAttribute $attribut
9961001
['property' => 'name'],
9971002
['someAttribute' => 'on name', 'priority' => 0, 'property' => 'name'],
9981003
],
1004+
'ccc' => [
1005+
['class' => TaggedService4::class],
1006+
['method' => 'fooAction'],
1007+
['someAttribute' => 'on fooAction', 'priority' => 0, 'method' => 'fooAction'],
1008+
['parameter' => 'param1'],
1009+
['someAttribute' => 'on param1 in fooAction', 'priority' => 0, 'parameter' => 'param1'],
1010+
['method' => 'barAction'],
1011+
['someAttribute' => 'on barAction', 'priority' => 0, 'method' => 'barAction'],
1012+
['property' => 'name'],
1013+
['someAttribute' => 'on name', 'priority' => 0, 'property' => 'name'],
1014+
],
9991015
], $collector->collectedTags);
10001016
}
10011017

0 commit comments

Comments
 (0)