|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Composer\Config\Util; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Composer\Config\Builder; |
|
8
|
|
|
use Yiisoft\Composer\Config\Exception\CircularDependencyException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Resolver class. |
|
12
|
|
|
* Reorders files according to their cross dependencies |
|
13
|
|
|
* and resolves `$name` paths. |
|
14
|
|
|
*/ |
|
15
|
|
|
class Resolver |
|
16
|
|
|
{ |
|
17
|
|
|
private array $dependenciesOrder = []; |
|
18
|
|
|
|
|
19
|
|
|
private array $dependencies = []; |
|
20
|
|
|
|
|
21
|
|
|
private array $following = []; |
|
22
|
|
|
|
|
23
|
|
|
private array $files; |
|
24
|
|
|
|
|
25
|
2 |
|
public function __construct(array $files) |
|
26
|
|
|
{ |
|
27
|
2 |
|
$this->files = $files; |
|
28
|
|
|
|
|
29
|
2 |
|
$this->collectDependencies($files); |
|
30
|
2 |
|
foreach (array_keys($files) as $name) { |
|
31
|
2 |
|
$this->followDependencies($name); |
|
32
|
|
|
} |
|
33
|
2 |
|
} |
|
34
|
|
|
|
|
35
|
2 |
|
public function get(): array |
|
36
|
|
|
{ |
|
37
|
2 |
|
$result = []; |
|
38
|
2 |
|
foreach ($this->dependenciesOrder as $name) { |
|
39
|
2 |
|
$result[$name] = $this->resolveDependencies($this->files[$name]); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
2 |
|
return $result; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
2 |
|
private function resolveDependencies(array $paths): array |
|
46
|
|
|
{ |
|
47
|
2 |
|
foreach ($paths as &$path) { |
|
48
|
2 |
|
if ($this->isDependency($path)) { |
|
49
|
|
|
$dependency = $this->parseDependencyName($path); |
|
50
|
|
|
|
|
51
|
|
|
$path = Builder::path($dependency); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
return $paths; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
private function followDependencies(string $name): void |
|
59
|
|
|
{ |
|
60
|
2 |
|
if (array_key_exists($name, $this->dependenciesOrder)) { |
|
61
|
|
|
return; |
|
62
|
|
|
} |
|
63
|
2 |
|
if (array_key_exists($name, $this->following)) { |
|
64
|
|
|
throw new CircularDependencyException($name . ' ' . implode(',', $this->following)); |
|
65
|
|
|
} |
|
66
|
2 |
|
$this->following[$name] = $name; |
|
67
|
2 |
|
if (array_key_exists($name, $this->dependencies)) { |
|
68
|
|
|
foreach ($this->dependencies[$name] as $dependency) { |
|
69
|
|
|
$this->followDependencies($dependency); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
2 |
|
$this->dependenciesOrder[$name] = $name; |
|
73
|
2 |
|
unset($this->following[$name]); |
|
74
|
2 |
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
private function collectDependencies(array $files): void |
|
77
|
|
|
{ |
|
78
|
2 |
|
foreach ($files as $name => $paths) { |
|
79
|
2 |
|
foreach ($paths as $path) { |
|
80
|
2 |
|
if ($this->isDependency($path)) { |
|
81
|
|
|
$dependencyName = $this->parseDependencyName($path); |
|
82
|
|
|
if (!array_key_exists($name, $this->dependencies)) { |
|
83
|
|
|
$this->dependencies[$name] = []; |
|
84
|
|
|
} |
|
85
|
|
|
$this->dependencies[$name][$dependencyName] = $dependencyName; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
2 |
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
private function isDependency(string $path): bool |
|
92
|
|
|
{ |
|
93
|
2 |
|
return 0 === strncmp($path, '$', 1); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
private function parseDependencyName(string $path): string |
|
97
|
|
|
{ |
|
98
|
|
|
return substr($path, 1); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|