4
4
5
5
namespace Doctrine \ORM \Internal ;
6
6
7
- use stdClass ;
7
+ use Doctrine \ORM \Internal \CommitOrder \Edge ;
8
+ use Doctrine \ORM \Internal \CommitOrder \Vertex ;
9
+ use Doctrine \ORM \Internal \CommitOrder \VertexState ;
10
+ use Doctrine \ORM \Mapping \ClassMetadata ;
8
11
9
12
use function array_reverse ;
10
13
17
20
*/
18
21
class CommitOrderCalculator
19
22
{
20
- public const NOT_VISITED = 0 ;
21
- public const IN_PROGRESS = 1 ;
22
- public const VISITED = 2 ;
23
+ /** @deprecated */
24
+ public const NOT_VISITED = VertexState::NOT_VISITED ;
25
+
26
+ /** @deprecated */
27
+ public const IN_PROGRESS = VertexState::IN_PROGRESS ;
28
+
29
+ /** @deprecated */
30
+ public const VISITED = VertexState::VISITED ;
23
31
24
32
/**
25
33
* Matrix of nodes (aka. vertex).
26
- * Keys are provided hashes and values are the node definition objects.
27
- *
28
- * The node state definition contains the following properties:
29
- *
30
- * - <b>state</b> (integer)
31
- * Whether the node is NOT_VISITED or IN_PROGRESS
32
- *
33
- * - <b>value</b> (object)
34
- * Actual node value
35
34
*
36
- * - <b>dependencyList</b> (array<string>)
37
- * Map of node dependencies defined as hashes.
35
+ * Keys are provided hashes and values are the node definition objects.
38
36
*
39
- * @var array<stdClass >
37
+ * @var array<string, Vertex >
40
38
*/
41
39
private $ nodeList = [];
42
40
43
41
/**
44
42
* Volatile variable holding calculated nodes during sorting process.
45
43
*
46
- * @psalm-var list<object >
44
+ * @psalm-var list<ClassMetadata >
47
45
*/
48
46
private $ sortedNodeList = [];
49
47
@@ -62,21 +60,14 @@ public function hasNode($hash)
62
60
/**
63
61
* Adds a new node (vertex) to the graph, assigning its hash and value.
64
62
*
65
- * @param string $hash
66
- * @param object $node
63
+ * @param string $hash
64
+ * @param ClassMetadata $node
67
65
*
68
66
* @return void
69
67
*/
70
68
public function addNode ($ hash , $ node )
71
69
{
72
- $ vertex = new stdClass ();
73
-
74
- $ vertex ->hash = $ hash ;
75
- $ vertex ->state = self ::NOT_VISITED ;
76
- $ vertex ->value = $ node ;
77
- $ vertex ->dependencyList = [];
78
-
79
- $ this ->nodeList [$ hash ] = $ vertex ;
70
+ $ this ->nodeList [$ hash ] = new Vertex ($ hash , $ node );
80
71
}
81
72
82
73
/**
@@ -90,14 +81,8 @@ public function addNode($hash, $node)
90
81
*/
91
82
public function addDependency ($ fromHash , $ toHash , $ weight )
92
83
{
93
- $ vertex = $ this ->nodeList [$ fromHash ];
94
- $ edge = new stdClass ();
95
-
96
- $ edge ->from = $ fromHash ;
97
- $ edge ->to = $ toHash ;
98
- $ edge ->weight = $ weight ;
99
-
100
- $ vertex ->dependencyList [$ toHash ] = $ edge ;
84
+ $ this ->nodeList [$ fromHash ]->dependencyList [$ toHash ]
85
+ = new Edge ($ fromHash , $ toHash , $ weight );
101
86
}
102
87
103
88
/**
@@ -106,12 +91,12 @@ public function addDependency($fromHash, $toHash, $weight)
106
91
*
107
92
* {@internal Highly performance-sensitive method.}
108
93
*
109
- * @psalm-return list<object >
94
+ * @psalm-return list<ClassMetadata >
110
95
*/
111
96
public function sort ()
112
97
{
113
98
foreach ($ this ->nodeList as $ vertex ) {
114
- if ($ vertex ->state !== self ::NOT_VISITED ) {
99
+ if ($ vertex ->state !== VertexState ::NOT_VISITED ) {
115
100
continue ;
116
101
}
117
102
@@ -131,19 +116,19 @@ public function sort()
131
116
*
132
117
* {@internal Highly performance-sensitive method.}
133
118
*/
134
- private function visit (stdClass $ vertex ): void
119
+ private function visit (Vertex $ vertex ): void
135
120
{
136
- $ vertex ->state = self ::IN_PROGRESS ;
121
+ $ vertex ->state = VertexState ::IN_PROGRESS ;
137
122
138
123
foreach ($ vertex ->dependencyList as $ edge ) {
139
124
$ adjacentVertex = $ this ->nodeList [$ edge ->to ];
140
125
141
126
switch ($ adjacentVertex ->state ) {
142
- case self ::VISITED :
127
+ case VertexState ::VISITED :
143
128
// Do nothing, since node was already visited
144
129
break ;
145
130
146
- case self ::IN_PROGRESS :
131
+ case VertexState ::IN_PROGRESS :
147
132
if (
148
133
isset ($ adjacentVertex ->dependencyList [$ vertex ->hash ]) &&
149
134
$ adjacentVertex ->dependencyList [$ vertex ->hash ]->weight < $ edge ->weight
@@ -153,25 +138,25 @@ private function visit(stdClass $vertex): void
153
138
foreach ($ adjacentVertex ->dependencyList as $ adjacentEdge ) {
154
139
$ adjacentEdgeVertex = $ this ->nodeList [$ adjacentEdge ->to ];
155
140
156
- if ($ adjacentEdgeVertex ->state === self ::NOT_VISITED ) {
141
+ if ($ adjacentEdgeVertex ->state === VertexState ::NOT_VISITED ) {
157
142
$ this ->visit ($ adjacentEdgeVertex );
158
143
}
159
144
}
160
145
161
- $ adjacentVertex ->state = self ::VISITED ;
146
+ $ adjacentVertex ->state = VertexState ::VISITED ;
162
147
163
148
$ this ->sortedNodeList [] = $ adjacentVertex ->value ;
164
149
}
165
150
166
151
break ;
167
152
168
- case self ::NOT_VISITED :
153
+ case VertexState ::NOT_VISITED :
169
154
$ this ->visit ($ adjacentVertex );
170
155
}
171
156
}
172
157
173
- if ($ vertex ->state !== self ::VISITED ) {
174
- $ vertex ->state = self ::VISITED ;
158
+ if ($ vertex ->state !== VertexState ::VISITED ) {
159
+ $ vertex ->state = VertexState ::VISITED ;
175
160
176
161
$ this ->sortedNodeList [] = $ vertex ->value ;
177
162
}
0 commit comments