From cd153901eae74dd10d1c9372ca7a6d7157304f04 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 5 Jan 2013 09:13:52 +0100 Subject: [PATCH] [DependencyInjection] fixed PhpDumper when an inlined service definition has some properties --- .../DependencyInjection/Dumper/PhpDumper.php | 12 +++--- .../Fixtures/containers/container9.php | 11 ++++++ .../Fixtures/graphviz/services9.dot | 4 ++ .../Fixtures/php/services9.php | 39 +++++++++++++++++++ .../Fixtures/php/services9_compiled.php | 22 +++++++++++ .../Fixtures/xml/services9.xml | 11 ++++++ .../Fixtures/yaml/services9.yml | 11 ++++++ 7 files changed, 103 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index d04f558980db..d5acdd729df6 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -415,16 +415,14 @@ private function addServiceInlinedDefinitionsSetup($id, $definition) } $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) { diff --git a/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/containers/container9.php b/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/containers/container9.php index da0a7bbb45bd..72c355f70c1b 100644 --- a/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/containers/container9.php +++ b/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/containers/container9.php @@ -57,4 +57,15 @@ 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; diff --git a/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/graphviz/services9.dot b/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/graphviz/services9.dot index 3bed9669c71f..b9618bb051ef 100644 --- a/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/graphviz/services9.dot +++ b/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/graphviz/services9.dot @@ -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"]; } diff --git a/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9.php b/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9.php index 19764998f5d0..92969123e761 100644 --- a/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9.php +++ b/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9.php @@ -104,6 +104,23 @@ protected function getFooBarService() 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 @@ protected function getAliasForFooService() 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. * diff --git a/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9_compiled.php b/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9_compiled.php index f38007055569..0ef72e1c712a 100644 --- a/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9_compiled.php +++ b/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9_compiled.php @@ -112,6 +112,28 @@ protected function getFooBarService() 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. * diff --git a/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/xml/services9.xml b/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/xml/services9.xml index 9b935d5845f5..4c22e6b51fdf 100644 --- a/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/xml/services9.xml +++ b/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/xml/services9.xml @@ -51,6 +51,17 @@ + + + + + + + pub + + + + diff --git a/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/services9.yml b/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/services9.yml index ebd3ed3d1a06..ab26425a7d6a 100644 --- a/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/services9.yml +++ b/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/services9.yml @@ -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