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

79 lines
2.4 KiB
PHP
Raw Normal View History

2011-01-17 22:28:59 +00:00
<?php
2011-05-31 09:57:06 +01:00
/*
2012-03-31 22:00:32 +01:00
* This file is part of the Symfony package.
2011-05-31 09:57:06 +01:00
*
* (c) Fabien Potencier <fabien@symfony.com>
*
2012-03-31 22:00:32 +01:00
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2011-05-31 09:57:06 +01:00
*/
2011-01-17 22:28:59 +00:00
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
2011-01-17 22:28:59 +00:00
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
2014-12-21 17:00:50 +00:00
* Checks your services for circular references.
2011-01-17 22:28:59 +00:00
*
* 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
{
private $currentPath;
2013-08-30 11:51:09 +01:00
private $checkedNodes;
2011-01-17 22:28:59 +00:00
2011-02-13 18:06:41 +00:00
/**
* Checks the ContainerBuilder object for circular references.
*/
2011-01-17 22:28:59 +00:00
public function process(ContainerBuilder $container)
{
$graph = $container->getCompiler()->getServiceReferenceGraph();
2013-08-30 11:51:09 +01:00
$this->checkedNodes = array();
2011-01-17 22:28:59 +00:00
foreach ($graph->getNodes() as $id => $node) {
$this->currentPath = array($id);
$this->checkOutEdges($node->getOutEdges());
}
}
2011-02-13 18:06:41 +00:00
/**
* Checks for circular references.
*
* @param ServiceReferenceGraphEdge[] $edges An array of Edges
2011-12-13 07:50:54 +00:00
*
* @throws ServiceCircularReferenceException when a circular reference is found
2011-02-13 18:06:41 +00:00
*/
private function checkOutEdges(array $edges)
2011-01-17 22:28:59 +00:00
{
foreach ($edges as $edge) {
2014-10-22 19:27:13 +01:00
$node = $edge->getDestNode();
$id = $node->getId();
2011-01-17 22:28:59 +00:00
2013-08-30 11:51:09 +01:00
if (empty($this->checkedNodes[$id])) {
// Don't check circular references for lazy edges
if (!$node->getValue() || (!$edge->isLazy() && !$edge->isWeak())) {
$searchKey = array_search($id, $this->currentPath);
$this->currentPath[] = $id;
if (false !== $searchKey) {
throw new ServiceCircularReferenceException($id, array_slice($this->currentPath, $searchKey));
}
2013-08-30 11:51:09 +01:00
$this->checkOutEdges($node->getOutEdges());
}
2013-08-30 11:51:09 +01:00
$this->checkedNodes[$id] = true;
array_pop($this->currentPath);
}
2011-01-17 22:28:59 +00:00
}
}
2011-06-08 11:16:48 +01:00
}