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\Runner\Config; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\Config; |
15
|
|
|
use CaptainHook\App\Runner; |
16
|
|
|
use RuntimeException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Configurator |
20
|
|
|
* |
21
|
|
|
* @package CaptainHook |
22
|
|
|
* @author Sebastian Feldmann <[email protected]> |
23
|
|
|
* @link https://github.com/captainhookphp/captainhook |
24
|
|
|
* @since Class available since Release 0.9.0 |
25
|
|
|
*/ |
26
|
|
|
class Creator extends Runner |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Force mode |
30
|
|
|
* |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
private $force; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Extend existing config or create new one |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $mode; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Use express setup mode |
44
|
|
|
* |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
private $advanced; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Path to the currently executed 'binary' |
51
|
4 |
|
* |
52
|
|
|
* @var string |
53
|
4 |
|
*/ |
54
|
3 |
|
protected $executable; |
55
|
|
|
|
56
|
3 |
|
/** |
57
|
3 |
|
* Execute the configurator |
58
|
|
|
* |
59
|
3 |
|
* @return void |
60
|
|
|
*/ |
61
|
3 |
|
public function run(): void |
62
|
|
|
{ |
63
|
|
|
$config = $this->getConfigToManipulate(); |
64
|
|
|
$setup = $this->getHookSetup(); |
65
|
3 |
|
|
66
|
|
|
$setup->configureHooks($config); |
67
|
|
|
Config\Util::writeToDisk($config); |
68
|
|
|
|
69
|
|
|
$this->io->write( |
70
|
|
|
[ |
71
|
|
|
'<info>Configuration created successfully</info>', |
72
|
|
|
'Run <comment>\'' . $this->getExecutable() . ' install\'</comment> to activate your hook configuration', |
73
|
2 |
|
] |
74
|
|
|
); |
75
|
2 |
|
} |
76
|
2 |
|
|
77
|
|
|
/** |
78
|
|
|
* Force mode setter |
79
|
|
|
* |
80
|
|
|
* @param bool $force |
81
|
|
|
* @return \CaptainHook\App\Runner\Config\Creator |
82
|
|
|
*/ |
83
|
|
|
public function force(bool $force): Creator |
84
|
|
|
{ |
85
|
3 |
|
$this->force = $force; |
86
|
|
|
return $this; |
87
|
3 |
|
} |
88
|
3 |
|
|
89
|
|
|
/** |
90
|
|
|
* Set configuration mode |
91
|
|
|
* |
92
|
|
|
* @param bool $extend |
93
|
|
|
* @return \CaptainHook\App\Runner\Config\Creator |
94
|
|
|
*/ |
95
|
|
|
public function extend(bool $extend): Creator |
96
|
|
|
{ |
97
|
4 |
|
$this->mode = $extend ? 'extend' : 'create'; |
98
|
|
|
return $this; |
99
|
4 |
|
} |
100
|
4 |
|
|
101
|
|
|
/** |
102
|
|
|
* Set configuration speed |
103
|
|
|
* |
104
|
|
|
* @param bool $advanced |
105
|
|
|
* @return \CaptainHook\App\Runner\Config\Creator |
106
|
|
|
*/ |
107
|
|
|
public function advanced(bool $advanced): Creator |
108
|
4 |
|
{ |
109
|
|
|
$this->advanced = $advanced; |
110
|
4 |
|
return $this; |
111
|
|
|
} |
112
|
3 |
|
|
113
|
|
|
/** |
114
|
2 |
|
* Set the currently executed 'binary' |
115
|
|
|
* |
116
|
1 |
|
* @param string $executable |
117
|
|
|
* @return \CaptainHook\App\Runner\Config\Creator |
118
|
|
|
*/ |
119
|
|
|
public function setExecutable(string $executable): Creator |
120
|
|
|
{ |
121
|
|
|
$this->executable = $executable; |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
3 |
|
|
125
|
|
|
/** |
126
|
3 |
|
* Return config to handle |
127
|
1 |
|
* |
128
|
3 |
|
* @return \CaptainHook\App\Config |
129
|
|
|
*/ |
130
|
|
|
public function getConfigToManipulate(): Config |
131
|
|
|
{ |
132
|
|
|
if (!$this->isExtending()) { |
133
|
|
|
// make sure the force option is set if the configuration file exists |
134
|
|
|
$this->ensureForce(); |
135
|
|
|
// create a blank configuration to overwrite the old one |
136
|
4 |
|
return new Config($this->config->getPath()); |
137
|
|
|
} |
138
|
4 |
|
return $this->config; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Return the setup handler to ask the user questions |
143
|
|
|
* |
144
|
|
|
* @return \CaptainHook\App\Runner\Config\Setup |
145
|
|
|
*/ |
146
|
|
|
private function getHookSetup(): Setup |
147
|
3 |
|
{ |
148
|
|
|
return $this->advanced |
149
|
3 |
|
? new Setup\Advanced($this->io) |
150
|
1 |
|
: new Setup\Express($this->io); |
151
|
|
|
} |
152
|
2 |
|
|
153
|
|
|
/** |
154
|
|
|
* Should the config file be extended |
155
|
|
|
* |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
private function isExtending(): bool |
159
|
|
|
{ |
160
|
|
|
return 'extend' === $this->mode; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Make sure force mode is set if config file exists |
165
|
|
|
* |
166
|
|
|
* @return void |
167
|
|
|
* @throws \RuntimeException |
168
|
|
|
*/ |
169
|
|
|
private function ensureForce(): void |
170
|
|
|
{ |
171
|
|
|
if ($this->config->isLoadedFromFile() && !$this->force) { |
172
|
|
|
throw new RuntimeException('Configuration file exists, use -f to overwrite, or -e to extend'); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Return path to currently executed 'binary' |
178
|
|
|
* |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
private function getExecutable() |
182
|
|
|
{ |
183
|
|
|
return $this->executable ?? 'vendor/bin/captainhook'; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|