Skip to content

Commit

Permalink
Support list<Type> syntax for autowiring a collection of services (#293)
Browse files Browse the repository at this point in the history
And for this usage and this case it's basically the same thing as Type[] and as array<int, Type>, both supported.
  • Loading branch information
spaze authored and dg committed Sep 22, 2023
1 parent 50e1970 commit bfc6b86
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/DI/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ private static function isArrayOf(\ReflectionParameter $parameter, ?Nette\Utils\
&& $type
&& $type->getSingleName() === 'array'
&& preg_match(
'#@param[ \t]+(?|([\w\\\\]+)\[\]|array<int,\s*([\w\\\\]+)>)[ \t]+\$' . $parameter->name . '#',
'#@param[ \t]+(?|([\w\\\\]+)\[\]|list<([\w\\\\]+)>|array<int,\s*([\w\\\\]+)>)[ \t]+\$' . $parameter->name . '#',
(string) $method->getDocComment(),
$m
)
Expand Down
15 changes: 14 additions & 1 deletion tests/DI/ContainerBuilder.autowiring.type[].phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ require __DIR__ . '/../bootstrap.php';
class Foo
{
public $bars;
public $waldos;
public $foos;
public $strings;


/**
* @param Service[] $bars
* @param list<Service> $waldos
* @param array<int,Foo> $foos
* @param string[] $strings
*/
public function __construct(array $bars = [], ?array $foos = null, array $strings = ['default'])
public function __construct(array $bars = [], array $waldos = [], ?array $foos = null, array $strings = ['default'])
{
$this->bars = $bars;
$this->waldos = $waldos;
$this->foos = $foos;
$this->strings = $strings;
}
Expand Down Expand Up @@ -67,6 +70,11 @@ Assert::same([
$container->getService('s2'),
$container->getService('s3'),
], $foo->bars);
Assert::same([
$container->getService('s1'),
$container->getService('s2'),
$container->getService('s3'),
], $foo->waldos);
Assert::same([], $foo->foos);
Assert::same(['default'], $foo->strings);

Expand All @@ -80,5 +88,10 @@ Assert::same([
$container->getService('s2'),
$container->getService('s3'),
], $foo2->bars);
Assert::same([
$container->getService('s1'),
$container->getService('s2'),
$container->getService('s3'),
], $foo2->waldos);
Assert::same([$foo], $foo2->foos); // difference
Assert::same(['default'], $foo2->strings);

0 comments on commit bfc6b86

Please sign in to comment.