|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of CaptainHook |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Sebastian Feldmann <[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 CaptainHook\App; |
|
13
|
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
|
15
|
|
|
use SebastianFeldmann\Camino\Check; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Config |
|
19
|
|
|
* |
|
20
|
|
|
* @package CaptainHook |
|
21
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
22
|
|
|
* @link https://github.com/captainhookphp/captainhook |
|
23
|
|
|
* @since Class available since Release 0.9.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
class Config |
|
26
|
|
|
{ |
|
27
|
|
|
public const SETTING_BOOTSTRAP = 'bootstrap'; |
|
28
|
|
|
public const SETTING_COLORS = 'ansi-colors'; |
|
29
|
|
|
public const SETTING_GIT_DIR = 'git-directory'; |
|
30
|
|
|
public const SETTING_INCLUDES = 'includes'; |
|
31
|
|
|
public const SETTING_INCLUDES_LEVEL = 'includes-level'; |
|
32
|
|
|
public const SETTING_RUN_EXEC = 'run-exec'; |
|
33
|
|
|
public const SETTING_RUN_MODE = 'run-mode'; |
|
34
|
|
|
public const SETTING_RUN_PATH = 'run-path'; |
|
35
|
|
|
public const SETTING_VERBOSITY = 'verbosity'; |
|
36
|
|
|
public const SETTING_FAIL_ON_FIRST_ERROR = 'fail-on-first-error'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Path to the config file |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $path; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Does the config file exist |
|
47
|
|
|
* |
|
48
|
|
|
* @var bool |
|
49
|
|
|
*/ |
|
50
|
|
|
private $fileExists; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* CaptainHook settings |
|
54
|
|
|
* |
|
55
|
|
|
* @var array<string, string> |
|
56
|
|
|
*/ |
|
57
|
|
|
private $settings; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* List of hook configs |
|
61
|
|
|
* |
|
62
|
|
|
* @var array<string, \CaptainHook\App\Config\Hook> |
|
63
|
|
|
*/ |
|
64
|
|
|
private $hooks = []; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Config constructor |
|
68
|
|
|
* |
|
69
|
69 |
|
* @param string $path |
|
70
|
|
|
* @param bool $fileExists |
|
71
|
69 |
|
* @param array $settings |
|
72
|
69 |
|
*/ |
|
73
|
69 |
|
public function __construct(string $path, bool $fileExists = false, array $settings = []) |
|
74
|
|
|
{ |
|
75
|
69 |
|
$this->path = $path; |
|
76
|
69 |
|
$this->fileExists = $fileExists; |
|
77
|
|
|
$this->settings = $settings; |
|
78
|
69 |
|
|
|
79
|
|
|
foreach (Hooks::getValidHooks() as $hook => $value) { |
|
80
|
|
|
$this->hooks[$hook] = new Config\Hook($hook); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
23 |
|
* Is configuration loaded from file |
|
86
|
|
|
* |
|
87
|
23 |
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
|
|
public function isLoadedFromFile(): bool |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->fileExists; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
29 |
|
* Path getter |
|
96
|
|
|
* |
|
97
|
29 |
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getPath(): string |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->path; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
8 |
|
* Return git directory path if configured, CWD/.git if not |
|
106
|
|
|
* |
|
107
|
8 |
|
* @return string |
|
108
|
2 |
|
*/ |
|
109
|
|
|
public function getGitDirectory(): string |
|
110
|
|
|
{ |
|
111
|
|
|
if (empty($this->settings[self::SETTING_GIT_DIR])) { |
|
112
|
6 |
|
return getcwd() . '/.git'; |
|
113
|
3 |
|
} |
|
114
|
6 |
|
|
|
115
|
|
|
// if repo path is absolute use it otherwise create an absolute path relative to the configuration file |
|
116
|
|
|
return Check::isAbsolutePath($this->settings[self::SETTING_GIT_DIR]) |
|
117
|
|
|
? $this->settings[self::SETTING_GIT_DIR] |
|
118
|
|
|
: dirname($this->path) . '/' . $this->settings[self::SETTING_GIT_DIR]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
5 |
|
* Return bootstrap file if configured, CWD/vendor/autoload.php by default |
|
123
|
|
|
* |
|
124
|
5 |
|
* @return string |
|
125
|
1 |
|
*/ |
|
126
|
5 |
|
public function getBootstrap(): string |
|
127
|
|
|
{ |
|
128
|
|
|
return !empty($this->settings[self::SETTING_BOOTSTRAP]) |
|
129
|
|
|
? $this->settings[self::SETTING_BOOTSTRAP] |
|
130
|
|
|
: 'vendor/autoload.php'; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
8 |
|
* Return the configured verbosity |
|
135
|
|
|
* |
|
136
|
8 |
|
* @return string |
|
137
|
1 |
|
*/ |
|
138
|
8 |
|
public function getVerbosity(): string |
|
139
|
|
|
{ |
|
140
|
|
|
return !empty($this->settings[self::SETTING_VERBOSITY]) |
|
141
|
|
|
? $this->settings[self::SETTING_VERBOSITY] |
|
142
|
|
|
: 'normal'; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
10 |
|
* Should the output use ansi colors |
|
147
|
|
|
* |
|
148
|
10 |
|
* @return bool |
|
149
|
|
|
*/ |
|
150
|
|
|
public function useAnsiColors(): bool |
|
151
|
|
|
{ |
|
152
|
|
|
return (bool) ($this->settings[self::SETTING_COLORS] ?? true); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
6 |
|
* Get configured run-mode |
|
157
|
|
|
* |
|
158
|
6 |
|
* @return string |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getRunMode(): string |
|
161
|
|
|
{ |
|
162
|
|
|
return (string) ($this->settings[self::SETTING_RUN_MODE] ?? 'local'); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
3 |
|
* Get configured run-exec |
|
167
|
|
|
* |
|
168
|
3 |
|
* @return string |
|
169
|
|
|
*/ |
|
170
|
|
|
public function getRunExec(): string |
|
171
|
|
|
{ |
|
172
|
|
|
return (string) ($this->settings[self::SETTING_RUN_EXEC] ?? ''); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Get configured run-path |
|
177
|
|
|
* |
|
178
|
28 |
|
* @return string |
|
179
|
|
|
*/ |
|
180
|
28 |
|
public function getRunPath(): string |
|
181
|
1 |
|
{ |
|
182
|
|
|
return (string) ($this->settings[self::SETTING_RUN_PATH] ?? ''); |
|
183
|
27 |
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Whether to abort the hook as soon as a any action has errored. Default is true. |
|
187
|
|
|
* Otherwise, all actions get executed (even if some of them have failed) and |
|
188
|
|
|
* finally, a non-zero exit code is returned if any action has errored. |
|
189
|
|
|
* |
|
190
|
|
|
* @return bool |
|
191
|
8 |
|
*/ |
|
192
|
|
|
public function failOnFirstError(): bool |
|
193
|
8 |
|
{ |
|
194
|
|
|
return (bool) ($this->settings[self::SETTING_FAIL_ON_FIRST_ERROR] ?? true); |
|
195
|
8 |
|
} |
|
196
|
1 |
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Return config for given hook |
|
199
|
8 |
|
* |
|
200
|
8 |
|
* @param string $hook |
|
201
|
|
|
* @return \CaptainHook\App\Config\Hook |
|
202
|
|
|
* @throws \InvalidArgumentException |
|
203
|
8 |
|
*/ |
|
204
|
|
|
public function getHookConfig(string $hook): Config\Hook |
|
205
|
|
|
{ |
|
206
|
|
|
if (!Hook\Util::isValid($hook)) { |
|
207
|
|
|
throw new InvalidArgumentException('Invalid hook name: ' . $hook); |
|
208
|
|
|
} |
|
209
|
|
|
return $this->hooks[$hook]; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Return config array to write to disc |
|
214
|
|
|
* |
|
215
|
|
|
* @return array |
|
216
|
|
|
*/ |
|
217
|
|
|
public function getJsonData(): array |
|
218
|
|
|
{ |
|
219
|
|
|
$data = []; |
|
220
|
|
|
// only append config settings if at least one setting is present |
|
221
|
|
|
if (!empty($this->settings)) { |
|
222
|
|
|
$data['config'] = $this->settings; |
|
223
|
|
|
} |
|
224
|
|
|
// append all configured hooks |
|
225
|
|
|
foreach (Hooks::getValidHooks() as $hook => $value) { |
|
226
|
|
|
$data[$hook] = $this->hooks[$hook]->getJsonData(); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return $data; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|