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 Pyrech\ComposerChangelogs\Util\FileSystemHelper; |
15
|
|
|
|
16
|
|
|
class ConfigBuilder |
17
|
|
|
{ |
18
|
|
|
private static $validCommitAutoValues = [ |
19
|
|
|
'never', |
20
|
|
|
'ask', |
21
|
|
|
'always', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** @var string[] */ |
25
|
|
|
private $warnings = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array $extra |
29
|
|
|
* @param string $baseDir |
30
|
|
|
* |
31
|
|
|
* @return Config |
32
|
|
|
*/ |
33
|
18 |
|
public function build(array $extra, $baseDir) |
34
|
|
|
{ |
35
|
18 |
|
$this->reset(); |
36
|
|
|
|
37
|
18 |
|
$commitAuto = 'never'; |
38
|
18 |
|
$commitBinFile = null; |
39
|
|
|
|
40
|
18 |
|
if (array_key_exists('commit-auto', $extra)) { |
41
|
12 |
|
if (in_array($extra['commit-auto'], self::$validCommitAutoValues, true)) { |
42
|
10 |
|
$commitAuto = $extra['commit-auto']; |
43
|
10 |
|
} else { |
44
|
2 |
|
$this->warnings[] = self::createWarningFromInvalidValue( |
45
|
2 |
|
$extra, |
46
|
2 |
|
'commit-auto', |
47
|
2 |
|
$commitAuto, |
48
|
2 |
|
sprintf('Valid options are "%s".', implode('", "', self::$validCommitAutoValues)) |
49
|
2 |
|
); |
50
|
|
|
} |
51
|
12 |
|
} |
52
|
|
|
|
53
|
18 |
|
if (array_key_exists('commit-bin-file', $extra)) { |
54
|
8 |
|
if ($commitAuto === 'never') { |
55
|
2 |
|
$this->warnings[] = '"commit-bin-file" is specified but "commit-auto" option is set to "never". Ignoring.'; |
56
|
2 |
|
} else { |
57
|
6 |
|
$file = realpath( |
58
|
6 |
|
FileSystemHelper::isAbsolute($extra['commit-bin-file']) |
59
|
6 |
|
? $extra['commit-bin-file'] |
60
|
6 |
|
: $baseDir . '/' . $extra['commit-bin-file'] |
61
|
6 |
|
); |
62
|
|
|
|
63
|
6 |
|
if (!file_exists($file)) { |
64
|
2 |
|
$this->warnings[] = 'The file pointed by the option "commit-bin-file" was not found. Ignoring.'; |
65
|
2 |
|
} else { |
66
|
4 |
|
$commitBinFile = $file; |
67
|
|
|
} |
68
|
|
|
} |
69
|
8 |
|
} else { |
70
|
10 |
|
if ($commitAuto !== 'never') { |
71
|
2 |
|
$this->warnings[] = sprintf( |
72
|
2 |
|
'"commit-auto" is set to "%s" but "commit-bin-file" was not specified.', |
73
|
|
|
$commitAuto |
74
|
2 |
|
); |
75
|
2 |
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
18 |
|
return new Config($commitAuto, $commitBinFile); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string[] |
83
|
|
|
*/ |
84
|
18 |
|
public function getWarnings() |
85
|
|
|
{ |
86
|
18 |
|
return $this->warnings; |
87
|
|
|
} |
88
|
|
|
|
89
|
18 |
|
private function reset() |
90
|
|
|
{ |
91
|
18 |
|
$this->warnings = []; |
92
|
18 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array $extra |
96
|
|
|
* @param string $key |
97
|
|
|
* @param mixed $default |
98
|
|
|
* @param string $additionalMessage |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
2 |
|
private static function createWarningFromInvalidValue(array $extra, $key, $default, $additionalMessage = '') |
103
|
|
|
{ |
104
|
2 |
|
$warning = sprintf( |
105
|
2 |
|
'Invalid value "%s" for option "%s", defaulting to "%s".', |
106
|
2 |
|
$extra[$key], |
107
|
2 |
|
$key, |
108
|
|
|
$default |
109
|
2 |
|
); |
110
|
|
|
|
111
|
2 |
|
if ($additionalMessage) { |
112
|
2 |
|
$warning .= ' ' . $additionalMessage; |
113
|
2 |
|
} |
114
|
|
|
|
115
|
2 |
|
return $warning; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|