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
|
3 |
|
public function beforeAction() |
73
|
|
|
{ |
74
|
|
|
// empty template method |
75
|
3 |
|
} |
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->executeAction($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 executeAction(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 |
|
$type = $action->getType(); |
136
|
3 |
|
$runner = self::getActionRunner($type); |
137
|
|
|
|
138
|
3 |
|
$this->beforeAction(); |
139
|
3 |
|
$runner->execute($this->config, $this->io, $this->repository, $action); |
140
|
|
|
|
141
|
|
|
// execute post handling only for php actions |
142
|
|
|
// cli actions should do post actions them self |
143
|
3 |
|
if ($type === 'php') { |
144
|
1 |
|
$this->afterAction(); |
145
|
|
|
} |
146
|
|
|
|
147
|
3 |
|
$this->io->write([str_repeat('-', 80)]); |
148
|
3 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Return matching action runner. |
152
|
|
|
* |
153
|
|
|
* @param string $type |
154
|
|
|
* @return \CaptainHook\App\Hook\Action |
155
|
|
|
* @throws \RuntimeException |
156
|
|
|
*/ |
157
|
5 |
|
public static function getActionRunner($type) : ActionInterface |
158
|
|
|
{ |
159
|
|
|
switch ($type) { |
160
|
5 |
|
case 'php': |
161
|
2 |
|
return new Action\PHP(); |
162
|
4 |
|
case 'cli': |
163
|
3 |
|
return new Action\Cli(); |
164
|
|
|
default: |
165
|
1 |
|
throw new \RuntimeException('Unknown action type: ' . $type); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|