Skip to content

[DI] Detect circular references with ChildDefinition parent #28480

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

Merged
merged 1 commit into from
Sep 18, 2018
Merged
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 @@ -15,6 +15,7 @@
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\ExceptionInterface;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;

/**
* This replaces all ChildDefinition instances with their equivalent fully
Expand All @@ -25,6 +26,8 @@
*/
class ResolveChildDefinitionsPass extends AbstractRecursivePass
{
private $currentPath;

protected function processValue($value, $isRoot = false)
{
if (!$value instanceof Definition) {
Expand All @@ -36,6 +39,7 @@ protected function processValue($value, $isRoot = false)
$value = $this->container->getDefinition($this->currentId);
}
if ($value instanceof ChildDefinition) {
$this->currentPath = array();
$value = $this->resolveDefinition($value);
if ($isRoot) {
$this->container->setDefinition($this->currentId, $value);
Expand All @@ -56,6 +60,8 @@ private function resolveDefinition(ChildDefinition $definition)
{
try {
return $this->doResolveDefinition($definition);
} catch (ServiceCircularReferenceException $e) {
throw $e;
} catch (ExceptionInterface $e) {
$r = new \ReflectionProperty($e, 'message');
$r->setAccessible(true);
Expand All @@ -71,6 +77,13 @@ private function doResolveDefinition(ChildDefinition $definition)
throw new RuntimeException(sprintf('Parent definition "%s" does not exist.', $parent));
}

$searchKey = array_search($parent, $this->currentPath);
$this->currentPath[] = $parent;

if (false !== $searchKey) {
throw new ServiceCircularReferenceException($parent, \array_slice($this->currentPath, $searchKey));
}

$parentDef = $this->container->findDefinition($parent);
if ($parentDef instanceof ChildDefinition) {
$id = $this->currentId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,4 +431,21 @@ protected function process(ContainerBuilder $container)
$pass = new ResolveChildDefinitionsPass();
$pass->process($container);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
Copy link
Member

Choose a reason for hiding this comment

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

could be worth adding an @expectedExceptionMessage: right now, I get: Service "a": Service "a": Service "a": Service "a": Circular reference detected for service "c", path: "c -> b -> a -> c"., which might need some tweaks :)

Copy link
Member

Choose a reason for hiding this comment

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

hmm, that's resolveDefinition prefixing the exception message. And the multiple prefixing looks really weird though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oups, didn't see that. I just pushed a fix.

Copy link
Member

Choose a reason for hiding this comment

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

still worth adding @expectedExceptionMessage :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe @expectedExceptionMessageRegExp to test if exception message is not prefixed?

* @expectedExceptionMessageRegExp /^Circular reference detected for service "c", path: "c -> b -> a -> c"./
*/
public function testProcessDetectsChildDefinitionIndirectCircularReference()
{
$container = new ContainerBuilder();

$container->register('a');

$container->setDefinition('b', new ChildDefinition('a'));
$container->setDefinition('c', new ChildDefinition('b'));
$container->setDefinition('a', new ChildDefinition('c'));

$this->process($container);
}
}