check for circular refs caused by method calls

This commit is contained in:
Christian Flothmann 2017-01-27 16:27:34 +01:00
parent a35986f657
commit fe4f7eccf7
8 changed files with 32 additions and 22 deletions

View File

@ -63,6 +63,7 @@ class PassConfig
new RemoveUnusedDefinitionsPass(), new RemoveUnusedDefinitionsPass(),
)), )),
new CheckExceptionOnInvalidReferenceBehaviorPass(), new CheckExceptionOnInvalidReferenceBehaviorPass(),
new CheckCircularReferencesPass(),
); );
} }

View File

@ -113,4 +113,30 @@ class IntegrationTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($container->hasDefinition('b')); $this->assertFalse($container->hasDefinition('b'));
$this->assertFalse($container->hasDefinition('c'), 'Service C was not inlined.'); $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();
}
} }

View File

@ -64,7 +64,6 @@ $container
; ;
$container $container
->register('baz', 'Baz') ->register('baz', 'Baz')
->addMethodCall('setFoo', array(new Reference('foo_with_inline')))
; ;
$container $container
->register('request', 'Request') ->register('request', 'Request')

View File

@ -36,6 +36,5 @@ digraph sc {
node_method_call1 -> node_foobaz [label="setBar()" style="dashed"]; node_method_call1 -> node_foobaz [label="setBar()" style="dashed"];
node_foo_with_inline -> node_inlined [label="setBar()" style="dashed"]; node_foo_with_inline -> node_inlined [label="setBar()" style="dashed"];
node_inlined -> node_baz [label="setBaz()" 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"]; node_configurator_service -> node_baz [label="setFoo()" style="dashed"];
} }

View File

@ -80,11 +80,7 @@ class ProjectServiceContainer extends Container
*/ */
protected function getBazService() protected function getBazService()
{ {
$this->services['baz'] = $instance = new \Baz(); return $this->services['baz'] = new \Baz();
$instance->setFoo($this->get('foo_with_inline'));
return $instance;
} }
/** /**

View File

@ -99,11 +99,7 @@ class ProjectServiceContainer extends Container
*/ */
protected function getBazService() protected function getBazService()
{ {
$this->services['baz'] = $instance = new \Baz(); return $this->services['baz'] = new \Baz();
$instance->setFoo($this->get('foo_with_inline'));
return $instance;
} }
/** /**
@ -227,12 +223,11 @@ class ProjectServiceContainer extends Container
protected function getFooWithInlineService() protected function getFooWithInlineService()
{ {
$a = new \Bar(); $a = new \Bar();
$this->services['foo_with_inline'] = $instance = new \Foo();
$a->pub = 'pub'; $a->pub = 'pub';
$a->setBaz($this->get('baz')); $a->setBaz($this->get('baz'));
$this->services['foo_with_inline'] = $instance = new \Foo();
$instance->setBar($a); $instance->setBar($a);
return $instance; return $instance;

View File

@ -70,11 +70,7 @@
<argument type="service" id="baz"/> <argument type="service" id="baz"/>
</call> </call>
</service> </service>
<service id="baz" class="Baz"> <service id="baz" class="Baz"/>
<call method="setFoo">
<argument type="service" id="foo_with_inline"/>
</call>
</service>
<service id="request" class="Request" synthetic="true"/> <service id="request" class="Request" synthetic="true"/>
<service id="configurator_service" class="ConfClass" public="false"> <service id="configurator_service" class="ConfClass" public="false">
<call method="setFoo"> <call method="setFoo">

View File

@ -52,8 +52,6 @@ services:
baz: baz:
class: Baz class: Baz
calls:
- [setFoo, ['@foo_with_inline']]
request: request:
class: Request class: Request