[DependencyInjection] fixed PhpDumper when an inlined service definition has some properties

This commit is contained in:
Fabien Potencier 2013-01-05 09:13:52 +01:00
parent e939a4236c
commit cd153901ea
7 changed files with 103 additions and 7 deletions

View File

@ -415,16 +415,14 @@ class PhpDumper extends Dumper
}
$processed->offsetSet($iDefinition);
if (!$this->hasReference($id, $iDefinition->getMethodCalls())) {
if (!$this->hasReference($id, $iDefinition->getMethodCalls()) && !$this->hasReference($id, $iDefinition->getProperties())) {
continue;
}
if ($iDefinition->getMethodCalls()) {
$code .= $this->addServiceMethodCalls(null, $iDefinition, (string) $this->definitionVariables->offsetGet($iDefinition));
}
if ($iDefinition->getConfigurator()) {
$code .= $this->addServiceConfigurator(null, $iDefinition, (string) $this->definitionVariables->offsetGet($iDefinition));
}
$name = (string) $this->definitionVariables->offsetGet($iDefinition);
$code .= $this->addServiceMethodCalls(null, $iDefinition, $name);
$code .= $this->addServiceProperties(null, $iDefinition, $name);
$code .= $this->addServiceConfigurator(null, $iDefinition, $name);
}
if ('' !== $code) {

View File

@ -57,4 +57,15 @@ $container->
setFactoryMethod('getInstance')
;
$container
->register('foo_with_inline', 'Foo')
->addMethodCall('setBar', array(new Reference('inlined')))
;
$container
->register('inlined', 'Bar')
->setProperty('pub', 'pub')
->addMethodCall('setFoo', array(new Reference('foo_with_inline')))
->setPublic(false)
;
return $container;

View File

@ -9,6 +9,8 @@ digraph sc {
node_foo_bar [label="foo_bar\nFooClass\n", shape=record, fillcolor="#eeeeee", style="dotted"];
node_method_call1 [label="method_call1\nFooClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_factory_service [label="factory_service\nBar\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_foo_with_inline [label="foo_with_inline\nFoo\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_inlined [label="inlined\nBar\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerBuilder\n", shape=record, fillcolor="#9999ff", style="filled"];
node_foo2 [label="foo2\n\n", shape=record, fillcolor="#ff9999", style="filled"];
node_foo3 [label="foo3\n\n", shape=record, fillcolor="#ff9999", style="filled"];
@ -22,4 +24,6 @@ digraph sc {
node_method_call1 -> node_foo2 [label="setBar()" style="dashed"];
node_method_call1 -> node_foo3 [label="setBar()" style="dashed"];
node_method_call1 -> node_foobaz [label="setBar()" style="dashed"];
node_foo_with_inline -> node_inlined [label="setBar()" style="dashed"];
node_inlined -> node_foo_with_inline [label="setFoo()" style="dashed"];
}

View File

@ -104,6 +104,23 @@ class ProjectServiceContainer extends Container
return new $class();
}
/**
* Gets the 'foo_with_inline' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return Foo A Foo instance.
*/
protected function getFooWithInlineService()
{
$this->services['foo_with_inline'] = $instance = new \Foo();
$instance->setBar($this->get('inlined'));
return $instance;
}
/**
* Gets the 'method_call1' service.
*
@ -140,6 +157,28 @@ class ProjectServiceContainer extends Container
return $this->get('foo');
}
/**
* Gets the 'inlined' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* This service is private.
* If you want to be able to request this service from the container directly,
* make it public, otherwise you might end up with broken code.
*
* @return Bar A Bar instance.
*/
protected function getInlinedService()
{
$this->services['inlined'] = $instance = new \Bar();
$instance->setFoo($this->get('foo_with_inline'));
$instance->pub = 'pub';
return $instance;
}
/**
* Gets the default parameters.
*

View File

@ -112,6 +112,28 @@ class ProjectServiceContainer extends Container
return new \FooClass();
}
/**
* Gets the 'foo_with_inline' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return Foo A Foo instance.
*/
protected function getFooWithInlineService()
{
$a = new \Bar();
$this->services['foo_with_inline'] = $instance = new \Foo();
$a->setFoo($instance);
$a->pub = 'pub';
$instance->setBar($a);
return $instance;
}
/**
* Gets the 'method_call1' service.
*

View File

@ -51,6 +51,17 @@
</call>
</service>
<service id="factory_service" class="Bar" factory-method="getInstance" factory-service="foo.baz"/>
<service id="foo_with_inline" class="Foo">
<call method="setBar">
<argument type="service" id="inlined"/>
</call>
</service>
<service id="inlined" class="Bar" public="false">
<property name="pub">pub</property>
<call method="setFoo">
<argument type="service" id="foo_with_inline"/>
</call>
</service>
<service id="alias_for_foo" alias="foo"/>
</services>
</container>

View File

@ -41,4 +41,15 @@ services:
class: Bar
factory_method: getInstance
factory_service: foo.baz
foo_with_inline:
class: Foo
calls:
- [setBar, ['@inlined']]
inlined:
class: Bar
properties: { pub: pub }
calls:
- [setFoo, ['@foo_with_inline']]
alias_for_foo: @foo