Skip to content

Commit

Permalink
[DependencyInjection] fixed service resolution for factories
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Mar 11, 2015
1 parent 43f74ef commit f86ad95
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
Expand Up @@ -37,6 +37,7 @@ public function process(ContainerBuilder $container)
$definition->setClass($parameterBag->resolveValue($definition->getClass()));
$definition->setFile($parameterBag->resolveValue($definition->getFile()));
$definition->setArguments($parameterBag->resolveValue($definition->getArguments()));
$definition->setFactoryClass($parameterBag->resolveValue($definition->getFactoryClass()));

$calls = array();
foreach ($definition->getMethodCalls() as $name => $arguments) {
Expand Down
Expand Up @@ -1223,7 +1223,9 @@ private function dumpValue($value, $interpolate = true)
if (null !== $value->getFactoryClass()) {
return sprintf("call_user_func(array(%s, '%s')%s)", $this->dumpValue($value->getFactoryClass()), $value->getFactoryMethod(), count($arguments) > 0 ? ', '.implode(', ', $arguments) : '');
} elseif (null !== $value->getFactoryService()) {
return sprintf("%s->%s(%s)", $this->getServiceCall($value->getFactoryService()), $value->getFactoryMethod(), implode(', ', $arguments));
$service = $this->dumpValue($definition->getFactoryService());

return sprintf("%s->%s(%s)", 0 === strpos($service, '$') ? sprintf('$this->get(%s)', $service) : $this->getServiceCall($value->getFactoryService()), $value->getFactoryMethod(), implode(', ', $arguments));
} else {
throw new RuntimeException('Cannot dump definitions which have factory method without factory service or factory class.');
}
Expand Down
Expand Up @@ -322,8 +322,14 @@ public function testCreateServiceFactoryMethod()
{
$builder = new ContainerBuilder();
$builder->register('bar', 'stdClass');
$builder->register('foo1', 'FooClass')->setFactoryClass('FooClass')->setFactoryMethod('getInstance')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar')));
$builder
->register('foo1', 'FooClass')
->setFactoryClass('%foo_class%')
->setFactoryMethod('getInstance')
->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar')))
;
$builder->setParameter('value', 'bar');
$builder->setParameter('foo_class', 'FooClass');
$this->assertTrue($builder->get('foo1')->called, '->createService() calls the factory method to create the service instance');
$this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar')), $builder->get('foo1')->arguments, '->createService() passes the arguments to the factory method');
}
Expand All @@ -334,10 +340,14 @@ public function testCreateServiceFactoryMethod()
public function testCreateServiceFactoryService()
{
$builder = new ContainerBuilder();
$builder->register('baz_service')->setFactoryService('baz_factory')->setFactoryMethod('getInstance');
$builder->register('baz_factory', 'BazClass');

$this->assertInstanceOf('BazClass', $builder->get('baz_service'));
$builder->register('foo_service', 'FooClass');
$builder
->register('foo', 'FooClass')
->setFactoryService('%foo_service%')
->setFactoryMethod('getInstance')
;
$builder->setParameter('foo_service', 'foo_service');
$this->assertTrue($builder->get('foo')->called, '->createService() calls the factory method to create the service instance');
}

/**
Expand Down

0 comments on commit f86ad95

Please sign in to comment.