10000 Merge branch '6.2' into 6.3 · symfony/symfony@a1ffae9 · GitHub
[go: up one dir, main page]

Skip to content

Commit a1ffae9

Browse files
Merge branch '6.2' into 6.3
* 6.2: [DependencyInjection] Fallback to default value when autowiring undefined parameters for optional arguments
2 parents 6378563 + 20ee89c commit a1ffae9

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\DependencyInjection\ContainerInterface;
2222
use Symfony\Component\DependencyInjection\Definition;
2323
use Symfony\Component\DependencyInjection\Exception\AutowiringFailedException;
24+
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
2425
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2526
use Symfony\Component\DependencyInjection\Reference;
2627
use Symfony\Component\DependencyInjection\TypedReference;
@@ -288,7 +289,18 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
288289
foreach ($parameter->getAttributes(Autowire::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
289290
$attribute = $attribute->newInstance();
290291
$invalidBehavior = $parameter->allowsNull() ? ContainerInterface::NULL_ON_INVALID_REFERENCE : ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE;
291-
$value = $this->processValue(new TypedReference($type ?: '?', $type ?: 'mixed', $invalidBehavior, $name, [$attribute, ...$target]));
292+
293+
try {
294+
$value = $this->processValue(new TypedReference($type ?: '?', $type ?: 'mixed', $invalidBehavior, $name, [$attribute, ...$target]));
295+
} catch (ParameterNotFoundException $e) {
296+
if (!$parameter->isDefaultValueAvailable()) {
297+
throw new AutowiringFailedException($this->currentId, $e->getMessage(), 0, $e);
298+
}
299+
$arguments[$index] = clone $this->defaultArgument;
300+
$arguments[$index]->value = $parameter->getDefaultValue();
301+
302+
continue 2;
303+
}
292304

293305
if ($attribute instanceof AutowireCallable) {
294306
$value = (new Definition($type = \is_string($attribute->lazy) ? $attribute->lazy : ($type ?: 'Closure')))

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

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

1332+
public function testAutowireAttributeNullFallbackTestRequired()
1333+
{
1334+
$container = new ContainerBuilder();
1335+
1336+
$container->register('foo', AutowireAttributeNullFallback::class)
1337+
->setAutowired(true)
1338+
->setPublic(true)
1339+
;
1340+
1341+
$this->expectException(AutowiringFailedException::class);
1342+
$this->expectExceptionMessage('You have requested a non-existent parameter "required.parameter".');
1343+
(new AutowirePass())->process($container);
1344+
}
1345+
1346+
public function testAutowireAttributeNullFallbackTestOptional()
1347+
{
1348+
$container = new ContainerBuilder();
1349+
1350+
$container->register('foo', AutowireAttributeNullFallback::class)
1351+
->setAutowired(true)
1352+
->setPublic(true)
1353+
;
1354+
1355+
$container->setParameter('required.parameter', 'foo');
1356+
1357+
(new AutowirePass())->process($container);
1358+
1359+
$definition = $container->getDefinition('foo');
1360+
1361+
$this->assertSame(['foo'], $definition->getArguments());
1362+
}
1363+
13321364
public function testAsDecoratorAttribute()
13331365
{
13341366
$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
@@ -69,6 +69,17 @@ public function __construct(
6969
}
7070
}
7171

72+
class AutowireAttributeNullFallback
73+
{
74+
public function __construct(
75+
#[Autowire('%required.parameter%')]
76+
public string $required,
77+
#[Autowire('%optional.parameter%')]
78+
public ?string $optional = null,
79+
) {
80+
}
81+
}
82+
7283
interface AsDecoratorInterface
7384
{
7485
}

0 commit comments

Comments
 (0)
0