Skip to content

Commit 5d5ec18

Browse files
enumagdg
authored andcommitted
ContainerBuilder: improved generated factory parameters resolution [Closes #17]
1 parent e78b79a commit 5d5ec18

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

src/DI/ContainerBuilder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,10 @@ private function resolveImplement(ServiceDefinition $def, $name)
306306

307307
if (!$def->parameters) {
308308
$ctorParams = array();
309-
if ($def->factory && !$def->factory->arguments && ($class = $this->resolveEntityClass($def->factory, array($name => 1)))
309+
if (empty($def->factory->entity)) {
310+
$def->setFactory($def->class, $def->factory ? $def->factory->arguments : array());
311+
}
312+
if (($class = $this->resolveEntityClass($def->factory, array($name => 1)))
310313
&& ($ctor = Reflection\ClassType::from($class)->getConstructor())
311314
) {
312315
foreach ($ctor->getParameters() as $param) {

tests/DI/Compiler.generatedFactory.phpt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,23 @@ interface IFooFactory
9797
public function create(Baz $baz = NULL);
9898
}
9999

100+
class TestClass
101+
{
102+
public $foo;
103+
public $bar;
104+
public function __construct($foo, $bar)
105+
{
106+
$this->foo = $foo;
107+
$this->bar = $bar;
108+
}
109+
}
110+
111+
interface ITestClassFactory
112+
{
113+
/** @return TestClass */
114+
public function create($bar);
115+
}
116+
100117
class TestExtension extends DI\CompilerExtension
101118
{
102119
public function loadConfiguration()
@@ -184,6 +201,26 @@ Assert::same($container->getService('bar'), $foo->bar);
184201
Assert::null( $foo->baz );
185202

186203

204+
Assert::type( 'IFooFactory', $container->getService('fooFactory4') );
205+
$foo = $container->getService('fooFactory4')->create($container->getService('baz'));
206+
Assert::type( 'Foo', $foo );
207+
Assert::type( 'Bar', $foo->bar );
208+
Assert::same($container->getService('bar'), $foo->bar);
209+
Assert::type( 'Baz', $foo->baz );
210+
Assert::same($container->getService('baz'), $foo->baz);
211+
$foo = $container->getService('fooFactory4')->create();
212+
Assert::type( 'Foo', $foo );
213+
Assert::type( 'Bar', $foo->bar );
214+
Assert::same($container->getService('bar'), $foo->bar);
215+
Assert::null( $foo->baz );
216+
217+
218+
Assert::type( 'ITestClassFactory', $container->getService('factory5') );
219+
$obj = $container->getService('factory5')->create('bar');
220+
Assert::same('foo', $obj->foo);
221+
Assert::same('bar', $obj->bar);
222+
223+
187224
class Bad1
188225
{
189226
public function __construct(Bar $bar)

tests/DI/files/compiler.generatedFactory.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ services:
2525
fooFactory3:
2626
create: Foo
2727
implement: IFooFactory
28+
29+
fooFactory4: IFooFactory
30+
31+
factory5:
32+
implement: ITestClassFactory
33+
arguments: ['foo']

0 commit comments

Comments
 (0)