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_COLORS = 'ansi-colors'; |
27
|
|
|
const SETTING_RUN_MODE = 'run-mode'; |
28
|
|
|
const SETTING_RUN_EXEC = 'run-exec'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Path to the config file |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $path; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Does the config file exist |
39
|
|
|
* |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
private $fileExists; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* CaptainHook settings |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $settings; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* List of hook configs |
53
|
|
|
* |
54
|
|
|
* @var \CaptainHook\App\Config\Hook[] |
55
|
|
|
*/ |
56
|
|
|
private $hooks = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Config constructor |
60
|
|
|
* |
61
|
|
|
* @param string $path |
62
|
|
|
* @param bool $fileExists |
63
|
|
|
* @param array $settings |
64
|
|
|
*/ |
65
|
63 |
|
public function __construct(string $path, bool $fileExists = false, array $settings = []) |
66
|
|
|
{ |
67
|
63 |
|
$this->path = $path; |
68
|
63 |
|
$this->fileExists = $fileExists; |
69
|
63 |
|
$this->settings = $settings; |
70
|
|
|
|
71
|
63 |
|
foreach (Hooks::getValidHooks() as $hook => $value) { |
72
|
63 |
|
$this->hooks[$hook] = new Config\Hook($hook); |
73
|
|
|
} |
74
|
63 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Is configuration loaded from file |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
23 |
|
public function isLoadedFromFile() : bool |
82
|
|
|
{ |
83
|
23 |
|
return $this->fileExists; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Path getter |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
27 |
|
public function getPath() : string |
92
|
|
|
{ |
93
|
27 |
|
return $this->path; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return git directory path if configured, CWD/.git if not |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
6 |
|
public function getGitDirectory() : string |
102
|
|
|
{ |
103
|
6 |
|
return !empty($this->settings[self::SETTING_GIT_DIR]) |
104
|
1 |
|
? dirname($this->path) . DIRECTORY_SEPARATOR . $this->settings[self::SETTING_GIT_DIR] |
105
|
6 |
|
: getcwd() . DIRECTORY_SEPARATOR . '.git'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Should the output use ansi colors |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
10 |
|
public function useAnsiColors() : bool |
114
|
|
|
{ |
115
|
10 |
|
return (bool) ($this->settings[self::SETTING_COLORS] ?? true); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get configured run-mode |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
7 |
|
public function getRunMode() : string |
124
|
|
|
{ |
125
|
7 |
|
return (string) ($this->settings[self::SETTING_RUN_MODE] ?? 'local'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get configured run-exec |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
7 |
|
public function getRunExec() : string |
134
|
|
|
{ |
135
|
7 |
|
return (string) ($this->settings[self::SETTING_RUN_EXEC] ?? ''); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Return config for given hook |
140
|
|
|
* |
141
|
|
|
* @param string $hook |
142
|
|
|
* @return \CaptainHook\App\Config\Hook |
143
|
|
|
* @throws \InvalidArgumentException |
144
|
|
|
*/ |
145
|
27 |
|
public function getHookConfig(string $hook) : Config\Hook |
146
|
|
|
{ |
147
|
27 |
|
if (!Hook\Util::isValid($hook)) { |
148
|
1 |
|
throw new InvalidArgumentException('Invalid hook name: ' . $hook); |
149
|
|
|
} |
150
|
26 |
|
return $this->hooks[$hook]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Return config array to write to disc |
155
|
|
|
* |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
6 |
|
public function getJsonData() : array |
159
|
|
|
{ |
160
|
6 |
|
$return = []; |
161
|
|
|
|
162
|
6 |
|
foreach (Hooks::getValidHooks() as $hook => $value) { |
163
|
6 |
|
$return[$hook] = $this->hooks[$hook]->getJsonData(); |
164
|
|
|
} |
165
|
|
|
|
166
|
6 |
|
return $return; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|