-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependencyGraph.php
More file actions
173 lines (148 loc) · 4.84 KB
/
DependencyGraph.php
File metadata and controls
173 lines (148 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
namespace Toppy\AsyncViewModel;
/**
* Directed Acyclic Graph for ViewModel dependency resolution.
*
* Provides topological ordering with priority given to nodes
* that have the most dependents (they should start first).
*/
final class DependencyGraph
{
/** @var array<string, list<string>> node -> dependencies */
private array $edges = [];
/** @var array<string, int> node -> count of direct dependents */
private array $dependentCount = [];
/**
* Add a node with its dependencies.
*
* @param string $class The ViewModel class name
* @param list<string> $dependencies Classes this node depends on
*/
public function addNode(string $class, array $dependencies): void
{
$this->edges[$class] = $dependencies;
// Initialize dependent count for this node if not set
if (!isset($this->dependentCount[$class])) {
$this->dependentCount[$class] = 0;
}
// Increment dependent count for each dependency
foreach ($dependencies as $dep) {
// Auto-add unknown dependencies with no deps
if (!isset($this->edges[$dep])) {
$this->edges[$dep] = [];
}
if (!isset($this->dependentCount[$dep])) {
$this->dependentCount[$dep] = 0;
}
$this->dependentCount[$dep]++;
}
}
/**
* Get nodes in start order: most dependents first.
*
* Uses topological sort with priority by dependent count.
*
* @return list<string>
*/
public function getStartOrder(): array
{
if ($this->edges === []) {
return [];
}
// Calculate transitive dependent counts using reverse topological order
$transitiveCounts = $this->calculateTransitiveDependentCounts();
// Sort by transitive dependent count (descending)
$nodes = array_keys($this->edges);
usort($nodes, static fn($a, $b) => $transitiveCounts[$b] <=> $transitiveCounts[$a]);
return $nodes;
}
/**
* Detect cycles in the graph.
*
* @throws \LogicException if a cycle is detected
*/
public function detectCycle(): void
{
$visited = [];
$recursionStack = [];
foreach (array_keys($this->edges) as $node) {
if ($this->hasCycle($node, $visited, $recursionStack)) {
throw new \LogicException(sprintf('Circular ViewModel dependency detected: %s', implode(
' -> ',
$recursionStack,
)));
}
}
}
/**
* @param array<string, bool> $visited
* @param list<string> $recursionStack
*/
private function hasCycle(string $node, array &$visited, array &$recursionStack): bool
{
if (in_array($node, $recursionStack, strict: true)) {
$recursionStack[] = $node; // Add to show the cycle
return true;
}
if (isset($visited[$node])) {
return false;
}
$visited[$node] = true;
$recursionStack[] = $node;
foreach ($this->edges[$node] ?? [] as $dep) {
if ($this->hasCycle($dep, $visited, $recursionStack)) {
return true;
}
}
array_pop($recursionStack);
return false;
}
/**
* Calculate transitive dependent count for each node.
*
* A node's transitive count = direct dependents + all their transitive dependents.
*
* @return array<string, int>
*/
private function calculateTransitiveDependentCounts(): array
{
// Build reverse graph (dependents instead of dependencies)
$reverseDeps = [];
foreach ($this->edges as $node => $deps) {
$reverseDeps[$node] ??= [];
foreach ($deps as $dep) {
$reverseDeps[$dep][] = $node;
}
}
// For each node, count all transitive dependents via BFS
$counts = [];
foreach (array_keys($this->edges) as $node) {
$counts[$node] = $this->countTransitiveDependents($node, $reverseDeps);
}
return $counts;
}
/**
* @param array<string, list<string>> $reverseDeps
*/
private function countTransitiveDependents(string $node, array $reverseDeps): int
{
$visited = [];
$queue = $reverseDeps[$node] ?? [];
$count = 0;
while ($queue !== []) {
$current = array_shift($queue);
if (isset($visited[$current])) {
continue;
}
$visited[$current] = true;
$count++;
foreach ($reverseDeps[$current] ?? [] as $dependent) {
if (!isset($visited[$dependent])) {
$queue[] = $dependent;
}
}
}
return $count;
}
}