1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase |
2
|
|
|
/** |
3
|
|
|
* Configuration loader for the changelogger tool. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-changelogger |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
// phpcs:disable WordPress.NamingConventions.ValidFunctionName, WordPress.NamingConventions.ValidVariableName, WordPress.WP.AlternativeFunctions |
9
|
|
|
|
10
|
|
|
namespace Automattic\Jetpack\Changelogger; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Configuration loader for the changelogger tool. |
16
|
|
|
*/ |
17
|
|
|
class Config { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Default config settings. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private static $defaultConfig = array( |
25
|
|
|
'changelog' => 'CHANGELOG.md', |
26
|
|
|
'changes-dir' => 'changelog', |
27
|
|
|
'link-template' => null, |
28
|
|
|
'ordering' => array( 'subheading', 'content' ), |
29
|
|
|
'formatter' => 'keepachangelog', |
30
|
|
|
'types' => array( |
31
|
|
|
'security' => 'Security', |
32
|
|
|
'added' => 'Added', |
33
|
|
|
'changed' => 'Changed', |
34
|
|
|
'deprecated' => 'Deprecated', |
35
|
|
|
'removed' => 'Removed', |
36
|
|
|
'fixed' => 'Fixed', |
37
|
|
|
), |
38
|
|
|
'versioning' => 'semver', |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Active config settings. |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private static $config = array(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Cached config settings. |
50
|
|
|
* |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
private static $cache = array(); |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Whether `load()` was called already. |
57
|
|
|
* |
58
|
|
|
* @var bool |
59
|
|
|
*/ |
60
|
|
|
private static $loaded = false; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* OutputInterface. |
64
|
|
|
* |
65
|
|
|
* @var OutputInterface|null |
66
|
|
|
*/ |
67
|
|
|
private static $out; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set the OutputInterface. |
71
|
|
|
* |
72
|
|
|
* @param OutputInterface $out OutputInterface. |
73
|
|
|
*/ |
74
|
|
|
public static function setOutput( OutputInterface $out ) { |
75
|
|
|
self::$out = $out; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Load the configuration. |
80
|
|
|
* |
81
|
|
|
* @throws \LogicException If called before `setOutput()`. |
82
|
|
|
* @throws \DomainException If the path to composer.json exists but can't be `realpath`-ed. |
83
|
|
|
*/ |
84
|
|
|
private static function load() { |
85
|
|
|
if ( ! self::$out ) { |
86
|
|
|
throw new \LogicException( 'Must call Config::setOutput() before Config::load()' ); |
87
|
|
|
} |
88
|
|
|
if ( self::$loaded ) { |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
self::$loaded = true; |
92
|
|
|
|
93
|
|
|
self::$config = self::$defaultConfig; |
94
|
|
|
self::$config['base'] = getcwd(); |
95
|
|
|
|
96
|
|
|
$composer = getenv( 'COMPOSER' ); |
97
|
|
|
if ( $composer ) { |
98
|
|
|
$from = ' (as specified by the COMPOSER environment variable)'; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
99
|
|
|
} else { |
100
|
|
|
$composer = 'composer.json'; |
101
|
|
|
$from = ''; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
102
|
|
|
} |
103
|
|
|
if ( ! file_exists( $composer ) ) { |
104
|
|
|
self::$out->writeln( "<error>File {$composer}{$from} is not found.</>" ); |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
$data = json_decode( file_get_contents( $composer ), true ); |
108
|
|
|
if ( ! is_array( $data ) ) { |
109
|
|
|
self::$out->writeln( "<error>File {$composer}{$from} could not be parsed.</>" ); |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$dir = realpath( $composer ); |
114
|
|
|
if ( false === $dir ) { |
115
|
|
|
throw new \DomainException( "Path $composer is not valid" ); // @codeCoverageIgnore |
116
|
|
|
} |
117
|
|
|
self::$config['base'] = dirname( $dir ); |
118
|
|
|
if ( isset( $data['extra']['changelogger'] ) ) { |
119
|
|
|
self::$config = array_merge( self::$config, $data['extra']['changelogger'] ); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the base directory. |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public static function base() { |
129
|
|
|
self::load(); |
130
|
|
|
return self::$config['base']; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Add the base directory to a path, if necessary. |
135
|
|
|
* |
136
|
|
|
* @param string $path Path. |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
private static function addBase( $path ) { |
140
|
|
|
// Stupid Windows requires a regex. |
141
|
|
|
if ( ! preg_match( '#^(?:/|' . preg_quote( DIRECTORY_SEPARATOR, '#' ) . '|[a-zA-Z]:\\\\)#', $path ) ) { |
142
|
|
|
$path = self::base() . DIRECTORY_SEPARATOR . $path; |
143
|
|
|
} |
144
|
|
|
return $path; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the changelog filename. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
View Code Duplication |
public static function changelogFile() { |
153
|
|
|
self::load(); |
154
|
|
|
if ( ! isset( self::$cache['changelog'] ) ) { |
155
|
|
|
self::$cache['changelog'] = self::addBase( self::$config['changelog'] ); |
156
|
|
|
} |
157
|
|
|
return self::$cache['changelog']; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the changes directory. |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
View Code Duplication |
public static function changesDir() { |
166
|
|
|
self::load(); |
167
|
|
|
if ( ! isset( self::$cache['changes-dir'] ) ) { |
168
|
|
|
self::$cache['changes-dir'] = self::addBase( self::$config['changes-dir'] ); |
169
|
|
|
} |
170
|
|
|
return self::$cache['changes-dir']; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get the link. |
175
|
|
|
* |
176
|
|
|
* @param string $old Old version number. |
177
|
|
|
* @param string $new New version number. |
178
|
|
|
* @return string|null |
179
|
|
|
*/ |
180
|
|
|
public static function link( $old, $new ) { |
181
|
|
|
self::load(); |
182
|
|
|
if ( null !== self::$config['link-template'] ) { |
183
|
|
|
return strtr( |
184
|
|
|
self::$config['link-template'], |
185
|
|
|
array( |
186
|
|
|
'${old}' => rawurlencode( $old ), |
187
|
|
|
'${new}' => rawurlencode( $new ), |
188
|
|
|
) |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
return null; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get change entry ordering. |
196
|
|
|
* |
197
|
|
|
* @return string[] |
198
|
|
|
*/ |
199
|
|
|
public static function ordering() { |
200
|
|
|
self::load(); |
201
|
|
|
if ( ! isset( self::$cache['ordering'] ) ) { |
202
|
|
|
self::$cache['ordering'] = array_map( 'strval', (array) self::$config['ordering'] ); |
203
|
|
|
} |
204
|
|
|
return self::$cache['ordering']; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get change types. |
209
|
|
|
* |
210
|
|
|
* @return array |
211
|
|
|
*/ |
212
|
|
|
public static function types() { |
213
|
|
|
self::load(); |
214
|
|
|
if ( ! isset( self::$cache['types'] ) ) { |
215
|
|
|
self::$cache['types'] = array(); |
216
|
|
|
foreach ( self::$config['types'] as $k => $v ) { |
217
|
|
|
self::$cache['types'][ strtolower( $k ) ] = $v; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
return self::$cache['types']; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Get a plugin. |
225
|
|
|
* |
226
|
|
|
* @param string|array $config Plugin name or configuration array. |
227
|
|
|
* @param string $suffix Plugin class suffix. |
228
|
|
|
* @param string $interface Expected interface name. |
229
|
|
|
* @return object|null Object, or null if the plugin was not found. |
230
|
|
|
*/ |
231
|
|
|
private static function getPlugin( $config, $suffix, $interface ) { |
232
|
|
|
if ( is_string( $config ) ) { |
233
|
|
|
$config = array( 'name' => $config ); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if ( isset( $config['name'] ) ) { |
237
|
|
|
$class = __NAMESPACE__ . '\\Plugins\\' . ucfirst( $config['name'] ) . $suffix; |
238
|
|
|
} elseif ( isset( $config['class'] ) ) { |
239
|
|
|
$class = $config['class']; |
240
|
|
|
} elseif ( isset( $config['filename'] ) ) { |
241
|
|
|
$classes = get_declared_classes(); |
242
|
|
|
require $config['filename']; |
243
|
|
|
$classes = array_filter( |
244
|
|
|
array_diff( get_declared_classes(), $classes ), |
245
|
|
|
function ( $class ) use ( $interface ) { |
246
|
|
|
return is_a( $class, $interface, true ); |
247
|
|
|
} |
248
|
|
|
); |
249
|
|
|
if ( count( $classes ) !== 1 ) { |
250
|
|
|
return null; |
251
|
|
|
} |
252
|
|
|
$class = array_pop( $classes ); |
253
|
|
|
} else { |
254
|
|
|
return null; |
255
|
|
|
} |
256
|
|
|
if ( ! class_exists( $class ) || ! is_a( $class, $interface, true ) ) { |
257
|
|
|
return null; |
258
|
|
|
} |
259
|
|
|
return $class::instantiate( $config ); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Get formatting plugin. |
264
|
|
|
* |
265
|
|
|
* @return Formatter |
266
|
|
|
* @throws \RuntimeException If the configured formatter is unknown. |
267
|
|
|
*/ |
268
|
|
View Code Duplication |
public static function formatterPlugin() { |
269
|
|
|
self::load(); |
270
|
|
|
if ( ! isset( self::$cache['formatter'] ) ) { |
271
|
|
|
$obj = self::getPlugin( self::$config['formatter'], 'Formatter', FormatterPlugin::class ); |
272
|
|
|
if ( ! $obj instanceof FormatterPlugin ) { |
273
|
|
|
$info = json_encode( self::$config['formatter'], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); |
274
|
|
|
throw new \RuntimeException( "Unknown formatter plugin $info" ); |
275
|
|
|
} |
276
|
|
|
self::$cache['formatter'] = $obj; |
277
|
|
|
} |
278
|
|
|
return self::$cache['formatter']; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Get verisoning plugin. |
283
|
|
|
* |
284
|
|
|
* @return Versioning |
285
|
|
|
* @throws \RuntimeException If the configured versioning plugin is unknown. |
286
|
|
|
*/ |
287
|
|
View Code Duplication |
public static function versioningPlugin() { |
288
|
|
|
self::load(); |
289
|
|
|
if ( ! isset( self::$cache['versioning'] ) ) { |
290
|
|
|
$obj = self::getPlugin( self::$config['versioning'], 'Versioning', VersioningPlugin::class ); |
291
|
|
|
if ( ! $obj instanceof VersioningPlugin ) { |
292
|
|
|
$info = json_encode( self::$config['versioning'], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); |
293
|
|
|
throw new \RuntimeException( "Unknown versioning plugin $info" ); |
294
|
|
|
} |
295
|
|
|
self::$cache['versioning'] = $obj; |
296
|
|
|
} |
297
|
|
|
return self::$cache['versioning']; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
} |
301
|
|
|
|