|
0
|
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 |
|
|
|
20 |
namespace Doctrine\ORM\Internal; |
|
|
21 |
|
|
|
22 |
/** |
|
|
23 |
* The CommitOrderCalculator is used by the UnitOfWork to sort out the |
|
|
24 |
* correct order in which changes to entities need to be persisted. |
|
|
25 |
* |
|
|
26 |
* @since 2.0 |
|
|
27 |
* @author Roman Borschel <roman@code-factory.org> |
|
|
28 |
*/ |
|
|
29 |
class CommitOrderCalculator |
|
|
30 |
{ |
|
|
31 |
const NOT_VISITED = 1; |
|
|
32 |
const IN_PROGRESS = 2; |
|
|
33 |
const VISITED = 3; |
|
|
34 |
|
|
|
35 |
private $_nodeStates = array(); |
|
|
36 |
private $_classes = array(); // The nodes to sort |
|
|
37 |
private $_relatedClasses = array(); |
|
|
38 |
private $_sorted = array(); |
|
|
39 |
|
|
|
40 |
/** |
|
|
41 |
* Clears the current graph. |
|
|
42 |
* |
|
|
43 |
* @return void |
|
|
44 |
*/ |
|
|
45 |
public function clear() |
|
|
46 |
{ |
|
|
47 |
$this->_classes = |
|
|
48 |
$this->_relatedClasses = array(); |
|
|
49 |
} |
|
|
50 |
|
|
|
51 |
/** |
|
|
52 |
* Gets a valid commit order for all current nodes. |
|
|
53 |
* |
|
|
54 |
* Uses a depth-first search (DFS) to traverse the graph. |
|
|
55 |
* The desired topological sorting is the reverse postorder of these searches. |
|
|
56 |
* |
|
|
57 |
* @return array The list of ordered classes. |
|
|
58 |
*/ |
|
|
59 |
public function getCommitOrder() |
|
|
60 |
{ |
|
|
61 |
// Check whether we need to do anything. 0 or 1 node is easy. |
|
|
62 |
$nodeCount = count($this->_classes); |
|
|
63 |
if ($nodeCount == 0) { |
|
|
64 |
return array(); |
|
|
65 |
} else if ($nodeCount == 1) { |
|
|
66 |
return array_values($this->_classes); |
|
|
67 |
} |
|
|
68 |
|
|
|
69 |
// Init |
|
|
70 |
foreach ($this->_classes as $node) { |
|
|
71 |
$this->_nodeStates[$node->name] = self::NOT_VISITED; |
|
|
72 |
} |
|
|
73 |
|
|
|
74 |
// Go |
|
|
75 |
foreach ($this->_classes as $node) { |
|
|
76 |
if ($this->_nodeStates[$node->name] == self::NOT_VISITED) { |
|
|
77 |
$this->_visitNode($node); |
|
|
78 |
} |
|
|
79 |
} |
|
|
80 |
|
|
|
81 |
$sorted = array_reverse($this->_sorted); |
|
|
82 |
|
|
|
83 |
$this->_sorted = $this->_nodeStates = array(); |
|
|
84 |
|
|
|
85 |
return $sorted; |
|
|
86 |
} |
|
|
87 |
|
|
|
88 |
private function _visitNode($node) |
|
|
89 |
{ |
|
|
90 |
$this->_nodeStates[$node->name] = self::IN_PROGRESS; |
|
|
91 |
|
|
|
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); |
|
|
96 |
} |
|
|
97 |
} |
|
|
98 |
} |
|
|
99 |
|
|
|
100 |
$this->_nodeStates[$node->name] = self::VISITED; |
|
|
101 |
$this->_sorted[] = $node; |
|
|
102 |
} |
|
|
103 |
|
|
|
104 |
public function addDependency($fromClass, $toClass) |
|
|
105 |
{ |
|
|
106 |
$this->_relatedClasses[$fromClass->name][] = $toClass; |
|
|
107 |
} |
|
|
108 |
|
|
|
109 |
public function hasClass($className) |
|
|
110 |
{ |
|
|
111 |
return isset($this->_classes[$className]); |
|
|
112 |
} |
|
|
113 |
|
|
|
114 |
public function addClass($class) |
|
|
115 |
{ |
|
|
116 |
$this->_classes[$class->name] = $class; |
|
|
117 |
} |
|
|
118 |
} |