Skip to content

Commit 516df4f

Browse files
spazedg
authored andcommitted
Support list<Type> syntax for autowiring a collection of services (#293)
And for this usage and this case it's basically the same thing as Type[] and as array<int, Type>, both supported.
1 parent 5c507e6 commit 516df4f

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/DI/Resolver.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ private static function isArrayOf(\ReflectionParameter $parameter, ?Nette\Utils\
690690
&& $type
691691
&& $type->getSingleName() === 'array'
692692
&& preg_match(
693-
'#@param[ \t]+(?|([\w\\\\]+)\[\]|array<int,\s*([\w\\\\]+)>)[ \t]+\$' . $parameter->name . '#',
693+
'#@param[ \t]+(?|([\w\\\\]+)\[\]|list<([\w\\\\]+)>|array<int,\s*([\w\\\\]+)>)[ \t]+\$' . $parameter->name . '#',
694694
(string) $method->getDocComment(),
695695
$m
696696
)

tests/DI/ContainerBuilder.autowiring.type[].phpt

+14-1
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@ require __DIR__ . '/../bootstrap.php';
1616
class Foo
1717
{
1818
public $bars;
19+
public $waldos;
1920
public $foos;
2021
public $strings;
2122

2223

2324
/**
2425
* @param Service[] $bars
26+
* @param list<Service> $waldos
2527
* @param array<int,Foo> $foos
2628
* @param string[] $strings
2729
*/
28-
public function __construct(array $bars = [], ?array $foos = null, array $strings = ['default'])
30+
public function __construct(array $bars = [], array $waldos = [], ?array $foos = null, array $strings = ['default'])
2931
{
3032
$this->bars = $bars;
33+
$this->waldos = $waldos;
3134
$this->foos = $foos;
3235
$this->strings = $strings;
3336
}
@@ -67,6 +70,11 @@ Assert::same([
6770
$container->getService('s2'),
6871
$container->getService('s3'),
6972
], $foo->bars);
73+
Assert::same([
74+
$container->getService('s1'),
75+
$container->getService('s2'),
76+
$container->getService('s3'),
77+
], $foo->waldos);
7078
Assert::same([], $foo->foos);
7179
Assert::same(['default'], $foo->strings);
7280

@@ -80,5 +88,10 @@ Assert::same([
8088
$container->getService('s2'),
8189
$container->getService('s3'),
8290
], $foo2->bars);
91+
Assert::same([
92+
$container->getService('s1'),
93+
$container->getService('s2'),
94+
$container->getService('s3'),
95+
], $foo2->waldos);
8396
Assert::same([$foo], $foo2->foos); // difference
8497
Assert::same(['default'], $foo2->strings);

0 commit comments

Comments
 (0)