1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of CaptainHook. |
4
|
|
|
* |
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace CaptainHook\App; |
11
|
|
|
|
12
|
|
|
use InvalidArgumentException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Config |
16
|
|
|
* |
17
|
|
|
* @package CaptainHook |
18
|
|
|
* @author Sebastian Feldmann <[email protected]> |
19
|
|
|
* @link https://github.com/captainhookphp/captainhook |
20
|
|
|
* @since Class available since Release 0.9.0 |
21
|
|
|
* @internal |
22
|
|
|
*/ |
23
|
|
|
class Config |
24
|
|
|
{ |
25
|
|
|
const SETTING_GIT_DIR = 'git-directory'; |
26
|
|
|
const SETTING_VERBOSITY = 'verbosity'; |
27
|
|
|
const SETTING_COLORS = 'ansi-colors'; |
28
|
|
|
const SETTING_INCLUDES = 'includes'; |
29
|
|
|
const SETTING_INCLUDES_LEVEL = 'includes-level'; |
30
|
|
|
const SETTING_RUN_MODE = 'run-mode'; |
31
|
|
|
const SETTING_RUN_EXEC = 'run-exec'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Path to the config file |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $path; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Does the config file exist |
42
|
|
|
* |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
private $fileExists; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* CaptainHook settings |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private $settings; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* List of hook configs |
56
|
|
|
* |
57
|
|
|
* @var \CaptainHook\App\Config\Hook[] |
58
|
|
|
*/ |
59
|
|
|
private $hooks = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Config constructor |
63
|
|
|
* |
64
|
|
|
* @param string $path |
65
|
|
|
* @param bool $fileExists |
66
|
|
|
* @param array $settings |
67
|
|
|
*/ |
68
|
63 |
|
public function __construct(string $path, bool $fileExists = false, array $settings = []) |
69
|
|
|
{ |
70
|
63 |
|
$this->path = $path; |
71
|
63 |
|
$this->fileExists = $fileExists; |
72
|
63 |
|
$this->settings = $settings; |
73
|
|
|
|
74
|
63 |
|
foreach (Hooks::getValidHooks() as $hook => $value) { |
75
|
63 |
|
$this->hooks[$hook] = new Config\Hook($hook); |
76
|
|
|
} |
77
|
63 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Is configuration loaded from file |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
23 |
|
public function isLoadedFromFile() : bool |
85
|
|
|
{ |
86
|
23 |
|
return $this->fileExists; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Path getter |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
27 |
|
public function getPath() : string |
95
|
|
|
{ |
96
|
27 |
|
return $this->path; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return git directory path if configured, CWD/.git if not |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
6 |
|
public function getGitDirectory() : string |
105
|
|
|
{ |
106
|
6 |
|
return !empty($this->settings[self::SETTING_GIT_DIR]) |
107
|
1 |
|
? dirname($this->path) . DIRECTORY_SEPARATOR . $this->settings[self::SETTING_GIT_DIR] |
108
|
6 |
|
: getcwd() . DIRECTORY_SEPARATOR . '.git'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Return the configured verbosity |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
8 |
|
public function getVerbosity() : string |
117
|
|
|
{ |
118
|
8 |
|
return !empty($this->settings[self::SETTING_VERBOSITY]) |
119
|
1 |
|
? $this->settings[self::SETTING_VERBOSITY] |
120
|
8 |
|
: 'verbose'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Should the output use ansi colors |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
10 |
|
public function useAnsiColors() : bool |
129
|
|
|
{ |
130
|
10 |
|
return (bool) ($this->settings[self::SETTING_COLORS] ?? true); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get configured run-mode |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
7 |
|
public function getRunMode() : string |
139
|
|
|
{ |
140
|
7 |
|
return (string) ($this->settings[self::SETTING_RUN_MODE] ?? 'local'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get configured run-exec |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
7 |
|
public function getRunExec() : string |
149
|
|
|
{ |
150
|
7 |
|
return (string) ($this->settings[self::SETTING_RUN_EXEC] ?? ''); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Return config for given hook |
155
|
|
|
* |
156
|
|
|
* @param string $hook |
157
|
|
|
* @return \CaptainHook\App\Config\Hook |
158
|
|
|
* @throws \InvalidArgumentException |
159
|
|
|
*/ |
160
|
27 |
|
public function getHookConfig(string $hook) : Config\Hook |
161
|
|
|
{ |
162
|
27 |
|
if (!Hook\Util::isValid($hook)) { |
163
|
1 |
|
throw new InvalidArgumentException('Invalid hook name: ' . $hook); |
164
|
|
|
} |
165
|
26 |
|
return $this->hooks[$hook]; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Return config array to write to disc |
170
|
|
|
* |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
6 |
|
public function getJsonData() : array |
174
|
|
|
{ |
175
|
6 |
|
$return = []; |
176
|
|
|
|
177
|
6 |
|
foreach (Hooks::getValidHooks() as $hook => $value) { |
178
|
6 |
|
$return[$hook] = $this->hooks[$hook]->getJsonData(); |
179
|
|
|
} |
180
|
|
|
|
181
|
6 |
|
return $return; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|