CommitOrderCalculator.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Internal;
  20. /**
  21. * The CommitOrderCalculator is used by the UnitOfWork to sort out the
  22. * correct order in which changes to entities need to be persisted.
  23. *
  24. * @since 2.0
  25. * @author Roman Borschel <roman@code-factory.org>
  26. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  27. */
  28. class CommitOrderCalculator
  29. {
  30. const NOT_VISITED = 1;
  31. const IN_PROGRESS = 2;
  32. const VISITED = 3;
  33. private $_nodeStates = array();
  34. private $_classes = array(); // The nodes to sort
  35. private $_relatedClasses = array();
  36. private $_sorted = array();
  37. /**
  38. * Clears the current graph.
  39. *
  40. * @return void
  41. */
  42. public function clear()
  43. {
  44. $this->_classes =
  45. $this->_relatedClasses = array();
  46. }
  47. /**
  48. * Gets a valid commit order for all current nodes.
  49. *
  50. * Uses a depth-first search (DFS) to traverse the graph.
  51. * The desired topological sorting is the reverse postorder of these searches.
  52. *
  53. * @return array The list of ordered classes.
  54. */
  55. public function getCommitOrder()
  56. {
  57. // Check whether we need to do anything. 0 or 1 node is easy.
  58. $nodeCount = count($this->_classes);
  59. if ($nodeCount <= 1) {
  60. return ($nodeCount == 1) ? array_values($this->_classes) : array();
  61. }
  62. // Init
  63. foreach ($this->_classes as $node) {
  64. $this->_nodeStates[$node->name] = self::NOT_VISITED;
  65. }
  66. // Go
  67. foreach ($this->_classes as $node) {
  68. if ($this->_nodeStates[$node->name] == self::NOT_VISITED) {
  69. $this->_visitNode($node);
  70. }
  71. }
  72. $sorted = array_reverse($this->_sorted);
  73. $this->_sorted = $this->_nodeStates = array();
  74. return $sorted;
  75. }
  76. private function _visitNode($node)
  77. {
  78. $this->_nodeStates[$node->name] = self::IN_PROGRESS;
  79. if (isset($this->_relatedClasses[$node->name])) {
  80. foreach ($this->_relatedClasses[$node->name] as $relatedNode) {
  81. if ($this->_nodeStates[$relatedNode->name] == self::NOT_VISITED) {
  82. $this->_visitNode($relatedNode);
  83. }
  84. }
  85. }
  86. $this->_nodeStates[$node->name] = self::VISITED;
  87. $this->_sorted[] = $node;
  88. }
  89. public function addDependency($fromClass, $toClass)
  90. {
  91. $this->_relatedClasses[$fromClass->name][] = $toClass;
  92. }
  93. public function hasClass($className)
  94. {
  95. return isset($this->_classes[$className]);
  96. }
  97. public function addClass($class)
  98. {
  99. $this->_classes[$class->name] = $class;
  100. }
  101. }