1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of CaptainHook |
5
|
|
|
* |
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CaptainHook\App\Config; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Hook |
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 Hook |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Hook name e.g. pre-commit |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $name; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Is hook enabled |
34
|
|
|
* |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
private $isEnabled = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* List of Actions |
41
|
|
|
* |
42
|
|
|
* @var \CaptainHook\App\Config\Action[] |
43
|
|
|
*/ |
44
|
|
|
private $actions = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Hook constructor |
48
|
|
|
* |
49
|
|
|
* @param string $name |
50
|
73 |
|
* @param bool $enabled |
51
|
|
|
*/ |
52
|
73 |
|
public function __construct(string $name, bool $enabled = false) |
53
|
73 |
|
{ |
54
|
73 |
|
$this->name = $name; |
55
|
|
|
$this->isEnabled = $enabled; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Name getter |
60
|
|
|
* |
61
|
5 |
|
* @return string |
62
|
|
|
*/ |
63
|
5 |
|
public function getName(): string |
64
|
|
|
{ |
65
|
|
|
return $this->name; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Enable or disable the hook |
70
|
|
|
* |
71
|
|
|
* @param bool $enabled |
72
|
27 |
|
* @return void |
73
|
|
|
*/ |
74
|
27 |
|
public function setEnabled(bool $enabled): void |
75
|
27 |
|
{ |
76
|
|
|
$this->isEnabled = $enabled; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Is this hook enabled |
81
|
|
|
* |
82
|
19 |
|
* @return bool |
83
|
|
|
*/ |
84
|
19 |
|
public function isEnabled(): bool |
85
|
|
|
{ |
86
|
|
|
return $this->isEnabled; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Add an action to the list |
91
|
|
|
* |
92
|
|
|
* @param \CaptainHook\App\Config\Action $action |
93
|
20 |
|
* @return void |
94
|
|
|
*/ |
95
|
20 |
|
public function addAction(Action $action): void |
96
|
20 |
|
{ |
97
|
|
|
$this->actions[] = $action; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return the action list |
102
|
|
|
* |
103
|
19 |
|
* @return \CaptainHook\App\Config\Action[] |
104
|
|
|
*/ |
105
|
19 |
|
public function getActions(): array |
106
|
|
|
{ |
107
|
|
|
return $this->actions; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Return config data |
112
|
|
|
* |
113
|
12 |
|
* @return array |
114
|
|
|
*/ |
115
|
12 |
|
public function getJsonData(): array |
116
|
12 |
|
{ |
117
|
4 |
|
$config = ['enabled' => $this->isEnabled, 'actions' => []]; |
118
|
|
|
foreach ($this->actions as $action) { |
119
|
12 |
|
$config['actions'][] = $action->getJsonData(); |
120
|
|
|
} |
121
|
|
|
return $config; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|