ServiceReferenceGraph.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * @autor ThurData <info@thurdata.ch>
  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. * @param bool $lazy
  78. */
  79. public function connect($sourceId, $sourceValue, $destId, $destValue = null, $reference = null/*, bool $lazy = false*/)
  80. {
  81. if (func_num_args() >= 6) {
  82. $lazy = func_get_arg(5);
  83. } else {
  84. if (__CLASS__ !== get_class($this)) {
  85. $r = new \ReflectionMethod($this, __FUNCTION__);
  86. if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
  87. @trigger_error(sprintf('Method %s() will have a 6th `bool $lazy = false` argument in version 4.0. Not defining it is deprecated since Symfony 3.3.', __METHOD__), E_USER_DEPRECATED);
  88. }
  89. }
  90. $lazy = false;
  91. }
  92. if (null === $sourceId || null === $destId) {
  93. return;
  94. }
  95. $sourceNode = $this->createNode($sourceId, $sourceValue);
  96. $destNode = $this->createNode($destId, $destValue);
  97. $edge = new ServiceReferenceGraphEdge($sourceNode, $destNode, $reference, $lazy);
  98. $sourceNode->addOutEdge($edge);
  99. $destNode->addInEdge($edge);
  100. }
  101. /**
  102. * Creates a graph node.
  103. *
  104. * @param string $id
  105. * @param mixed $value
  106. *
  107. * @return ServiceReferenceGraphNode
  108. */
  109. private function createNode($id, $value)
  110. {
  111. if (isset($this->nodes[$id]) && $this->nodes[$id]->getValue() === $value) {
  112. return $this->nodes[$id];
  113. }
  114. return $this->nodes[$id] = new ServiceReferenceGraphNode($id, $value);
  115. }
  116. }