Completed
Push — add/changelog-tooling ( 257a85 )
by
unknown
149:15 queued 138:48
created

Config::load()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 10
nop 0
dl 0
loc 34
rs 8.4426
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 verisoning method.
125
	 *
126
	 * @return Versioning
127
	 */
128
	public static function versioning() {
129
		self::load();
130
		if ( ! isset( self::$cache['versioning'] ) ) {
131
			$class = __NAMESPACE . '\\Versioning\\' . ucfirst( self::$config['versioning'] );
132
			if ( ! class_exists( $class ) ) {
133
				self::$out->writeln( '<warning>Unknown versioning method "' . self::$config['versioning'] . '". Using "semver".</>' );
134
				$class = __NAMESPACE . '\\Versioning\\Semver';
135
			}
136
			self::$cache['versioning'] = new $class( self::$out );
137
		}
138
		return self::$cache['versioning'];
139
	}
140
141
	/**
142
	 * Get change types.
143
	 *
144
	 * @return array
145
	 */
146
	public static function types() {
147
		self::load();
148
		if ( ! isset( self::$cache['types'] ) ) {
149
			self::$cache['types'] = array();
150
			foreach ( self::$config['types'] as $k => $v ) {
151
				self::$cache['types'][ strtolower( $k ) ] = $v;
152
			}
153
		}
154
		return self::$cache['types'];
155
	}
156
157
}
158