This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/Compiler/CheckCircularReferencesPass.php

60 lines
1.8 KiB
PHP
Raw Normal View History

2011-01-17 22:28:59 +00:00
<?php
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* Checks your services for circular references
*
* References from method calls are ignored since we might be able to resolve
* these references depending on the order in which services are called.
*
* Circular reference from method calls will only be detected at run-time.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class CheckCircularReferencesPass implements CompilerPassInterface
{
protected $currentId;
protected $currentNode;
protected $currentPath;
2011-02-13 18:06:41 +00:00
/**
* Checks the ContainerBuilder object for circular references.
*
* @param ContainerBuilder $container The ContainerBuilder instances
*/
2011-01-17 22:28:59 +00:00
public function process(ContainerBuilder $container)
{
$graph = $container->getCompiler()->getServiceReferenceGraph();
foreach ($graph->getNodes() as $id => $node) {
$this->currentId = $id;
$this->currentPath = array($id);
$this->checkOutEdges($node->getOutEdges());
}
}
2011-02-13 18:06:41 +00:00
/**
* Checks for circular references.
*
* @param array $edges An array of Nodes
* @throws \RuntimeException When a circular reference is found.
*/
2011-01-17 22:28:59 +00:00
protected function checkOutEdges(array $edges)
{
foreach ($edges as $edge) {
$node = $edge->getDestNode();
$this->currentPath[] = $id = $node->getId();
if ($this->currentId === $id) {
throw new \RuntimeException(sprintf('Circular reference detected for "%s", path: "%s".', $this->currentId, implode(' -> ', $this->currentPath)));
}
$this->checkOutEdges($node->getOutEdges());
array_pop($this->currentPath);
}
}
}