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

71 lines
2.0 KiB
PHP
Raw Normal View History

2011-01-17 22:28:59 +00:00
<?php
2011-05-31 09:57:06 +01:00
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
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;
/**
* 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
{
private $currentId;
private $currentPath;
2011-01-17 22:28:59 +00:00
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
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) {
$node = $edge->getDestNode();
$this->currentPath[] = $id = $node->getId();
if ($this->currentId === $id) {
throw new ServiceCircularReferenceException($this->currentId, $this->currentPath);
2011-01-17 22:28:59 +00:00
}
$this->checkOutEdges($node->getOutEdges());
array_pop($this->currentPath);
}
}
2011-06-08 11:16:48 +01:00
}