Drupal investigation

CheckCircularReferencesPass.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. /**
  14. * Checks your services for circular references.
  15. *
  16. * References from method calls are ignored since we might be able to resolve
  17. * these references depending on the order in which services are called.
  18. *
  19. * Circular reference from method calls will only be detected at run-time.
  20. *
  21. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  22. */
  23. class CheckCircularReferencesPass implements CompilerPassInterface
  24. {
  25. private $currentPath;
  26. private $checkedNodes;
  27. /**
  28. * Checks the ContainerBuilder object for circular references.
  29. *
  30. * @param ContainerBuilder $container The ContainerBuilder instances
  31. */
  32. public function process(ContainerBuilder $container)
  33. {
  34. $graph = $container->getCompiler()->getServiceReferenceGraph();
  35. $this->checkedNodes = array();
  36. foreach ($graph->getNodes() as $id => $node) {
  37. $this->currentPath = array($id);
  38. $this->checkOutEdges($node->getOutEdges());
  39. }
  40. }
  41. /**
  42. * Checks for circular references.
  43. *
  44. * @param ServiceReferenceGraphEdge[] $edges An array of Edges
  45. *
  46. * @throws ServiceCircularReferenceException When a circular reference is found.
  47. */
  48. private function checkOutEdges(array $edges)
  49. {
  50. foreach ($edges as $edge) {
  51. $node = $edge->getDestNode();
  52. $id = $node->getId();
  53. if (empty($this->checkedNodes[$id])) {
  54. // don't check circular dependencies for lazy services
  55. if (!$node->getValue() || !$node->getValue()->isLazy()) {
  56. $searchKey = array_search($id, $this->currentPath);
  57. $this->currentPath[] = $id;
  58. if (false !== $searchKey) {
  59. throw new ServiceCircularReferenceException($id, array_slice($this->currentPath, $searchKey));
  60. }
  61. $this->checkOutEdges($node->getOutEdges());
  62. }
  63. $this->checkedNodes[$id] = true;
  64. array_pop($this->currentPath);
  65. }
  66. }
  67. }
  68. }