bug #13087 [DependencyInjection] use/fix newest Definition::setFactory (nicolas-grekas)
This PR was merged into the 2.6 branch.
Discussion
----------
[DependencyInjection] use/fix newest Definition::setFactory
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
Commits
-------
f683a7a
[DependencyInjection] use/fix newest Definition::setFactory
This commit is contained in:
commit
c90edd95a6
@ -55,7 +55,8 @@
|
||||
<argument /> <!-- version format -->
|
||||
</service>
|
||||
|
||||
<service id="templating.asset.request_aware_package" class="Symfony\Component\Templating\Asset\PackageInterface" factory-service="templating.asset.package_factory" factory-method="getPackage" abstract="true">
|
||||
<service id="templating.asset.request_aware_package" class="Symfony\Component\Templating\Asset\PackageInterface" abstract="true">
|
||||
<factory service="templating.asset.package_factory" method="getPackage" />
|
||||
<argument type="service" id="request" strict="false" />
|
||||
<argument /> <!-- HTTP id -->
|
||||
<argument /> <!-- SSL id -->
|
||||
|
@ -16,9 +16,12 @@
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
<service id="validator" class="%validator.class%" factory-service="validator.builder" factory-method="getValidator" />
|
||||
<service id="validator" class="%validator.class%">
|
||||
<factory service="validator.builder" method="getValidator" />
|
||||
</service>
|
||||
|
||||
<service id="validator.builder" class="%validator.builder.class%" factory-class="%validator.builder.factory.class%" factory-method="createValidatorBuilder">
|
||||
<service id="validator.builder" class="%validator.builder.class%">
|
||||
<factory class="%validator.builder.factory.class%" method="createValidatorBuilder" />
|
||||
<call method="setConstraintValidatorFactory">
|
||||
<argument type="service" id="validator.validator_factory" />
|
||||
</call>
|
||||
|
@ -81,9 +81,12 @@ class ResolveDefinitionTemplatesPass implements CompilerPassInterface
|
||||
$def->setArguments($parentDef->getArguments());
|
||||
$def->setMethodCalls($parentDef->getMethodCalls());
|
||||
$def->setProperties($parentDef->getProperties());
|
||||
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());
|
||||
|
@ -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());
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -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
|
||||
|
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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'));
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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.");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user