ServiceReferenceGraph.php 2.5 KB

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