diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml index 0ebb44bbd2..6e482f8573 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml @@ -55,7 +55,8 @@ - + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml index 336379a11b..c161a1e3d2 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml @@ -16,9 +16,12 @@ - + + + - + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/LoggingTranslatorPassTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/LoggingTranslatorPassTest.php index 9fb0721ece..e71237a7fc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/LoggingTranslatorPassTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/LoggingTranslatorPassTest.php @@ -39,6 +39,11 @@ class LoggingTranslatorPassTest extends \PHPUnit_Framework_TestCase ->method('getDefinition') ->will($this->returnValue($definition)); + $container->expects($this->once()) + ->method('hasParameter') + ->with('translator.logging') + ->will($this->returnValue(true)); + $definition->expects($this->once()) ->method('getClass') ->will($this->returnValue("%translator.class%")); diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ResolveDefinitionTemplatesPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ResolveDefinitionTemplatesPass.php index ce681e5f3a..032d27aaee 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ResolveDefinitionTemplatesPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ResolveDefinitionTemplatesPass.php @@ -81,9 +81,12 @@ class ResolveDefinitionTemplatesPass implements CompilerPassInterface $def->setArguments($parentDef->getArguments()); $def->setMethodCalls($parentDef->getMethodCalls()); $def->setProperties($parentDef->getProperties()); - $def->setFactoryClass($parentDef->getFactoryClass()); - $def->setFactoryMethod($parentDef->getFactoryMethod()); - $def->setFactoryService($parentDef->getFactoryService()); + if (null !== $parentDef->getFactoryMethod()) { + $def->setFactoryClass($parentDef->getFactoryClass()); + $def->setFactoryMethod($parentDef->getFactoryMethod()); + $def->setFactoryService($parentDef->getFactoryService()); + } + $def->setFactory($parentDef->getFactory()); $def->setConfigurator($parentDef->getConfigurator()); $def->setFile($parentDef->getFile()); $def->setPublic($parentDef->isPublic()); diff --git a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php index 510b942476..e2b1761e6b 100644 --- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php +++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php @@ -940,18 +940,14 @@ class ContainerBuilder extends Container implements TaggedContainerInterface $arguments = $this->resolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArguments()))); - if (null !== $definition->getFactory()) { - $factory = $definition->getFactory(); - - if (is_string($factory)) { - $callable = $definition->getFactory(); - } elseif (is_array($factory)) { - $callable = array($this->resolveServices($factory[0]), $factory[1]); - } else { + if (null !== $factory = $definition->getFactory()) { + if (is_array($factory)) { + $factory = array($this->resolveServices($parameterBag->resolveValue($factory[0])), $factory[1]); + } elseif (!is_string($factory)) { throw new RuntimeException(sprintf('Cannot create service "%s" because of invalid factory', $id)); } - $service = call_user_func_array($callable, $arguments); + $service = call_user_func_array($factory, $arguments); } elseif (null !== $definition->getFactoryMethod()) { if (null !== $definition->getFactoryClass()) { $factory = $parameterBag->resolveValue($definition->getFactoryClass()); diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php index e74e5d4644..debdc5da49 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php @@ -316,31 +316,6 @@ class ContainerBuilderTest extends \PHPUnit_Framework_TestCase $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'); } - /** - * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService - */ - public function testCreateServiceFactoryMethod() - { - $builder = new ContainerBuilder(); - $builder->register('bar', 'stdClass'); - $builder->register('foo1', 'Bar\FooClass')->setFactoryClass('Bar\FooClass')->setFactoryMethod('getInstance')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar'))); - $builder->setParameter('value', 'bar'); - $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'); - } - - /** - * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService - */ - 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')); - } - /** * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService */ diff --git a/src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php b/src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php index cfcb5f486f..583d49f1e9 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php @@ -42,30 +42,6 @@ class DefinitionTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array('Foo', 'bar'), $def->getFactory(), '->setFactory() converts string static method call to the array'); } - public function testSetGetFactoryClass() - { - $def = new Definition('stdClass'); - $this->assertNull($def->getFactoryClass()); - $this->assertSame($def, $def->setFactoryClass('stdClass2'), "->setFactoryClass() implements a fluent interface."); - $this->assertEquals('stdClass2', $def->getFactoryClass(), "->getFactoryClass() returns current class to construct this service."); - } - - public function testSetGetFactoryMethod() - { - $def = new Definition('stdClass'); - $this->assertNull($def->getFactoryMethod()); - $this->assertSame($def, $def->setFactoryMethod('foo'), '->setFactoryMethod() implements a fluent interface'); - $this->assertEquals('foo', $def->getFactoryMethod(), '->getFactoryMethod() returns the factory method name'); - } - - public function testSetGetFactoryService() - { - $def = new Definition('stdClass'); - $this->assertNull($def->getFactoryService()); - $this->assertSame($def, $def->setFactoryService('foo.bar'), "->setFactoryService() implements a fluent interface."); - $this->assertEquals('foo.bar', $def->getFactoryService(), "->getFactoryService() returns current service to construct this service."); - } - /** * @covers Symfony\Component\DependencyInjection\Definition::setClass * @covers Symfony\Component\DependencyInjection\Definition::getClass diff --git a/src/Symfony/Component/DependencyInjection/Tests/LegacyContainerBuilderTest.php b/src/Symfony/Component/DependencyInjection/Tests/LegacyContainerBuilderTest.php new file mode 100644 index 0000000000..496b621b6b --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/LegacyContainerBuilderTest.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests; + +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; + +class LegacyContainerBuilderTest extends \PHPUnit_Framework_TestCase +{ + /** + * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService + */ + public function testCreateServiceFactoryMethod() + { + $builder = new ContainerBuilder(); + $builder->register('bar', 'stdClass'); + $builder->register('foo1', 'Bar\FooClass')->setFactoryClass('Bar\FooClass')->setFactoryMethod('getInstance')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar'))); + $builder->setParameter('value', 'bar'); + $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'); + } + + /** + * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService + */ + 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')); + } +} diff --git a/src/Symfony/Component/DependencyInjection/Tests/LegacyDefinitionTest.php b/src/Symfony/Component/DependencyInjection/Tests/LegacyDefinitionTest.php new file mode 100644 index 0000000000..9fb0c7ff0e --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/LegacyDefinitionTest.php @@ -0,0 +1,41 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests; + +use Symfony\Component\DependencyInjection\Definition; + +class LegacyDefinitionTest extends \PHPUnit_Framework_TestCase +{ + public function testSetGetFactoryClass() + { + $def = new Definition('stdClass'); + $this->assertNull($def->getFactoryClass()); + $this->assertSame($def, $def->setFactoryClass('stdClass2'), "->setFactoryClass() implements a fluent interface."); + $this->assertEquals('stdClass2', $def->getFactoryClass(), "->getFactoryClass() returns current class to construct this service."); + } + + public function testSetGetFactoryMethod() + { + $def = new Definition('stdClass'); + $this->assertNull($def->getFactoryMethod()); + $this->assertSame($def, $def->setFactoryMethod('foo'), '->setFactoryMethod() implements a fluent interface'); + $this->assertEquals('foo', $def->getFactoryMethod(), '->getFactoryMethod() returns the factory method name'); + } + + public function testSetGetFactoryService() + { + $def = new Definition('stdClass'); + $this->assertNull($def->getFactoryService()); + $this->assertSame($def, $def->setFactoryService('foo.bar'), "->setFactoryService() implements a fluent interface."); + $this->assertEquals('foo.bar', $def->getFactoryService(), "->getFactoryService() returns current service to construct this service."); + } +}