10000 [DependencyInjection] Added support for variadics in named arguments · symfony/symfony@b5c0e89 · GitHub
[go: up one dir, main page]

Skip to content

Commit b5c0e89

Browse files
pkowalczykfabpot
pkowalczyk
authored andcommitted
[DependencyInjection] Added support for variadics in named arguments
1 parent a135690 commit b5c0e89

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ protected function processValue($value, $isRoot = false)
5555
if (isset($key[0]) && '$' === $key[0]) {
5656
foreach ($parameters as $j => $p) {
5757
if ($key === '$'.$p->name) {
58-
$resolvedArguments[$j] = $argument;
58+
if ($p->isVariadic() && \is_array($argument)) {
59+
foreach ($argument as $variadicArgument) {
60+
$resolvedArguments[$j++] = $variadicArgument;
61+
}
62+
} else {
63+
$resolvedArguments[$j] = $argument;
64+
}
5965

6066
continue 2;
6167
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\DependencyInjection\Reference;
1818
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
1919
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
20+
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsVariadicsDummy;
2021
use Symfony\Component\DependencyInjection\Tests\Fixtures\SimilarArgumentsDummy;
2122

2223
/**
@@ -152,6 +153,36 @@ public function testResolvePrioritizeNamedOverType()
152153

153154
$this->assertEquals(array(new Reference('bar'), 'qwerty', new Reference('foo')), $definition->getArguments());
154155
}
156+
157+
public function testVariadics()
158+
{
159+
$container = new ContainerBuilder();
160+
161+
$definition = $container->register(NamedArgumentsVariadicsDummy::class, NamedArgumentsVariadicsDummy::class);
162+
$definition->setArguments(
163+
array(
164+
'$class' => new \stdClass(),
165+
'$variadics' => array(
166+
new Reference('foo'),
167+
new Reference('bar'),
168+
new Reference('baz'),
169+
),
170+
)
171+
);
172+
173+
$pass = new ResolveNamedArgumentsPass();
174+
$pass->process($container);
175+
176+
$this->assertEquals(
177+
array(
178+
0 => new \stdClass(),
179+
1 => new Reference('foo'),
180+
2 => new Reference('bar'),
181+
3 => new Reference('baz'),
182+
),
183+
$definition->getArguments()
184+
);
185+
}
155186
}
156187

157188
class NoConstructor
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
4+
5+
class NamedArgumentsVariadicsDummy
6+
{
7+
public function __construct(\stdClass $class, ...$variadics)
8+
{
9+
}
10+
}

0 commit comments

Comments
 (0)
0