Skip to content

Commit 4deec09

Browse files
[DependencyInjection] use/fix newest Definition::setFactory
1 parent d7a8085 commit 4deec09

File tree

6 files changed

+95
-61
lines changed

6 files changed

+95
-61
lines changed

Compiler/ResolveDefinitionTemplatesPass.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,12 @@ private function resolveDefinition($id, DefinitionDecorator $definition)
8181
$def->setArguments($parentDef->getArguments());
8282
$def->setMethodCalls($parentDef->getMethodCalls());
8383
$def->setProperties($parentDef->getProperties());
84-
$def->setFactoryClass($parentDef->getFactoryClass());
85-
$def->setFactoryMethod($parentDef->getFactoryMethod());
86-
$def->setFactoryService($parentDef->getFactoryService());
84+
if (null !== $parentDef->getFactoryMethod()) {
85+
$def->setFactoryClass($parentDef->getFactoryClass());
86+
$def->setFactoryMethod($parentDef->getFactoryMethod());
87+
$def->setFactoryService($parentDef->getFactoryService());
88+
}
89+
$def->setFactory($parentDef->getFactory());
8790
$def->setConfigurator($parentDef->getConfigurator());
8891
$def->setFile($parentDef->getFile());
8992
$def->setPublic($parentDef->isPublic());

ContainerBuilder.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -940,18 +940,14 @@ public function createService(Definition $definition, $id, $tryProxy = true)
940940

941941
$arguments = $this->resolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArguments())));
942942

943-
if (null !== $definition->getFactory()) {
944-
$factory = $definition->getFactory();
945-
946-
if (is_string($factory)) {
947-
$callable = $definition->getFactory();
948-
} elseif (is_array($factory)) {
949-
$callable = array($this->resolveServices($factory[0]), $factory[1]);
950-
} else {
943+
if (null !== $factory = $definition->getFactory()) {
944+
if (is_array($factory)) {
945+
$factory = array($this->resolveServices($parameterBag->resolveValue($factory[0])), $factory[1]);
946+
} elseif (!is_string($factory)) {
951947
throw new RuntimeException(sprintf('Cannot create service "%s" because of invalid factory', $id));
952948
}
953949

954-
$service = call_user_func_array($callable, $arguments);
950+
$service = call_user_func_array($factory, $arguments);
955951
} elseif (null !== $definition->getFactoryMethod()) {
956952
if (null !== $definition->getFactoryClass()) {
957953
$factory = $parameterBag->resolveValue($definition->getFactoryClass());

Tests/ContainerBuilderTest.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -316,31 +316,6 @@ public function testCreateServiceArguments()
316316
$this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar'), '%unescape_it%'), $builder->get('foo1')->arguments, '->createService() replaces parameters and service references in the arguments provided by the service definition');
317317
}
318318

319-
/**
320-
* @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
321-
*/
322-
public function testCreateServiceFactoryMethod()
323-
{
324-
$builder = new ContainerBuilder();
325-
$builder->register('bar', 'stdClass');
326-
$builder->register('foo1', 'Bar\FooClass')->setFactoryClass('Bar\FooClass')->setFactoryMethod('getInstance')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar')));
327-
$builder->setParameter('value', 'bar');
328-
$this->assertTrue($builder->get('foo1')->called, '->createService() calls the factory method to create the service instance');
329-
$this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar')), $builder->get('foo1')->arguments, '->createService() passes the arguments to the factory method');
330-
}
331-
332-
/**
333-
* @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
334-
*/
335-
public function testCreateServiceFactoryService()
336-
{
337-
$builder = new ContainerBuilder();
338-
$builder->register('baz_service')->setFactoryService('baz_factory')->setFactoryMethod('getInstance');
339-
$builder->register('baz_factory', 'BazClass');
340-
341-
$this->assertInstanceOf('BazClass', $builder->get('baz_service'));
342-
}
343-
344319
/**
345320
* @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
346321
*/

Tests/DefinitionTest.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,6 @@ public function testSetGetFactory()
4242
$this->assertEquals(array('Foo', 'bar'), $def->getFactory(), '->setFactory() converts string static method call to the array');
4343
}
4444

45-
public function testSetGetFactoryClass()
46-
{
47-
$def = new Definition('stdClass');
48-
$this->assertNull($def->getFactoryClass());
49-
$this->assertSame($def, $def->setFactoryClass('stdClass2'), "->setFactoryClass() implements a fluent interface.");
50-
$this->assertEquals('stdClass2', $def->getFactoryClass(), "->getFactoryClass() returns current class to construct this service.");
51-
}
52-
53-
public function testSetGetFactoryMethod()
54-
{
55-
$def = new Definition('stdClass');
56-
$this->assertNull($def->getFactoryMethod());
57-
$this->assertSame($def, $def->setFactoryMethod('foo'), '->setFactoryMethod() implements a fluent interface');
58-
$this->assertEquals('foo', $def->getFactoryMethod(), '->getFactoryMethod() returns the factory method name');
59-
}
60-
61-
public function testSetGetFactoryService()
62-
{
63-
$def = new Definition('stdClass');
64-
$this->assertNull($def->getFactoryService());
65-
$this->assertSame($def, $def->setFactoryService('foo.bar'), "->setFactoryService() implements a fluent interface.");
66-
$this->assertEquals('foo.bar', $def->getFactoryService(), "->getFactoryService() returns current service to construct this service.");
67-
}
68-
6945
/**
7046
* @covers Symfony\Component\DependencyInjection\Definition::setClass
7147
* @covers Symfony\Component\DependencyInjection\Definition::getClass

Tests/LegacyContainerBuilderTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests;
13+
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\DependencyInjection\Reference;
16+
17+
class LegacyContainerBuilderTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
21+
*/
22+
public function testCreateServiceFactoryMethod()
23+
{
24+
$builder = new ContainerBuilder();
25+
$builder->register('bar', 'stdClass');
26+
$builder->register('foo1', 'Bar\FooClass')->setFactoryClass('Bar\FooClass')->setFactoryMethod('getInstance')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar')));
27+
$builder->setParameter('value', 'bar');
28+
$this->assertTrue($builder->get('foo1')->called, '->createService() calls the factory method to create the service instance');
29+
$this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar')), $builder->get('foo1')->arguments, '->createService() passes the arguments to the factory method');
30+
}
31+
32+
/**
33+
* @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
34+
*/
35+
public function testCreateServiceFactoryService()
36+
{
37+
$builder = new ContainerBuilder();
38+
$builder->register('baz_service')->setFactoryService('baz_factory')->setFactoryMethod('getInstance');
39+
$builder->register('baz_factory', 'BazClass');
40+
41+
$this->assertInstanceOf('BazClass', $builder->get('baz_service'));
42+
}
43+
}

Tests/LegacyDefinitionTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests;
13+
14+
use Symfony\Component\DependencyInjection\Definition;
15+
16+
class LegacyDefinitionTest extends \PHPUnit_Framework_TestCase
17+
{
18+
public function testSetGetFactoryClass()
19+
{
20+
$def = new Definition('stdClass');
21+
$this->assertNull($def->getFactoryClass());
22+
$this->assertSame($def, $def->setFactoryClass('stdClass2'), "->setFactoryClass() implements a fluent interface.");
23+
$this->assertEquals('stdClass2', $def->getFactoryClass(), "->getFactoryClass() returns current class to construct this service.");
24+
}
25+
26+
public function testSetGetFactoryMethod()
27+
{
28+
$def = new Definition('stdClass');
29+
$this->assertNull($def->getFactoryMethod());
30+
$this->assertSame($def, $def->setFactoryMethod('foo'), '->setFactoryMethod() implements a fluent interface');
31+
$this->assertEquals('foo', $def->getFactoryMethod(), '->getFactoryMethod() returns the factory method name');
32+
}
33+
34+
public function testSetGetFactoryService()
35+
{
36+
$def = new Definition('stdClass');
37+
$this->assertNull($def->getFactoryService());
38+
$this->assertSame($def, $def->setFactoryService('foo.bar'), "->setFactoryService() implements a fluent interface.");
39+
$this->assertEquals('foo.bar', $def->getFactoryService(), "->getFactoryService() returns current service to construct this service.");
40+
}
41+
}

0 commit comments

Comments
 (0)