ServiceReferenceGraph.php 2.9 KB

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