|
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\Runner; |
|
11
|
|
|
|
|
12
|
|
|
use CaptainHook\App\Config; |
|
13
|
|
|
use CaptainHook\App\Config\Options; |
|
14
|
|
|
use CaptainHook\App\Console\IO; |
|
15
|
|
|
use CaptainHook\App\Console\IOUtil; |
|
16
|
|
|
use CaptainHook\App\Hook\Action as ActionInterface; |
|
17
|
|
|
use SebastianFeldmann\Git\Repository; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Hook |
|
21
|
|
|
* |
|
22
|
|
|
* @package CaptainHook |
|
23
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
24
|
|
|
* @link https://github.com/sebastianfeldmann/captainhook |
|
25
|
|
|
* @since Class available since Release 0.9.0 |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class Hook extends RepositoryAware |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Hook that should be handled. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $hook; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* List of original hook arguments |
|
38
|
|
|
* |
|
39
|
|
|
* @var \CaptainHook\App\Config\Options |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $arguments; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* HookHandler constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param \CaptainHook\App\Console\IO $io |
|
47
|
|
|
* @param \CaptainHook\App\Config $config |
|
48
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
|
49
|
|
|
* @param \CaptainHook\App\Config\Options $arguments |
|
50
|
|
|
*/ |
|
51
|
12 |
|
public function __construct(IO $io, Config $config, Repository $repository, Options $arguments) |
|
52
|
|
|
{ |
|
53
|
12 |
|
parent::__construct($io, $config, $repository); |
|
54
|
12 |
|
$this->arguments = $arguments; |
|
55
|
12 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Execute stuff before executing any actions |
|
59
|
|
|
* |
|
60
|
|
|
* @return void |
|
61
|
|
|
*/ |
|
62
|
10 |
|
public function beforeHook() |
|
63
|
|
|
{ |
|
64
|
|
|
// empty template method |
|
65
|
10 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Execute stuff before every actions |
|
69
|
|
|
* |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function beforeAction() |
|
73
|
|
|
{ |
|
74
|
|
|
// empty template method |
|
75
|
1 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Execute stuff after every actions |
|
79
|
|
|
* |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function afterAction() |
|
83
|
|
|
{ |
|
84
|
|
|
//empty template method |
|
85
|
1 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Execute stuff after all actions |
|
89
|
|
|
* |
|
90
|
|
|
* @return void |
|
91
|
|
|
*/ |
|
92
|
9 |
|
public function afterHook() |
|
93
|
|
|
{ |
|
94
|
|
|
// empty template method |
|
95
|
9 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Execute installation. |
|
99
|
|
|
* |
|
100
|
|
|
* @throws \Exception |
|
101
|
|
|
*/ |
|
102
|
12 |
|
public function run() |
|
103
|
|
|
{ |
|
104
|
12 |
|
$this->beforeHook(); |
|
105
|
|
|
/** @var \CaptainHook\App\Config\Hook $hookConfig */ |
|
106
|
10 |
|
$hookConfig = $this->config->getHookConfig($this->hook); |
|
107
|
|
|
|
|
108
|
|
|
// if hook is not enabled in captainhook.json skip action execution |
|
109
|
10 |
|
if (!$hookConfig->isEnabled()) { |
|
110
|
1 |
|
$this->io->write('<info>skip hook:</info> <comment>' . $this->hook . '</comment>'); |
|
111
|
1 |
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
9 |
|
$this->io->write(['', '<info>execute hook:</info> <comment>' . $this->hook . '</comment>']); |
|
115
|
9 |
|
foreach ($hookConfig->getActions() as $action) { |
|
116
|
3 |
|
$this->handleAction($action); |
|
117
|
|
|
} |
|
118
|
9 |
|
$this->afterHook(); |
|
119
|
9 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Executes a configured hook action |
|
123
|
|
|
* |
|
124
|
|
|
* @param \CaptainHook\App\Config\Action $action |
|
125
|
|
|
* @throws \Exception |
|
126
|
|
|
*/ |
|
127
|
3 |
|
protected function handleAction(Config\Action $action) |
|
128
|
|
|
{ |
|
129
|
3 |
|
$this->io->write([ |
|
130
|
3 |
|
'', |
|
131
|
3 |
|
'Action: <comment>' . $action->getAction() . '</comment>', |
|
132
|
3 |
|
IOUtil::getLineSeparator() |
|
133
|
|
|
]); |
|
134
|
|
|
|
|
135
|
3 |
|
$execMethod = self::getExecMethod($action->getType()); |
|
136
|
3 |
|
$this->{$execMethod}($action); |
|
137
|
|
|
|
|
138
|
3 |
|
$this->io->write([str_repeat('-', 80)]); |
|
139
|
3 |
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Execute a php hook action |
|
143
|
|
|
* |
|
144
|
|
|
* @param \CaptainHook\App\Config\Action $action |
|
145
|
|
|
* @throws \CaptainHook\App\Exception\ActionFailed |
|
146
|
|
|
*/ |
|
147
|
1 |
|
protected function executePhpAction(Config\Action $action) |
|
148
|
|
|
{ |
|
149
|
1 |
|
$this->beforeAction(); |
|
150
|
1 |
|
$runner = new Action\PHP(); |
|
151
|
1 |
|
$runner->execute($this->config, $this->io, $this->repository, $action); |
|
152
|
1 |
|
$this->afterAction(); |
|
153
|
1 |
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Execute a cli hook action |
|
157
|
|
|
* |
|
158
|
|
|
* @param \CaptainHook\App\Config\Action $action |
|
159
|
|
|
* @throws \CaptainHook\App\Exception\ActionFailed |
|
160
|
|
|
*/ |
|
161
|
2 |
|
protected function executeCliAction(Config\Action $action) |
|
162
|
|
|
{ |
|
163
|
|
|
// since the cli has no straight way to communicate back to php |
|
164
|
|
|
// cli hooks have to handle sync stuff by them self |
|
165
|
|
|
// so no 'beforeAction' or 'afterAction' is called here |
|
166
|
2 |
|
$runner = new Action\Cli(); |
|
167
|
2 |
|
$runner->execute($this->io, $action, $this->arguments); |
|
168
|
2 |
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Return the right method to call to execute an action |
|
172
|
|
|
* |
|
173
|
|
|
* @param string $type |
|
174
|
|
|
* @return string |
|
175
|
|
|
*/ |
|
176
|
5 |
|
public static function getExecMethod(string $type) : string |
|
177
|
|
|
{ |
|
178
|
5 |
|
$valid = ['php' => 'executePhpAction', 'cli' => 'executeCliAction']; |
|
179
|
|
|
|
|
180
|
5 |
|
if (!isset($valid[$type])) { |
|
181
|
1 |
|
throw new \RuntimeException('invalid action type: ' . $type); |
|
182
|
|
|
} |
|
183
|
4 |
|
return $valid[$type]; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|