Skip to content

[DependencyInjection] Adding an exception on circular reference #28452

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 @@ -27,6 +27,7 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\Compiler\ResolveEnvPlaceholdersPass;
use Symfony\Component\DependencyInjection\Exception\ArgumentCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
Expand Down Expand Up @@ -1017,6 +1018,10 @@ public function setDefinition($id, Definition $definition)
throw new BadMethodCallException('Adding definition to a compiled container is not allowed');
}

if ($definition instanceof ChildDefinition && $id === $definition->getParent()) {
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest moving the logic to ResolveChildDefinitionsPass.
Also, can't the loop involve intermediary ids? A => B => C => A?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your feedback.
I can move it to ResolveInstanceofConditionalsPass (here), but I think it's not a good option too...
Currently I can't find the way to move it to ResolveChildDefinitionsPass. If you have an idea...

throw new ArgumentCircularReferenceException($definition->getParent(), $id);
}

$id = $this->normalizeId($id);

unset($this->aliasDefinitions[$id], $this->removedIds[$id]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Exception;

/**
* This exception is thrown when a circular reference is detected between services in arguments.
*
* @author Nicolas LEFEVRE <weblefevre@gmail.com>
*/
class ArgumentCircularReferenceException extends RuntimeException
Copy link
Member

Choose a reason for hiding this comment

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

if we want this to make it into a bug fix release, we should not add any new class in the PR (otherwise that'd be a smell for a new feature).
Instead, what about throwing a ServiceCircularReferenceException?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, throwing ServiceCircularReferenceException should be a good idea...

{
private $argument;
private $serviceId;

public function __construct($argument, $serviceId, \Exception $previous = null)
{
parent::__construct(sprintf('Circular reference detected between service "%s" and parent argument "%s". A "ChildDefinition" can\'t references itself as parent.', $argument, $serviceId), 0, $previous);

$this->argument = $argument;
$this->serviceId = $serviceId;
}

public function getArgument()
{
return $this->argument;
}

public function getServiceId()
{
return $this->serviceId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
use Symfony\Component\DependencyInjection\Compiler\CheckCircularReferencesPass;
use Symfony\Component\DependencyInjection\Compiler\Compiler;
Expand Down Expand Up @@ -108,6 +109,22 @@ public function testDeepCircularReference()
$this->process($container);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\ArgumentCircularReferenceException
*/
public function testProcessDetectCircularReferenceWhenChildDefinitionReferencesItselfAsParent()
{
$container = new ContainerBuilder();

$container->register('a');

$definition = new ChildDefinition('a');
$definition->setClass('b');
$container->setDefinition('a', $definition);

$this->process($container);
}

public function testProcessIgnoresMethodCalls()
{
$container = new ContainerBuilder();
Expand Down