|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the composer-changelogs project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Loïck Piera <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Pyrech\ComposerChangelogs\Config; |
|
13
|
|
|
|
|
14
|
|
|
use Composer\Composer; |
|
15
|
|
|
|
|
16
|
|
|
class ConfigLocator |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var Composer */ |
|
19
|
|
|
private $composer; |
|
20
|
|
|
|
|
21
|
|
|
/** @var array */ |
|
22
|
|
|
public $cache = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param Composer $composer |
|
26
|
|
|
*/ |
|
27
|
12 |
|
public function __construct(Composer $composer) |
|
28
|
|
|
{ |
|
29
|
12 |
|
$this->composer = $composer; |
|
30
|
12 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $key |
|
34
|
|
|
* |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
12 |
|
public function getConfig($key) |
|
38
|
|
|
{ |
|
39
|
12 |
|
$this->locate($key); |
|
40
|
|
|
|
|
41
|
12 |
|
return $this->cache[$key]['config']; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string $key |
|
46
|
|
|
* |
|
47
|
|
|
* @return string|null mixed |
|
48
|
|
|
*/ |
|
49
|
12 |
|
public function getPath($key) |
|
50
|
|
|
{ |
|
51
|
12 |
|
$this->locate($key); |
|
52
|
|
|
|
|
53
|
12 |
|
return $this->cache[$key]['path']; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Try to locate where is the config for the given key. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $key |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
12 |
|
public function locate($key) |
|
64
|
|
|
{ |
|
65
|
12 |
|
if (array_key_exists($key, $this->cache)) { |
|
66
|
12 |
|
return $this->cache[$key]['found']; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
12 |
|
if ($this->locateLocal($key)) { |
|
70
|
2 |
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
10 |
|
if ($this->locateGlobal($key)) { |
|
74
|
4 |
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
6 |
|
$this->cache[$key] = [ |
|
78
|
6 |
|
'found' => false, |
|
79
|
6 |
|
'config' => [], |
|
80
|
6 |
|
'path' => null, |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
6 |
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Search config in the local root package. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $key |
|
90
|
|
|
* |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
12 |
|
private function locateLocal($key) |
|
94
|
|
|
{ |
|
95
|
12 |
|
$composerConfig = $this->composer->getConfig(); |
|
96
|
|
|
|
|
97
|
|
|
// Sorry for this, I couldn't find any way to get the path of the current root package |
|
98
|
12 |
|
$reflection = new \ReflectionClass($composerConfig); |
|
99
|
12 |
|
$property = $reflection->getProperty('baseDir'); |
|
100
|
12 |
|
$property->setAccessible(true); |
|
101
|
|
|
|
|
102
|
12 |
|
$path = $property->getValue($composerConfig); |
|
103
|
|
|
|
|
104
|
12 |
|
$localComposerExtra = $this->composer->getPackage()->getExtra(); |
|
105
|
|
|
|
|
106
|
12 |
|
if (array_key_exists($key, $localComposerExtra)) { |
|
107
|
2 |
|
$this->cache[$key] = [ |
|
108
|
2 |
|
'found' => true, |
|
109
|
2 |
|
'config' => $localComposerExtra[$key], |
|
110
|
2 |
|
'path' => $path, |
|
111
|
|
|
]; |
|
112
|
|
|
|
|
113
|
2 |
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
10 |
|
return false; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Search config in the global root package. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $key |
|
123
|
|
|
* |
|
124
|
|
|
* @return bool |
|
125
|
|
|
*/ |
|
126
|
10 |
|
private function locateGlobal($key) |
|
127
|
|
|
{ |
|
128
|
10 |
|
$path = $this->composer->getConfig()->get('home'); |
|
129
|
|
|
|
|
130
|
10 |
|
$globalComposerJsonFile = $path . '/composer.json'; |
|
131
|
|
|
|
|
132
|
10 |
|
if (file_exists($globalComposerJsonFile)) { |
|
133
|
6 |
|
$globalComposerJson = json_decode(file_get_contents($globalComposerJsonFile), true); |
|
134
|
|
|
|
|
135
|
6 |
|
if (array_key_exists('extra', $globalComposerJson) && array_key_exists($key, $globalComposerJson['extra'])) { |
|
136
|
4 |
|
$this->cache[$key] = [ |
|
137
|
4 |
|
'found' => true, |
|
138
|
4 |
|
'config' => $globalComposerJson['extra'][$key], |
|
139
|
4 |
|
'path' => $path, |
|
140
|
|
|
]; |
|
141
|
|
|
|
|
142
|
4 |
|
return true; |
|
143
|
|
|
} |
|
144
|
2 |
|
} |
|
145
|
|
|
|
|
146
|
6 |
|
return false; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|