|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Composer plugin for config assembling |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/composer-config-plugin |
|
6
|
|
|
* @package composer-config-plugin |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2016-2018, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\composer\config\utils; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\composer\config\exceptions\CircularDependencyException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Resolver class. |
|
17
|
|
|
* Reorders files according to their cross dependencies |
|
18
|
|
|
* and resolves `$name` pathes. |
|
19
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class Resolver |
|
22
|
|
|
{ |
|
23
|
|
|
protected $order = []; |
|
24
|
|
|
|
|
25
|
|
|
protected $deps = []; |
|
26
|
|
|
|
|
27
|
|
|
protected $following = []; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(array $files) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->files = $files; |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
$this->collectDeps(); |
|
34
|
|
|
foreach (array_keys($this->files) as $name) { |
|
35
|
|
|
$this->followDeps($name); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function get() |
|
40
|
|
|
{ |
|
41
|
|
|
$result = []; |
|
42
|
|
|
foreach ($this->order as $name) { |
|
43
|
|
|
$result[$name] = $this->resolveDeps($this->files[$name]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $result; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function resolveDeps(array $paths) |
|
50
|
|
|
{ |
|
51
|
|
|
foreach ($paths as &$path) { |
|
52
|
|
|
$dep = $this->isDep($path); |
|
53
|
|
|
if ($dep) { |
|
54
|
|
|
$path = Builder::path($dep); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $paths; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function followDeps($name) |
|
62
|
|
|
{ |
|
63
|
|
|
if (isset($this->order[$name])) { |
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
if (isset($this->following[$name])) { |
|
67
|
|
|
throw new CircularDependencyException($name . ' ' . implode(',', $this->following)); |
|
68
|
|
|
} |
|
69
|
|
|
$this->following[$name] = $name; |
|
70
|
|
|
if (isset($this->deps[$name])) { |
|
71
|
|
|
foreach ($this->deps[$name] as $dep) { |
|
72
|
|
|
$this->followDeps($dep); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
$this->order[$name] = $name; |
|
76
|
|
|
unset($this->following[$name]); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function collectDeps() |
|
80
|
|
|
{ |
|
81
|
|
|
foreach ($this->files as $name => $paths) { |
|
82
|
|
|
foreach ($paths as $path) { |
|
83
|
|
|
$dep = $this->isDep($path); |
|
84
|
|
|
if ($dep) { |
|
85
|
|
|
if (!isset($this->deps[$name])) { |
|
86
|
|
|
$this->deps[$name] = []; |
|
87
|
|
|
} |
|
88
|
|
|
$this->deps[$name][$dep] = $dep; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
protected function isDep($path) |
|
95
|
|
|
{ |
|
96
|
|
|
return 0 === strncmp($path, '$', 1) ? substr($path, 1) : false; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|