Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DependencyInjection] check for circular refs caused by method calls #21642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function __construct()
new RemoveUnusedDefinitionsPass(),
)),
new CheckExceptionOnInvalidReferenceBehaviorPass(),
new CheckCircularReferencesPass(),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,30 @@ public function testProcessInlinesWhenThereAreMultipleReferencesButFromTheSameDe
$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('bar', '\stdClass')
->addMethodCall('addFoobar', array(new Reference('foobar')))
;

$container->compile();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
;
$container
->register('baz', 'Baz')
->addMethodCall('setFoo', array(new Reference('foo_with_inline')))
;
$container
->register('request', 'Request')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@ protected function getBarService()
*/
protected function getBazService()
{
$this->services['baz'] = $instance = new \Baz();

$instance->setFoo($this->get('foo_with_inline'));

return $instance;
return $this->services['baz'] = new \Baz();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ protected function getBarService()
*/
protected function getBazService()
{
$this->services['baz'] = $instance = new \Baz();

$instance->setFoo($this->get('foo_with_inline'));

return $instance;
return $this->services['baz'] = new \Baz();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a case where the circular reference was working, as the service can be instantiated and registered in $this->services before the point triggering the call to the other service, meaning it will receive the instance.
Forbidding a case which was working previously is a BC break. So this is not acceptable, especially for a patch release

}

/**
Expand Down Expand Up @@ -227,12 +223,11 @@ protected function getFooBarService()
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@
<argument type="service" id="baz"/>
</call>
</service>
<service id="baz" class="Baz">
<call method="setFoo">
<argument type="service" id="foo_with_inline"/>
</call>
</service>
<service id="baz" class="Baz"/>
<service id="request" class="Request" synthetic="true"/>
<service id="configurator_service" class="ConfClass" public="false">
<call method="setFoo">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ services:

baz:
class: Baz
calls:
- [setFoo, ['@foo_with_inline']]

request:
class: Request
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.