From fe4f7eccf74db5ca92f89ea0850fd0fd230f2277 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 27 Jan 2017 16:27:34 +0100 Subject: [PATCH] check for circular refs caused by method calls --- .../Compiler/PassConfig.php | 1 + .../Tests/Compiler/IntegrationTest.php | 26 +++++++++++++++++++ .../Tests/Fixtures/containers/container9.php | 1 - .../Tests/Fixtures/graphviz/services9.dot | 1 - .../Tests/Fixtures/php/services9.php | 6 +---- .../Tests/Fixtures/php/services9_compiled.php | 11 +++----- .../Tests/Fixtures/xml/services9.xml | 6 +---- .../Tests/Fixtures/yaml/services9.yml | 2 -- 8 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php b/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php index c03ff9deb7..6daa4b7546 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php @@ -63,6 +63,7 @@ class PassConfig new RemoveUnusedDefinitionsPass(), )), new CheckExceptionOnInvalidReferenceBehaviorPass(), + new CheckCircularReferencesPass(), ); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php index c4479403aa..c4eee4033e 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php @@ -113,4 +113,30 @@ class IntegrationTest extends \PHPUnit_Framework_TestCase $this->assertFalse($container->hasDefinition('b')); $this->assertFalse($container->hasDefinition('c'), 'Service C was not inlined.'); } + + /** + * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException + */ + public function testCircularReferencesCausedByMethodCallsAreDetectedDuringCompilation() + { + $container = new ContainerBuilder(); + $container->setResourceTracking(false); + + $container + ->register('foobar', '\stdClass') + ->addArgument(new Reference('foo')) + ; + + $container + ->register('foo', '\stdClass') + ->addArgument(new Reference('bar')) + ; + + $container + ->register('foo', '\stdClass') + ->addMethodCall('addFoobar', array(new Reference('foobar'))) + ; + + $container->compile(); + } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php index 695f2875ff..3db661cf8c 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php @@ -64,7 +64,6 @@ $container ; $container ->register('baz', 'Baz') - ->addMethodCall('setFoo', array(new Reference('foo_with_inline'))) ; $container ->register('request', 'Request') diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot index b3b424e2e7..49de5aa2f5 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot @@ -36,6 +36,5 @@ digraph sc { node_method_call1 -> node_foobaz [label="setBar()" style="dashed"]; node_foo_with_inline -> node_inlined [label="setBar()" style="dashed"]; node_inlined -> node_baz [label="setBaz()" style="dashed"]; - node_baz -> node_foo_with_inline [label="setFoo()" style="dashed"]; node_configurator_service -> node_baz [label="setFoo()" style="dashed"]; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php index ce8930b8dd..107812974c 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php @@ -80,11 +80,7 @@ class ProjectServiceContainer extends Container */ protected function getBazService() { - $this->services['baz'] = $instance = new \Baz(); - - $instance->setFoo($this->get('foo_with_inline')); - - return $instance; + return $this->services['baz'] = new \Baz(); } /** diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php index 559560fa6d..9592ed87af 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -99,11 +99,7 @@ class ProjectServiceContainer extends Container */ protected function getBazService() { - $this->services['baz'] = $instance = new \Baz(); - - $instance->setFoo($this->get('foo_with_inline')); - - return $instance; + return $this->services['baz'] = new \Baz(); } /** @@ -227,12 +223,11 @@ class ProjectServiceContainer extends Container protected function getFooWithInlineService() { $a = new \Bar(); - - $this->services['foo_with_inline'] = $instance = new \Foo(); - $a->pub = 'pub'; $a->setBaz($this->get('baz')); + $this->services['foo_with_inline'] = $instance = new \Foo(); + $instance->setBar($a); return $instance; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml index cba6814126..f2dadb4247 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml @@ -70,11 +70,7 @@ - - - - - + diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml index 84f62d25c0..a7a3234855 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml @@ -52,8 +52,6 @@ services: baz: class: Baz - calls: - - [setFoo, ['@foo_with_inline']] request: class: Request