Skip to content

Commit e34387d

Browse files
committed
bug #13519 [DependencyInjection] fixed service resolution for factories (fabpot)
This PR was merged into the 2.3 branch. Discussion ---------- [DependencyInjection] fixed service resolution for factories | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #13455 | License | MIT | Doc PR | n/a In the service container, factories can be defined with a class/method pair or a service/method pair. The class or service value can be a container parameter, but it was not supported everywhere, this PR fixes that. Note that the method can never be a container parameter as this is supported nowhere in the current code, so this has not been changed. Another PR will fix the 2.6 way of configuring a factory. Commits ------- f86ad95 [DependencyInjection] fixed service resolution for factories
2 parents 52ab206 + f86ad95 commit e34387d

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

src/Symfony/Component/DependencyInjection/Compiler/ResolveParameterPlaceHoldersPass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function process(ContainerBuilder $container)
3737
$definition->setClass($parameterBag->resolveValue($definition->getClass()));
3838
$definition->setFile($parameterBag->resolveValue($definition->getFile()));
3939
$definition->setArguments($parameterBag->resolveValue($definition->getArguments()));
40+
$definition->setFactoryClass($parameterBag->resolveValue($definition->getFactoryClass()));
4041

4142
$calls = array();
4243
foreach ($definition->getMethodCalls() as $name => $arguments) {

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,9 @@ private function dumpValue($value, $interpolate = true)
12231223
if (null !== $value->getFactoryClass()) {
12241224
return sprintf("call_user_func(array(%s, '%s')%s)", $this->dumpValue($value->getFactoryClass()), $value->getFactoryMethod(), count($arguments) > 0 ? ', '.implode(', ', $arguments) : '');
12251225
} elseif (null !== $value->getFactoryService()) {
1226-
return sprintf("%s->%s(%s)", $this->getServiceCall($value->getFactoryService()), $value->getFactoryMethod(), implode(', ', $arguments));
1226+
$service = $this->dumpValue($definition->getFactoryService());
1227+
1228+
return sprintf("%s->%s(%s)", 0 === strpos($service, '$') ? sprintf('$this->get(%s)', $service) : $this->getServiceCall($value->getFactoryService()), $value->getFactoryMethod(), implode(', ', $arguments));
12271229
} else {
12281230
throw new RuntimeException('Cannot dump definitions which have factory method without factory service or factory class.');
12291231
}

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,14 @@ public function testCreateServiceFactoryMethod()
322322
{
323323
$builder = new ContainerBuilder();
324324
$builder->register('bar', 'stdClass');
325-
$builder->register('foo1', 'FooClass')->setFactoryClass('FooClass')->setFactoryMethod('getInstance')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar')));
325+
$builder
326+
->register('foo1', 'FooClass')
327+
->setFactoryClass('%foo_class%')
328+
->setFactoryMethod('getInstance')
329+
->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar')))
330+
;
326331
$builder->setParameter('value', 'bar');
332+
$builder->setParameter('foo_class', 'FooClass');
327333
$this->assertTrue($builder->get('foo1')->called, '->createService() calls the factory method to create the service instance');
328334
$this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar')), $builder->get('foo1')->arguments, '->createService() passes the arguments to the factory method');
329335
}
@@ -334,10 +340,14 @@ public function testCreateServiceFactoryMethod()
334340
public function testCreateServiceFactoryService()
335341
{
336342
$builder = new ContainerBuilder();
337-
$builder->register('baz_service')->setFactoryService('baz_factory')->setFactoryMethod('getInstance');
338-
$builder->register('baz_factory', 'BazClass');
339-
340-
$this->assertInstanceOf('BazClass', $builder->get('baz_service'));
343+
$builder->register('foo_service', 'FooClass');
344+
$builder
345+
->register('foo', 'FooClass')
346+
->setFactoryService('%foo_service%')
347+
->setFactoryMethod('getInstance')
348+
;
349+
$builder->setParameter('foo_service', 'foo_service');
350+
$this->assertTrue($builder->get('foo')->called, '->createService() calls the factory method to create the service instance');
341351
}
342352

343353
/**

0 commit comments

Comments
 (0)