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
|
22 |
|
public function build(array $extra, $baseDir) |
34
|
|
|
{ |
35
|
22 |
|
$this->reset(); |
36
|
|
|
|
37
|
22 |
|
$commitAuto = 'never'; |
38
|
22 |
|
$commitBinFile = null; |
39
|
22 |
|
$commitMessage = 'Update dependencies'; |
40
|
|
|
|
41
|
22 |
|
if (array_key_exists('commit-auto', $extra)) { |
42
|
16 |
|
if (in_array($extra['commit-auto'], self::$validCommitAutoValues, true)) { |
43
|
14 |
|
$commitAuto = $extra['commit-auto']; |
44
|
14 |
|
} else { |
45
|
2 |
|
$this->warnings[] = self::createWarningFromInvalidValue( |
46
|
2 |
|
$extra, |
47
|
2 |
|
'commit-auto', |
48
|
2 |
|
$commitAuto, |
49
|
2 |
|
sprintf('Valid options are "%s".', implode('", "', self::$validCommitAutoValues)) |
50
|
2 |
|
); |
51
|
|
|
} |
52
|
16 |
|
} |
53
|
|
|
|
54
|
22 |
|
if (array_key_exists('commit-bin-file', $extra)) { |
55
|
12 |
|
if ($commitAuto === 'never') { |
56
|
2 |
|
$this->warnings[] = '"commit-bin-file" is specified but "commit-auto" option is set to "never". Ignoring.'; |
57
|
2 |
|
} else { |
58
|
10 |
|
$file = realpath( |
59
|
10 |
|
FileSystemHelper::isAbsolute($extra['commit-bin-file']) |
60
|
10 |
|
? $extra['commit-bin-file'] |
61
|
10 |
|
: $baseDir . '/' . $extra['commit-bin-file'] |
62
|
10 |
|
); |
63
|
|
|
|
64
|
10 |
|
if (!file_exists($file)) { |
65
|
2 |
|
$this->warnings[] = 'The file pointed by the option "commit-bin-file" was not found. Ignoring.'; |
66
|
2 |
|
} else { |
67
|
8 |
|
$commitBinFile = $file; |
68
|
|
|
} |
69
|
|
|
} |
70
|
12 |
|
} else { |
71
|
10 |
|
if ($commitAuto !== 'never') { |
72
|
2 |
|
$this->warnings[] = sprintf( |
73
|
2 |
|
'"commit-auto" is set to "%s" but "commit-bin-file" was not specified.', |
74
|
|
|
$commitAuto |
75
|
2 |
|
); |
76
|
2 |
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
22 |
|
if (array_key_exists('commit-message', $extra)) { |
80
|
2 |
|
if (strlen(trim($extra['commit-message'])) === 0) { |
81
|
|
|
$this->warnings[] = '"commit-message" is specified but empty. Ignoring and using default commit message.'; |
82
|
|
|
} else { |
83
|
2 |
|
$commitMessage = $extra['commit-message']; |
84
|
|
|
} |
85
|
2 |
|
} |
86
|
|
|
|
87
|
22 |
|
return new Config($commitAuto, $commitBinFile, $commitMessage); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string[] |
92
|
|
|
*/ |
93
|
22 |
|
public function getWarnings() |
94
|
|
|
{ |
95
|
22 |
|
return $this->warnings; |
96
|
|
|
} |
97
|
|
|
|
98
|
22 |
|
private function reset() |
99
|
|
|
{ |
100
|
22 |
|
$this->warnings = []; |
101
|
22 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $extra |
105
|
|
|
* @param string $key |
106
|
|
|
* @param mixed $default |
107
|
|
|
* @param string $additionalMessage |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
2 |
|
private static function createWarningFromInvalidValue(array $extra, $key, $default, $additionalMessage = '') |
112
|
|
|
{ |
113
|
2 |
|
$warning = sprintf( |
114
|
2 |
|
'Invalid value "%s" for option "%s", defaulting to "%s".', |
115
|
2 |
|
$extra[$key], |
116
|
2 |
|
$key, |
117
|
|
|
$default |
118
|
2 |
|
); |
119
|
|
|
|
120
|
2 |
|
if ($additionalMessage) { |
121
|
2 |
|
$warning .= ' ' . $additionalMessage; |
122
|
2 |
|
} |
123
|
|
|
|
124
|
2 |
|
return $warning; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|