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 SebastianFeldmann\CaptainHook\Config; |
11
|
|
|
|
12
|
|
|
use SebastianFeldmann\CaptainHook\Config; |
13
|
|
|
use SebastianFeldmann\CaptainHook\Hook\Util as HookUtil; |
14
|
|
|
use SebastianFeldmann\CaptainHook\Storage\File\Json; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Factory |
18
|
|
|
* |
19
|
|
|
* @package CaptainHook |
20
|
|
|
* @author Sebastian Feldmann <[email protected]> |
21
|
|
|
* @link https://github.com/sebastianfeldmann/captainhook |
22
|
|
|
* @since Class available since Release 0.9.0 |
23
|
|
|
*/ |
24
|
|
|
class Factory |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Config factory method. |
28
|
|
|
* |
29
|
|
|
* @param string $path |
30
|
|
|
* @return \SebastianFeldmann\CaptainHook\Config |
31
|
|
|
*/ |
32
|
13 |
|
public static function create(string $path = '') |
33
|
|
|
{ |
34
|
13 |
|
$factory = new static(); |
35
|
|
|
|
36
|
13 |
|
return $factory->createConfig($path); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a CaptainHook configuration. |
41
|
|
|
* |
42
|
|
|
* @param string $path |
43
|
|
|
* @return \SebastianFeldmann\CaptainHook\Config |
44
|
|
|
*/ |
45
|
13 |
|
public function createConfig($path = '') |
46
|
|
|
{ |
47
|
13 |
|
$path = $path ?: getcwd() . DIRECTORY_SEPARATOR . 'captainhook.json'; |
48
|
13 |
|
$json = new Json($path); |
49
|
13 |
|
$fileExists = $json->exists(); |
50
|
13 |
|
$config = new Config($path, $fileExists); |
51
|
|
|
|
52
|
13 |
|
if ($fileExists) { |
53
|
10 |
|
$this->configure($config, $json->readAssoc()); |
54
|
|
|
} |
55
|
|
|
|
56
|
13 |
|
return $config; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Initialize the configuration with data load from config file. |
61
|
|
|
* |
62
|
|
|
* @param \SebastianFeldmann\CaptainHook\Config $config |
63
|
|
|
* @param array $json |
64
|
|
|
*/ |
65
|
10 |
|
protected function configure(Config $config, array $json) |
66
|
|
|
{ |
67
|
10 |
|
Util::validateJsonConfiguration($json); |
68
|
|
|
|
69
|
10 |
|
foreach ($json as $hook => $data) { |
70
|
9 |
|
$this->configureHook($config->getHookConfig($hook), $data); |
71
|
|
|
} |
72
|
10 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Setup a hook configuration by json data. |
76
|
|
|
* |
77
|
|
|
* @param \SebastianFeldmann\CaptainHook\Config\Hook $config |
78
|
|
|
* @param array $json |
79
|
|
|
*/ |
80
|
9 |
|
protected function configureHook(Config\Hook $config, array $json) |
81
|
|
|
{ |
82
|
9 |
|
$config->setEnabled($json['enabled']); |
83
|
9 |
|
foreach ($json['actions'] as $actionJson) { |
84
|
7 |
|
$type = HookUtil::getActionType($actionJson['action']); |
85
|
7 |
|
$options = isset($actionJson['options']) && is_array($actionJson['options']) ? $actionJson['options'] : []; |
86
|
7 |
|
$config->addAction(new Config\Action($type, $actionJson['action'], $options)); |
87
|
|
|
} |
88
|
9 |
|
} |
89
|
|
|
} |
90
|
|
|
|