CommitOrderCalculator.php 3.6KB

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. */
  27. class CommitOrderCalculator
  28. {
  29. const NOT_VISITED = 1;
  30. const IN_PROGRESS = 2;
  31. const VISITED = 3;
  32. private $_nodeStates = array();
  33. private $_classes = array(); // The nodes to sort
  34. private $_relatedClasses = array();
  35. private $_sorted = array();
  36. /**
  37. * Clears the current graph.
  38. *
  39. * @return void
  40. */
  41. public function clear()
  42. {
  43. $this->_classes =
  44. $this->_relatedClasses = array();
  45. }
  46. /**
  47. * Gets a valid commit order for all current nodes.
  48. *
  49. * Uses a depth-first search (DFS) to traverse the graph.
  50. * The desired topological sorting is the reverse postorder of these searches.
  51. *
  52. * @return array The list of ordered classes.
  53. */
  54. public function getCommitOrder()
  55. {
  56. // Check whether we need to do anything. 0 or 1 node is easy.
  57. $nodeCount = count($this->_classes);
  58. if ($nodeCount == 0) {
  59. return array();
  60. } else if ($nodeCount == 1) {
  61. return array_values($this->_classes);
  62. }
  63. // Init
  64. foreach ($this->_classes as $node) {
  65. $this->_nodeStates[$node->name] = self::NOT_VISITED;
  66. }
  67. // Go
  68. foreach ($this->_classes as $node) {
  69. if ($this->_nodeStates[$node->name] == self::NOT_VISITED) {
  70. $this->_visitNode($node);
  71. }
  72. }
  73. $sorted = array_reverse($this->_sorted);
  74. $this->_sorted = $this->_nodeStates = array();
  75. return $sorted;
  76. }
  77. private function _visitNode($node)
  78. {
  79. $this->_nodeStates[$node->name] = self::IN_PROGRESS;
  80. if (isset($this->_relatedClasses[$node->name])) {
  81. foreach ($this->_relatedClasses[$node->name] as $relatedNode) {
  82. if ($this->_nodeStates[$relatedNode->name] == self::NOT_VISITED) {
  83. $this->_visitNode($relatedNode);
  84. }
  85. }
  86. }
  87. $this->_nodeStates[$node->name] = self::VISITED;
  88. $this->_sorted[] = $node;
  89. }
  90. public function addDependency($fromClass, $toClass)
  91. {
  92. $this->_relatedClasses[$fromClass->name][] = $toClass;
  93. }
  94. public function hasClass($className)
  95. {
  96. return isset($this->_classes[$className]);
  97. }
  98. public function addClass($class)
  99. {
  100. $this->_classes[$class->name] = $class;
  101. }
  102. }