Permalink
Browse files

Autowiring: autowiring by type[] works in runtime [Closes #183]

  • Loading branch information...
dg committed Feb 8, 2019
1 parent 547020b commit 869ada0bf1c06be1a2a02772e97cbda6d7b5ba9e
Showing with 18 additions and 5 deletions.
  1. +5 −5 src/DI/Autowiring.php
  2. +13 −0 tests/DI/ContainerBuilder.autowiring.type[].phpt
@@ -196,17 +196,17 @@ public static function completeArguments(\ReflectionFunctionAbstract $method, ar
}
} elseif (
$container instanceof Resolver
&& $method instanceof \ReflectionMethod
$method instanceof \ReflectionMethod
&& $parameter->isArray()
&& preg_match('#@param[ \t]+([\w\\\\]+)\[\][ \t]+\$' . $paramName . '#', (string) $method->getDocComment(), $m)
&& ($type = Reflection::expandClassName($m[1], $method->getDeclaringClass()))
&& (class_exists($type) || interface_exists($type))
) {
$src = $container instanceof Resolver ? $container->getContainerBuilder() : $container;
$res[$num] = [];
foreach ($container->getContainerBuilder()->findAutowired($type) as $def) {
if ($def !== $current) {
$res[$num][] = $def;
foreach ($src->findAutowired($type) as $item) {
if ($item !== $current) {
$res[$num][] = $item;
}
}
@@ -69,3 +69,16 @@ Assert::same([
], $foo->bars);
Assert::same([], $foo->foos);
Assert::same(['default'], $foo->strings);
// runtime
$foo2 = $container->createInstance('Foo');
Assert::type(Foo::class, $foo2);
Assert::same([
$container->getService('s1'),
$container->getService('s2'),
$container->getService('s3'),
], $foo2->bars);
Assert::same([$foo], $foo2->foos); // difference
Assert::same(['default'], $foo2->strings);

0 comments on commit 869ada0

Please sign in to comment.