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
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Factory |
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 Factory |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Config factory method |
29
|
|
|
* |
30
|
|
|
* @param string $path |
31
|
|
|
* @return \CaptainHook\App\Config |
32
|
|
|
*/ |
33
|
24 |
|
public static function create(string $path = '') : Config |
34
|
|
|
{ |
35
|
24 |
|
$factory = new static(); |
36
|
|
|
|
37
|
24 |
|
return $factory->createConfig($path); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create a CaptainHook configuration |
42
|
|
|
* |
43
|
|
|
* @param string $path |
44
|
|
|
* @return \CaptainHook\App\Config |
45
|
|
|
* @throws \Exception |
46
|
|
|
*/ |
47
|
24 |
|
public function createConfig($path = '') : Config |
48
|
|
|
{ |
49
|
24 |
|
$path = $path ?: getcwd() . DIRECTORY_SEPARATOR . CH::CONFIG; |
50
|
24 |
|
$json = new Json($path); |
51
|
24 |
|
$fileExists = $json->exists(); |
52
|
24 |
|
$config = new Config($path, $fileExists); |
53
|
|
|
|
54
|
24 |
|
if ($fileExists) { |
55
|
19 |
|
$this->configure($config, $json->readAssoc()); |
56
|
|
|
} |
57
|
|
|
|
58
|
24 |
|
return $config; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Initialize the configuration with data load from config file |
63
|
|
|
* |
64
|
|
|
* @param \CaptainHook\App\Config $config |
65
|
|
|
* @param array $json |
66
|
|
|
* @return void |
67
|
|
|
* @throws \Exception |
68
|
|
|
*/ |
69
|
19 |
|
protected function configure(Config $config, array $json) : void |
70
|
|
|
{ |
71
|
19 |
|
Util::validateJsonConfiguration($json); |
72
|
|
|
|
73
|
19 |
|
foreach (HookUtil::getValidHooks() as $hook => $class) { |
74
|
19 |
|
if (isset($json[$hook])) { |
75
|
18 |
|
$this->configureHook($config->getHookConfig($hook), $json[$hook]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
19 |
|
$this->appendIncludedConfigurations($config, $json); |
80
|
19 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Setup a hook configuration by json data |
84
|
|
|
* |
85
|
|
|
* @param \CaptainHook\App\Config\Hook $config |
86
|
|
|
* @param array $json |
87
|
|
|
* @return void |
88
|
|
|
* @throws \Exception |
89
|
|
|
*/ |
90
|
18 |
|
protected function configureHook(Config\Hook $config, array $json) : void |
91
|
|
|
{ |
92
|
18 |
|
$config->setEnabled($json['enabled']); |
93
|
18 |
|
foreach ($json['actions'] as $actionJson) { |
94
|
12 |
|
$options = isset($actionJson['options']) && is_array($actionJson['options']) |
95
|
12 |
|
? $actionJson['options'] |
96
|
12 |
|
: []; |
97
|
12 |
|
$conditions = isset($actionJson['conditions']) && is_array($actionJson['conditions']) |
98
|
1 |
|
? $actionJson['conditions'] |
99
|
12 |
|
: []; |
100
|
12 |
|
$config->addAction(new Config\Action($actionJson['action'], $options, $conditions)); |
101
|
|
|
} |
102
|
18 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Append all included configuration to the current configuration |
106
|
|
|
* |
107
|
|
|
* @param \CaptainHook\App\Config $config |
108
|
|
|
* @param array $json |
109
|
|
|
*/ |
110
|
19 |
|
private function appendIncludedConfigurations(Config $config, array $json) |
111
|
|
|
{ |
112
|
19 |
|
$includes = $this->loadIncludedConfigs($json, $config->getPath()); |
113
|
19 |
|
foreach (HookUtil::getValidHooks() as $hook => $class) { |
114
|
19 |
|
foreach ($includes as $includedConfig) { |
115
|
1 |
|
$this->copyActionsFromTo($includedConfig->getHookConfig($hook), $config->getHookConfig($hook)); |
116
|
|
|
} |
117
|
|
|
} |
118
|
19 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Read included configurations to add them to the main configuration later |
122
|
|
|
* |
123
|
|
|
* @param array $json |
124
|
|
|
* @param string $path |
125
|
|
|
* @return \CaptainHook\App\Config[] |
126
|
|
|
*/ |
127
|
19 |
|
protected function loadIncludedConfigs(array $json, string $path) : array |
128
|
|
|
{ |
129
|
19 |
|
$includes = []; |
130
|
19 |
|
$directory = dirname($path); |
131
|
19 |
|
$files = isset($json['config']['includes']) && is_array($json['config']['includes']) |
132
|
1 |
|
? $json['config']['includes'] |
133
|
19 |
|
: []; |
134
|
|
|
|
135
|
19 |
|
foreach ($files as $file) { |
136
|
1 |
|
$includes[] = self::create($directory . DIRECTORY_SEPARATOR . $file); |
137
|
|
|
} |
138
|
19 |
|
return $includes; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Append actions for a given hook from all included configurations to the current configuration |
143
|
|
|
* |
144
|
|
|
* @param \CaptainHook\App\Config\Hook $includedConfig |
145
|
|
|
* @param \CaptainHook\App\Config\Hook $hookConfig |
146
|
|
|
*/ |
147
|
1 |
|
private function copyActionsFromTo(Hook $includedConfig, Hook $hookConfig) |
148
|
|
|
{ |
149
|
1 |
|
foreach ($includedConfig->getActions() as $action) { |
150
|
1 |
|
$hookConfig->addAction($action); |
151
|
|
|
} |
152
|
1 |
|
} |
153
|
|
|
} |
154
|
|
|
|