vendor/symfony/dependency-injection/Compiler/ServiceReferenceGraph.php line 77

Open in your IDE?
  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\InvalidArgumentException;
  12. use Symfony\Component\DependencyInjection\Reference;
  13. /**
  14.  * This is a directed graph of your services.
  15.  *
  16.  * This information can be used by your compiler passes instead of collecting
  17.  * it themselves which improves performance quite a lot.
  18.  *
  19.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  20.  *
  21.  * @final
  22.  */
  23. class ServiceReferenceGraph
  24. {
  25.     /**
  26.      * @var ServiceReferenceGraphNode[]
  27.      */
  28.     private $nodes = [];
  29.     public function hasNode(string $id): bool
  30.     {
  31.         return isset($this->nodes[$id]);
  32.     }
  33.     /**
  34.      * Gets a node by identifier.
  35.      *
  36.      * @throws InvalidArgumentException if no node matches the supplied identifier
  37.      */
  38.     public function getNode(string $id): ServiceReferenceGraphNode
  39.     {
  40.         if (!isset($this->nodes[$id])) {
  41.             throw new InvalidArgumentException(sprintf('There is no node with id "%s".'$id));
  42.         }
  43.         return $this->nodes[$id];
  44.     }
  45.     /**
  46.      * Returns all nodes.
  47.      *
  48.      * @return ServiceReferenceGraphNode[]
  49.      */
  50.     public function getNodes(): array
  51.     {
  52.         return $this->nodes;
  53.     }
  54.     /**
  55.      * Clears all nodes.
  56.      */
  57.     public function clear()
  58.     {
  59.         foreach ($this->nodes as $node) {
  60.             $node->clear();
  61.         }
  62.         $this->nodes = [];
  63.     }
  64.     /**
  65.      * Connects 2 nodes together in the Graph.
  66.      */
  67.     public function connect(?string $sourceId$sourceValue, ?string $destId$destValue nullReference $reference nullbool $lazy falsebool $weak falsebool $byConstructor false)
  68.     {
  69.         if (null === $sourceId || null === $destId) {
  70.             return;
  71.         }
  72.         $sourceNode $this->createNode($sourceId$sourceValue);
  73.         $destNode $this->createNode($destId$destValue);
  74.         $edge = new ServiceReferenceGraphEdge($sourceNode$destNode$reference$lazy$weak$byConstructor);
  75.         $sourceNode->addOutEdge($edge);
  76.         $destNode->addInEdge($edge);
  77.     }
  78.     private function createNode(string $id$value): ServiceReferenceGraphNode
  79.     {
  80.         if (isset($this->nodes[$id]) && $this->nodes[$id]->getValue() === $value) {
  81.             return $this->nodes[$id];
  82.         }
  83.         return $this->nodes[$id] = new ServiceReferenceGraphNode($id$value);
  84.     }
  85. }