8000 bug #50022 [DependencyInjection] Fallback to default value when autow… · symfony/symfony@20ee89c · GitHub
[go: up one dir, main page]

Skip to content

Commit 20ee89c

Browse files
bug #50022 [DependencyInjection] Fallback to default value when autowiring undefined parameters for optional arguments (radar3301)
This PR was merged into the 6.2 branch. Discussion ---------- [DependencyInjection] Fallback to default value when autowiring undefined parameters for optional arguments | Q | A | ------------- | --- | Branch? | 6.2 / 6.3 | Bug fix? | yes | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #50020 <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT | Related | PR #50023 Allows autowiring of undefined nullable parameters, similar to existing behavior for non-parameter values. See related issue for more detail. Commits ------- 2d9bc8c [DependencyInjection] Fallback to default value when autowiring undefined parameters for optional arguments
2 parents 9f451ed + 2d9bc8c commit 20ee89c

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\DependencyInjection\ContainerInterface;
2424
use Symfony\Component\DependencyInjection\Definition;
2525
use Symfony\Component\DependencyInjection\Exception\AutowiringFailedException;
26+
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
2627
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2728
use Symfony\Component\DependencyInjection\Reference;
2829
use Symfony\Component\DependencyInjection\TypedReference;
@@ -279,9 +280,16 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
279280
if ($checkAttributes) {
280281
foreach ($parameter->getAttributes() as $attribute) {
281282
if (\in_array($attribute->getName(), [TaggedIterator::class, TaggedLocator::class, Autowire::class, MapDecorated::class], true)) {
282-
$arguments[$index] = $this->processAttribute($attribute->newInstance(), $parameter->allowsNull());
283-
284-
continue 2;
283+
try {
284+
$arguments[$index] = $this->processAttribute($attribute->newInstance(), $parameter->allowsNull());
285+
continue 2;
286+
} catch (ParameterNotFoundException $e) {
287+
if (!$parameter->isDefaultValueAvailable()) {
288+
throw new AutowiringFailedException($this->currentId, $e->getMessage(), 0, $e);
289+
}
290+
$arguments[$index] = clone $this->defaultArgument;
291+
$arguments[$index]->value = $parameter->getDefaultValue();
292+
}
285293
}
286294
}
287295
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,38 @@ public function testAutowireAttribute()
12171217
$this->assertNull($service->invalid);
12181218
}
12191219

1220+
public function testAutowireAttributeNullFallbackTestRequired()
1221+
{
1222+
$container = new ContainerBuilder();
1223+
1224+
$container->register('foo', AutowireAttributeNullFallback::class)
1225+
->setAutowired(true)
1226+
->setPublic(true)
1227+
;
1228+
1229+
$this->expectException(AutowiringFailedException::class);
1230+
$this->expectExceptionMessage('You have requested a non-existent parameter "required.parameter".');
1231+
(new AutowirePass())->process($container);
1232+
}
1233+
1234+
public function testAutowireAttributeNullFallbackTestOptional()
1235+
{
1236+
$container = new ContainerBuilder();
1237+
1238+
$container->register('foo', AutowireAttributeNullFallback::class)
1239+
->setAutowired(true)
1240+
->setPublic(true)
1241+
;
1242+
1243+
$container->setParameter('required.parameter', 'foo');
1244+
1245+
(new AutowirePass())->process($container);
1246+
1247+
$definition = $container->getDefinition('foo');
1248+
1249+
$this->assertSame(['foo'], $definition->getArguments());
1250+
}
1251+
12201252
public function testAsDecoratorAttribute()
12211253
{
12221254
$container = new ContainerBuilder();

src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes_80.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ public function __construct(
5656
}
5757
}
5858

59+
class AutowireAttributeNullFallback
60+
{
61+
public function __construct(
62+
#[Autowire('%required.parameter%')]
63+
public string $required,
64+
#[Autowire('%optional.parameter%')]
65+
public ?string $optional = null,
66+
) {
67+
}
68+
}
69+
5970
interface AsDecoratorInterface
6071
{
6172
}

0 commit comments

Comments
 (0)
0