Skip to content

Commit

Permalink
[DependencyInjection] use/fix newest Definition::setFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Dec 24, 2014
1 parent 72c169e commit f683a7a
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 64 deletions.
Expand Up @@ -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 -->
Expand Down
Expand Up @@ -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">

This comment has been minimized.

Copy link
@craigh

craigh Dec 24, 2014

I believe this is the source of the error I am getting right now when I do composer update:

 [Matthias\SymfonyServiceDefinitionValidator\Exception\InvalidServiceDefinitionsException]                                       
  Service definition validation errors (1):                                                                                       
  - validator: Definition for class Symfony\Component\Validator\Validator has no argument for required parameter metadataFactory 

This comment has been minimized.

Copy link
@nicolas-grekas

nicolas-grekas Dec 25, 2014

Author Member

Please open an issue, inline comment can't be tracked up to resolution

This comment has been minimized.

Copy link
@craigh

craigh Dec 25, 2014

done #13113

<argument type="service" id="validator.validator_factory" />
</call>
Expand Down
Expand Up @@ -81,9 +81,12 @@ private function resolveDefinition($id, DefinitionDecorator $definition)
$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());
Expand Down
14 changes: 5 additions & 9 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Expand Up @@ -940,18 +940,14 @@ public function createService(Definition $definition, $id, $tryProxy = true)

$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());
Expand Down
Expand Up @@ -316,31 +316,6 @@ public function testCreateServiceArguments()
$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
*/
Expand Down
24 changes: 0 additions & 24 deletions src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php
Expand Up @@ -42,30 +42,6 @@ public function testSetGetFactory()
$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
Expand Down
@@ -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.");
}
}

0 comments on commit f683a7a

Please sign in to comment.