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.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\ORM\Internal;
23 * The CommitOrderCalculator is used by the UnitOfWork to sort out the
24 * correct order in which changes to entities need to be persisted.
27 * @author Roman Borschel <roman@code-factory.org>
28 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
30 class CommitOrderCalculator
32 const NOT_VISITED = 1;
33 const IN_PROGRESS = 2;
36 private $_nodeStates = array();
37 private $_classes = array(); // The nodes to sort
38 private $_relatedClasses = array();
39 private $_sorted = array();
42 * Clears the current graph.
46 public function clear()
49 $this->_relatedClasses = array();
53 * Gets a valid commit order for all current nodes.
55 * Uses a depth-first search (DFS) to traverse the graph.
56 * The desired topological sorting is the reverse postorder of these searches.
58 * @return array The list of ordered classes.
60 public function getCommitOrder()
62 // Check whether we need to do anything. 0 or 1 node is easy.
63 $nodeCount = count($this->_classes);
65 if ($nodeCount <= 1) {
66 return ($nodeCount == 1) ? array_values($this->_classes) : array();
70 foreach ($this->_classes as $node) {
71 $this->_nodeStates[$node->name] = self::NOT_VISITED;
75 foreach ($this->_classes as $node) {
76 if ($this->_nodeStates[$node->name] == self::NOT_VISITED) {
77 $this->_visitNode($node);
81 $sorted = array_reverse($this->_sorted);
83 $this->_sorted = $this->_nodeStates = array();
88 private function _visitNode($node)
90 $this->_nodeStates[$node->name] = self::IN_PROGRESS;
92 if (isset($this->_relatedClasses[$node->name])) {
93 foreach ($this->_relatedClasses[$node->name] as $relatedNode) {
94 if ($this->_nodeStates[$relatedNode->name] == self::NOT_VISITED) {
95 $this->_visitNode($relatedNode);
100 $this->_nodeStates[$node->name] = self::VISITED;
101 $this->_sorted[] = $node;
104 public function addDependency($fromClass, $toClass)
106 $this->_relatedClasses[$fromClass->name][] = $toClass;
109 public function hasClass($className)
111 return isset($this->_classes[$className]);
114 public function addClass($class)
116 $this->_classes[$class->name] = $class;