Completed
Push — add/changelog-tooling ( 3f93f3...c37726 )
by
unknown
250:15 queued 241:25
created

Config::setOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
		'versioning' => 'semver',
26
		'types'      => array(
27
			'security'   => 'Security',
28
			'added'      => 'Added',
29
			'changed'    => 'Changed',
30
			'deprecated' => 'Deprecated',
31
			'removed'    => 'Removed',
32
			'fixed'      => 'Fixed',
33
		),
34
	);
35
36
	/**
37
	 * Active config settings.
38
	 *
39
	 * @var array
40
	 */
41
	private static $config = array();
42
43
	/**
44
	 * Cached config settings.
45
	 *
46
	 * @var array
47
	 */
48
	private static $cache = array();
49
50
	/**
51
	 * Whether `load()` was called already.
52
	 *
53
	 * @var bool
54
	 */
55
	private static $loaded = false;
56
57
	/**
58
	 * OutputInterface.
59
	 *
60
	 * @var OutputInterface|null
61
	 */
62
	private static $out;
63
64
	/**
65
	 * Set the OutputInterface.
66
	 *
67
	 * @param OutputInterface $out OutputInterface.
68
	 */
69
	public static function setOutput( OutputInterface $out ) {
70
		self::$out = $out;
71
	}
72
73
	/**
74
	 * Load the configuration.
75
	 *
76
	 * @throws \LogicException If called before `setOutput()`.
77
	 */
78
	private static function load() {
79
		if ( ! self::$out ) {
80
			throw new \LogicException( 'Must call Config::setOutput() before Config::load()' );
81
		}
82
		if ( self::$loaded ) {
83
			return;
84
		}
85
		self::$loaded = true;
86
87
		self::$config         = self::$defaultConfig;
88
		self::$config['base'] = getcwd();
89
90
		$composer = getenv( 'COMPOSER' );
91
		if ( $composer ) {
92
			$from = ' (as specified by the COMPOSER environment variable)'; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
93
		} else {
94
			$composer = 'composer.json';
95
			$from     = ''; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
96
		}
97
		if ( ! file_exists( $composer ) ) {
98
			self::$out->writeln( "<error>File {$composer}{$from} is not found.</>" );
99
			return;
100
		}
101
		$data = json_decode( file_get_contents( $composer ), true );
102
		if ( ! is_array( $data ) ) {
103
			self::$out->writeln( "<error>File {$composer}{$from} could not be parsed.</>" );
104
			return;
105
		}
106
107
		self::$config['base'] = dirname( realpath( $composer ) );
108
		if ( isset( $data['extra']['changelogger'] ) ) {
109
			self::$config = array_merge( self::$config, $data['extra']['changelogger'] );
110
		}
111
	}
112
113
	/**
114
	 * Get the base directory.
115
	 *
116
	 * @return string
117
	 */
118
	public static function base() {
119
		self::load();
120
		return self::$config['base'];
121
	}
122
123
	/**
124
	 * Get the changelog directory.
125
	 *
126
	 * @return string
127
	 */
128
	public static function changelogDir() {
129
		self::load();
130
		return self::$config['base'] . '/changelog';
131
	}
132
133
	/**
134
	 * Get verisoning method.
135
	 *
136
	 * @return Versioning
137
	 */
138
	public static function versioning() {
139
		self::load();
140
		if ( ! isset( self::$cache['versioning'] ) ) {
141
			$class = __NAMESPACE . '\\Versioning\\' . ucfirst( self::$config['versioning'] );
142
			if ( ! class_exists( $class ) ) {
143
				self::$out->writeln( '<warning>Unknown versioning method "' . self::$config['versioning'] . '". Using "semver".</>' );
144
				$class = __NAMESPACE . '\\Versioning\\Semver';
145
			}
146
			self::$cache['versioning'] = new $class( self::$out );
147
		}
148
		return self::$cache['versioning'];
149
	}
150
151
	/**
152
	 * Get change types.
153
	 *
154
	 * @return array
155
	 */
156
	public static function types() {
157
		self::load();
158
		if ( ! isset( self::$cache['types'] ) ) {
159
			self::$cache['types'] = array();
160
			foreach ( self::$config['types'] as $k => $v ) {
161
				self::$cache['types'][ strtolower( $k ) ] = $v;
162
			}
163
		}
164
		return self::$cache['types'];
165
	}
166
167
}
168