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