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\Config; |
11
|
|
|
|
12
|
|
|
use CaptainHook\App\CH; |
13
|
|
|
use CaptainHook\App\Config; |
14
|
|
|
use CaptainHook\App\Hook\Util as HookUtil; |
15
|
|
|
use CaptainHook\App\Storage\File\Json; |
16
|
|
|
use RuntimeException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Factory |
20
|
|
|
* |
21
|
|
|
* @package CaptainHook |
22
|
|
|
* @author Sebastian Feldmann <[email protected]> |
23
|
|
|
* @link https://github.com/captainhookphp/captainhook |
24
|
|
|
* @since Class available since Release 0.9.0 |
25
|
|
|
* @internal |
26
|
|
|
*/ |
27
|
|
|
final class Factory |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Maximal level in including config files |
31
|
|
|
* |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
private $maxIncludeLevel = 1; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Current level of inclusion |
38
|
|
|
* |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
private $includeLevel = 0; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create a CaptainHook configuration |
45
|
|
|
* |
46
|
|
|
* @param string $path |
47
|
|
|
* @return \CaptainHook\App\Config |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
29 |
|
public function createConfig($path = '') : Config |
51
|
|
|
{ |
52
|
29 |
|
$path = $path ?: getcwd() . DIRECTORY_SEPARATOR . CH::CONFIG; |
53
|
29 |
|
$file = new Json($path); |
54
|
|
|
|
55
|
29 |
|
return $this->setupConfig($file); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $path |
60
|
|
|
* @return \CaptainHook\App\Config |
61
|
|
|
* @throws \Exception |
62
|
|
|
*/ |
63
|
6 |
|
private function includeConfig(string $path) : Config |
64
|
|
|
{ |
65
|
6 |
|
$file = new Json($path); |
66
|
6 |
|
if (!$file->exists()) { |
67
|
1 |
|
throw new RuntimeException('Config to include not found: ' . $path); |
68
|
|
|
} |
69
|
5 |
|
return $this->setupConfig($file); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Return a configuration with data loaded from json file it it exists |
75
|
|
|
* |
76
|
|
|
* @param \CaptainHook\App\Storage\File\Json $file |
77
|
|
|
* @return \CaptainHook\App\Config |
78
|
|
|
* @throws \Exception |
79
|
|
|
*/ |
80
|
29 |
|
private function setupConfig(Json $file) : Config |
81
|
|
|
{ |
82
|
|
|
// use variable to not check file system twice |
83
|
29 |
|
$fileExists = $file->exists(); |
84
|
29 |
|
$config = new Config($file->getPath(), $fileExists); |
85
|
29 |
|
if ($fileExists) { |
86
|
24 |
|
$this->loadConfigFromFile($config, $file); |
87
|
|
|
} |
88
|
28 |
|
return $config; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Loads a given file into given the configuration |
93
|
|
|
* |
94
|
|
|
* @param \CaptainHook\App\Config $config |
95
|
|
|
* @param \CaptainHook\App\Storage\File\Json $file |
96
|
|
|
* @throws \Exception |
97
|
|
|
*/ |
98
|
24 |
|
private function loadConfigFromFile(Config $config, Json $file) : void |
99
|
|
|
{ |
100
|
24 |
|
$json = $file->readAssoc(); |
101
|
24 |
|
Util::validateJsonConfiguration($json); |
102
|
|
|
|
103
|
24 |
|
$this->appendIncludedConfigurations($config, $json); |
104
|
|
|
|
105
|
23 |
|
foreach (HookUtil::getValidHooks() as $hook => $class) { |
106
|
23 |
|
if (isset($json[$hook])) { |
107
|
23 |
|
$this->configureHook($config->getHookConfig($hook), $json[$hook]); |
108
|
|
|
} |
109
|
|
|
} |
110
|
23 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Setup a hook configuration by json data |
114
|
|
|
* |
115
|
|
|
* @param \CaptainHook\App\Config\Hook $config |
116
|
|
|
* @param array $json |
117
|
|
|
* @return void |
118
|
|
|
* @throws \Exception |
119
|
|
|
*/ |
120
|
22 |
|
private function configureHook(Config\Hook $config, array $json) : void |
121
|
|
|
{ |
122
|
22 |
|
$config->setEnabled($json['enabled']); |
123
|
22 |
|
foreach ($json['actions'] as $actionJson) { |
124
|
16 |
|
$options = isset($actionJson['options']) && is_array($actionJson['options']) |
125
|
16 |
|
? $actionJson['options'] |
126
|
16 |
|
: []; |
127
|
16 |
|
$conditions = isset($actionJson['conditions']) && is_array($actionJson['conditions']) |
128
|
1 |
|
? $actionJson['conditions'] |
129
|
16 |
|
: []; |
130
|
16 |
|
$config->addAction(new Config\Action($actionJson['action'], $options, $conditions)); |
131
|
|
|
} |
132
|
22 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Append all included configuration to the current configuration |
136
|
|
|
* |
137
|
|
|
* @param \CaptainHook\App\Config $config |
138
|
|
|
* @param array $json |
139
|
|
|
* @throws \Exception |
140
|
|
|
*/ |
141
|
24 |
|
private function appendIncludedConfigurations(Config $config, array $json) |
142
|
|
|
{ |
143
|
24 |
|
$this->readMaxIncludeLevel($json); |
144
|
24 |
|
if ($this->includeLevel < $this->maxIncludeLevel) { |
145
|
24 |
|
$includes = $this->loadIncludedConfigs($json, $config->getPath()); |
146
|
23 |
|
foreach (HookUtil::getValidHooks() as $hook => $class) { |
147
|
23 |
|
$this->mergeHookConfigFromIncludes($config->getHookConfig($hook), $includes); |
148
|
|
|
} |
149
|
|
|
} |
150
|
23 |
|
$this->includeLevel++; |
151
|
23 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Check config section for 'maxIncludeLevel' setting |
155
|
|
|
* |
156
|
|
|
* @param array $json |
157
|
|
|
*/ |
158
|
24 |
|
private function readMaxIncludeLevel(array $json) : void |
159
|
|
|
{ |
160
|
|
|
// read the include level setting only for the actual configuration |
161
|
24 |
|
if ($this->includeLevel === 0 && isset($json['config']['maxIncludeLevel'])) { |
162
|
1 |
|
$this->maxIncludeLevel = (int) $json['config']['maxIncludeLevel']; |
163
|
|
|
} |
164
|
24 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Merge a given hook config with the corresponding hook configs from a list of included configurations |
168
|
|
|
* |
169
|
|
|
* @param \CaptainHook\App\Config\Hook $hook |
170
|
|
|
* @param \CaptainHook\App\Config[] $includes |
171
|
|
|
* @return void |
172
|
|
|
*/ |
173
|
23 |
|
private function mergeHookConfigFromIncludes(Hook $hook, array $includes) : void |
174
|
|
|
{ |
175
|
23 |
|
foreach ($includes as $includedConfig) { |
176
|
5 |
|
$includedHook = $includedConfig->getHookConfig($hook->getName()); |
177
|
5 |
|
if ($includedHook->isEnabled()) { |
178
|
5 |
|
$hook->setEnabled(true); |
179
|
|
|
} |
180
|
5 |
|
$this->copyActionsFromTo($includedHook, $hook); |
181
|
|
|
} |
182
|
23 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Return list of included configurations to add them to the main configuration afterwards |
186
|
|
|
* |
187
|
|
|
* @param array $json |
188
|
|
|
* @param string $path |
189
|
|
|
* @return \CaptainHook\App\Config[] |
190
|
|
|
* @throws \Exception |
191
|
|
|
*/ |
192
|
24 |
|
protected function loadIncludedConfigs(array $json, string $path) : array |
193
|
|
|
{ |
194
|
24 |
|
$includes = []; |
195
|
24 |
|
$directory = dirname($path); |
196
|
24 |
|
$files = isset($json['config']['includes']) && is_array($json['config']['includes']) |
197
|
6 |
|
? $json['config']['includes'] |
198
|
24 |
|
: []; |
199
|
|
|
|
200
|
24 |
|
foreach ($files as $file) { |
201
|
6 |
|
$includes[] = $this->includeConfig($directory . DIRECTORY_SEPARATOR . $file); |
202
|
|
|
} |
203
|
23 |
|
return $includes; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Copy action from a given configuration to the second given configuration |
208
|
|
|
* |
209
|
|
|
* @param \CaptainHook\App\Config\Hook $sourceConfig |
210
|
|
|
* @param \CaptainHook\App\Config\Hook $targetConfig |
211
|
|
|
*/ |
212
|
5 |
|
private function copyActionsFromTo(Hook $sourceConfig, Hook $targetConfig) |
213
|
|
|
{ |
214
|
5 |
|
foreach ($sourceConfig->getActions() as $action) { |
215
|
5 |
|
$targetConfig->addAction($action); |
216
|
|
|
} |
217
|
5 |
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Config factory method |
221
|
|
|
* |
222
|
|
|
* @param string $path |
223
|
|
|
* @return \CaptainHook\App\Config |
224
|
|
|
*/ |
225
|
29 |
|
public static function create(string $path = '') : Config |
226
|
|
|
{ |
227
|
29 |
|
$factory = new static(); |
228
|
29 |
|
return $factory->createConfig($path); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|