Drupal investigation

ServiceReferenceGraph.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 string $sourceValue
  74. * @param string $destId
  75. * @param string $destValue
  76. * @param string $reference
  77. */
  78. public function connect($sourceId, $sourceValue, $destId, $destValue = null, $reference = null)
  79. {
  80. $sourceNode = $this->createNode($sourceId, $sourceValue);
  81. $destNode = $this->createNode($destId, $destValue);
  82. $edge = new ServiceReferenceGraphEdge($sourceNode, $destNode, $reference);
  83. $sourceNode->addOutEdge($edge);
  84. $destNode->addInEdge($edge);
  85. }
  86. /**
  87. * Creates a graph node.
  88. *
  89. * @param string $id
  90. * @param string $value
  91. *
  92. * @return ServiceReferenceGraphNode
  93. */
  94. private function createNode($id, $value)
  95. {
  96. if (isset($this->nodes[$id]) && $this->nodes[$id]->getValue() === $value) {
  97. return $this->nodes[$id];
  98. }
  99. return $this->nodes[$id] = new ServiceReferenceGraphNode($id, $value);
  100. }
  101. }