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