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;
|
|
|
|
|
2011-05-25 08:46:30 +01:00
|
|
|
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
|
|
|
|
{
|
2011-03-11 13:50:46 +00:00
|
|
|
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
|
|
|
*
|
2011-12-04 23:57:59 +00:00
|
|
|
* @throws ServiceCircularReferenceException When a circular reference is found.
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-03-11 13:50:46 +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) {
|
2011-05-25 08:46:30 +01:00
|
|
|
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
|
|
|
}
|