ServiceReferenceGraph.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. class ServiceReferenceGraph
  21. {
  22. /**
  23. * @var ServiceReferenceGraphNode[]
  24. */
  25. private $nodes = array();
  26. /**
  27. * Checks if the graph has a specific node.
  28. *
  29. * @param string $id Id to check
  30. *
  31. * @return bool
  32. */
  33. public function hasNode($id)
  34. {
  35. return isset($this->nodes[$id]);
  36. }
  37. /**
  38. * Gets a node by identifier.
  39. *
  40. * @param string $id The id to retrieve
  41. *
  42. * @return ServiceReferenceGraphNode
  43. *
  44. * @throws InvalidArgumentException if no node matches the supplied identifier
  45. */
  46. public function getNode($id)
  47. {
  48. if (!isset($this->nodes[$id])) {
  49. throw new InvalidArgumentException(sprintf('There is no node with id "%s".', $id));
  50. }
  51. return $this->nodes[$id];
  52. }
  53. /**
  54. * Returns all nodes.
  55. *
  56. * @return ServiceReferenceGraphNode[]
  57. */
  58. public function getNodes()
  59. {
  60. return $this->nodes;
  61. }
  62. /**
  63. * Clears all nodes.
  64. */
  65. public function clear()
  66. {
  67. $this->nodes = array();
  68. }
  69. /**
  70. * Connects 2 nodes together in the Graph.
  71. *
  72. * @param string $sourceId
  73. * @param mixed $sourceValue
  74. * @param string $destId
  75. * @param mixed $destValue
  76. * @param string $reference
  77. */
  78. public function connect($sourceId, $sourceValue, $destId, $destValue = null, $reference = null)
  79. {
  80. if (null === $sourceId || null === $destId) {
  81. return;
  82. }
  83. $sourceNode = $this->createNode($sourceId, $sourceValue);
  84. $destNode = $this->createNode($destId, $destValue);
  85. $edge = new ServiceReferenceGraphEdge($sourceNode, $destNode, $reference);
  86. $sourceNode->addOutEdge($edge);
  87. $destNode->addInEdge($edge);
  88. }
  89. /**
  90. * Creates a graph node.
  91. *
  92. * @param string $id
  93. * @param mixed $value
  94. *
  95. * @return ServiceReferenceGraphNode
  96. */
  97. private function createNode($id, $value)
  98. {
  99. if (isset($this->nodes[$id]) && $this->nodes[$id]->getValue() === $value) {
  100. return $this->nodes[$id];
  101. }
  102. return $this->nodes[$id] = new ServiceReferenceGraphNode($id, $value);
  103. }
  104. }