|
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\Runner\Configurator\Setup; |
|
11
|
|
|
|
|
12
|
|
|
use SebastianFeldmann\CaptainHook\Config; |
|
13
|
|
|
use SebastianFeldmann\CaptainHook\Console\IOUtil; |
|
14
|
|
|
use SebastianFeldmann\CaptainHook\Hook\Util; |
|
15
|
|
|
use SebastianFeldmann\CaptainHook\Runner\Configurator\Setup; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Advanced |
|
19
|
|
|
* |
|
20
|
|
|
* @package CaptainHook |
|
21
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
22
|
|
|
* @link https://github.com/sebastianfeldmann/captainhook |
|
23
|
|
|
* @since Class available since Release 2.2.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
class Advanced extends Guided implements Setup |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Setup hook configurations by asking some questions |
|
29
|
|
|
* |
|
30
|
|
|
* @param \SebastianFeldmann\CaptainHook\Config $config |
|
31
|
|
|
* @return void |
|
32
|
|
|
* @throws \Exception |
|
33
|
|
|
*/ |
|
34
|
3 |
|
public function configureHooks(Config $config) |
|
35
|
|
|
{ |
|
36
|
3 |
|
foreach (Util::getHooks() as $hook) { |
|
37
|
3 |
|
$this->configureHook($config->getHookConfig($hook), $hook); |
|
38
|
|
|
} |
|
39
|
3 |
|
} |
|
40
|
|
|
/** |
|
41
|
|
|
* Configure a hook by asking some questions |
|
42
|
|
|
* |
|
43
|
|
|
* @param \SebastianFeldmann\CaptainHook\Config\Hook $config |
|
44
|
|
|
* @param string $name |
|
45
|
|
|
* @throws \Exception |
|
46
|
|
|
*/ |
|
47
|
3 |
|
public function configureHook(Config\Hook $config, string $name) |
|
48
|
|
|
{ |
|
49
|
3 |
|
$answer = $this->io->ask(' <info>Enable \'' . $name . '\' hook?</info> <comment>[y,n]</comment> ', 'n'); |
|
50
|
3 |
|
$enable = IOUtil::answerToBool($answer); |
|
51
|
|
|
|
|
52
|
3 |
|
$config->setEnabled($enable); |
|
53
|
|
|
|
|
54
|
3 |
|
if ($enable) { |
|
55
|
3 |
|
$addAction = $this->io->ask(' <info>Add a validation action?</info> <comment>[y,n]</comment> ', 'n'); |
|
56
|
|
|
|
|
57
|
3 |
|
while (IOUtil::answerToBool($addAction)) { |
|
58
|
3 |
|
$config->addAction($this->getActionConfig()); |
|
59
|
|
|
// add another action? |
|
60
|
3 |
|
$addAction = $this->io->ask( |
|
61
|
3 |
|
' <info>Add another validation action?</info> <comment>[y,n]</comment> ', |
|
62
|
3 |
|
'n' |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
3 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Setup a action config with user input |
|
70
|
|
|
* |
|
71
|
|
|
* @return \SebastianFeldmann\CaptainHook\Config\Action |
|
72
|
|
|
* @throws \Exception |
|
73
|
|
|
*/ |
|
74
|
3 |
|
public function getActionConfig() : Config\Action |
|
75
|
|
|
{ |
|
76
|
3 |
|
$call = $this->io->ask(' <info>PHP class or shell command to execute?</info> ', ''); |
|
77
|
3 |
|
$type = Util::getActionType($call); |
|
78
|
3 |
|
$options = $this->getActionOptions($type); |
|
79
|
|
|
|
|
80
|
3 |
|
return new Config\Action($type, $call, $options); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Ask the user for any action options |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $type |
|
87
|
|
|
* @return array |
|
88
|
|
|
* @throws \Exception |
|
89
|
|
|
*/ |
|
90
|
3 |
|
public function getActionOptions(string $type) : array |
|
91
|
|
|
{ |
|
92
|
3 |
|
return 'php' === $type ? $this->getPHPActionOptions() : []; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get the php action options |
|
97
|
|
|
* |
|
98
|
|
|
* @return array |
|
99
|
|
|
* @throws \Exception |
|
100
|
|
|
*/ |
|
101
|
2 |
|
protected function getPHPActionOptions() : array |
|
102
|
|
|
{ |
|
103
|
2 |
|
$options = []; |
|
104
|
2 |
|
$addOption = $this->io->ask(' <info>Add a validator option?</info> <comment>[y,n]</comment> ', 'n'); |
|
105
|
2 |
|
while (IOUtil::answerToBool($addOption)) { |
|
106
|
2 |
|
$options = array_merge($options, $this->getPHPActionOption()); |
|
107
|
|
|
// add another action? |
|
108
|
2 |
|
$addOption = $this->io->ask(' <info>Add another validator option?</info> <comment>[y,n]</comment> ', 'n'); |
|
109
|
|
|
} |
|
110
|
2 |
|
return $options; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Ask the user for a php action option |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
* @throws \Exception |
|
118
|
|
|
*/ |
|
119
|
2 |
|
protected function getPHPActionOption() : array |
|
120
|
|
|
{ |
|
121
|
2 |
|
$result = []; |
|
122
|
2 |
|
$answer = $this->io->askAndValidate( |
|
123
|
2 |
|
' <info>Specify options key and value</info> <comment>[key:value]</comment> ', |
|
124
|
2 |
|
['\\SebastianFeldmann\\CaptainHook\\Runner\\Configurator\\Setup\\Guided', 'isPHPActionOptionValid'], |
|
125
|
2 |
|
3, |
|
126
|
2 |
|
null |
|
127
|
|
|
); |
|
128
|
2 |
|
if (null !== $answer) { |
|
129
|
2 |
|
list($key, $value) = explode(':', $answer); |
|
130
|
2 |
|
$result = [$key => $value]; |
|
131
|
|
|
} |
|
132
|
2 |
|
return $result; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|