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
|
20 |
|
public function __construct(Composer $composer) |
28
|
|
|
{ |
29
|
20 |
|
$this->composer = $composer; |
30
|
20 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $key |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
20 |
|
public function getConfig($key) |
38
|
|
|
{ |
39
|
20 |
|
$this->locate($key); |
40
|
|
|
|
41
|
20 |
|
return $this->cache[$key]['config']; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $key |
46
|
|
|
* |
47
|
|
|
* @return string|null mixed |
48
|
|
|
*/ |
49
|
20 |
|
public function getPath($key) |
50
|
|
|
{ |
51
|
20 |
|
$this->locate($key); |
52
|
|
|
|
53
|
20 |
|
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
|
20 |
|
public function locate($key) |
64
|
|
|
{ |
65
|
20 |
|
if (array_key_exists($key, $this->cache)) { |
66
|
20 |
|
return $this->cache[$key]['found']; |
67
|
|
|
} |
68
|
|
|
|
69
|
20 |
|
if ($this->locateLocal($key)) { |
70
|
2 |
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
18 |
|
if ($this->locateGlobal($key)) { |
74
|
10 |
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
8 |
|
$this->cache[$key] = [ |
78
|
8 |
|
'found' => false, |
79
|
8 |
|
'config' => [], |
80
|
8 |
|
'path' => null, |
81
|
|
|
]; |
82
|
|
|
|
83
|
8 |
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Search config in the local root package. |
88
|
|
|
* |
89
|
|
|
* @param string $key |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
20 |
|
private function locateLocal($key) |
94
|
|
|
{ |
95
|
20 |
|
$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
|
20 |
|
$reflection = new \ReflectionClass($composerConfig); |
99
|
20 |
|
$property = $reflection->getProperty('baseDir'); |
100
|
20 |
|
$property->setAccessible(true); |
101
|
|
|
|
102
|
20 |
|
$path = $property->getValue($composerConfig); |
103
|
|
|
|
104
|
20 |
|
$localComposerExtra = $this->composer->getPackage()->getExtra(); |
105
|
|
|
|
106
|
20 |
|
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
|
18 |
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Search config in the global root package. |
121
|
|
|
* |
122
|
|
|
* @param string $key |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
18 |
|
private function locateGlobal($key) |
127
|
|
|
{ |
128
|
18 |
|
$path = $this->composer->getConfig()->get('home'); |
129
|
|
|
|
130
|
18 |
|
$globalComposerJsonFile = $path . '/composer.json'; |
131
|
|
|
|
132
|
18 |
|
if (file_exists($globalComposerJsonFile)) { |
133
|
12 |
|
$globalComposerJson = json_decode(file_get_contents($globalComposerJsonFile), true); |
134
|
|
|
|
135
|
12 |
|
if (array_key_exists('extra', $globalComposerJson) && array_key_exists($key, $globalComposerJson['extra'])) { |
136
|
10 |
|
$this->cache[$key] = [ |
137
|
10 |
|
'found' => true, |
138
|
10 |
|
'config' => $globalComposerJson['extra'][$key], |
139
|
10 |
|
'path' => $path, |
140
|
|
|
]; |
141
|
|
|
|
142
|
10 |
|
return true; |
143
|
|
|
} |
144
|
2 |
|
} |
145
|
|
|
|
146
|
8 |
|
return false; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|