|
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; |
|
11
|
|
|
|
|
12
|
|
|
use SebastianFeldmann\CaptainHook\Config; |
|
13
|
|
|
use SebastianFeldmann\CaptainHook\Console\IOUtil; |
|
14
|
|
|
use SebastianFeldmann\CaptainHook\Hook\Util; |
|
15
|
|
|
use SebastianFeldmann\CaptainHook\Runner; |
|
16
|
|
|
use SebastianFeldmann\CaptainHook\Storage\File\Json; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class Configurator |
|
20
|
|
|
* |
|
21
|
|
|
* @package CaptainHook |
|
22
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
23
|
|
|
* @link https://github.com/sebastianfeldmann/captainhook |
|
24
|
|
|
* @since Class available since Release 0.9.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
class Configurator 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
|
|
|
* Execute the configurator |
|
51
|
|
|
*/ |
|
52
|
4 |
|
public function run() |
|
53
|
|
|
{ |
|
54
|
4 |
|
$config = $this->getConfigToManipulate(); |
|
55
|
3 |
|
$setup = $this->getHookSetup(); |
|
56
|
|
|
|
|
57
|
3 |
|
$setup->configureHooks($config); |
|
58
|
3 |
|
$this->writeConfig($config); |
|
59
|
|
|
|
|
60
|
3 |
|
$this->io->write( |
|
61
|
|
|
[ |
|
62
|
3 |
|
'<info>Configuration created successfully</info>', |
|
63
|
|
|
'Run <comment>\'vendor/bin/captainhook install\'</comment> to activate your hook configuration', |
|
64
|
|
|
] |
|
65
|
|
|
); |
|
66
|
3 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Force mode setter |
|
70
|
|
|
* |
|
71
|
|
|
* @param bool $force |
|
72
|
|
|
* @return \SebastianFeldmann\CaptainHook\Runner\Configurator |
|
73
|
|
|
*/ |
|
74
|
2 |
|
public function force(bool $force) : Configurator |
|
75
|
|
|
{ |
|
76
|
2 |
|
$this->force = $force; |
|
77
|
2 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Set configuration mode |
|
82
|
|
|
* |
|
83
|
|
|
* @param bool $extend |
|
84
|
|
|
* @return \SebastianFeldmann\CaptainHook\Runner\Configurator |
|
85
|
|
|
*/ |
|
86
|
3 |
|
public function extend(bool $extend) : Configurator |
|
87
|
|
|
{ |
|
88
|
3 |
|
$this->mode = $extend ? 'extend' : 'create'; |
|
89
|
3 |
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Set configuration speed |
|
94
|
|
|
* |
|
95
|
|
|
* @param bool $advanced |
|
96
|
|
|
* @return \SebastianFeldmann\CaptainHook\Runner\Configurator |
|
97
|
|
|
*/ |
|
98
|
4 |
|
public function advanced(bool $advanced) : Configurator |
|
99
|
|
|
{ |
|
100
|
4 |
|
$this->advanced = $advanced; |
|
101
|
4 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Return config to handle |
|
106
|
|
|
* |
|
107
|
|
|
* @return \SebastianFeldmann\CaptainHook\Config |
|
108
|
|
|
*/ |
|
109
|
4 |
|
public function getConfigToManipulate() : Config |
|
110
|
|
|
{ |
|
111
|
|
|
// create mode, create blank configuration |
|
112
|
4 |
|
if ('extend' !== $this->mode) { |
|
113
|
|
|
// make sure the force option is set if the configuration file exists |
|
114
|
2 |
|
$this->ensureForce(); |
|
115
|
1 |
|
return new Config($this->config->getPath()); |
|
116
|
|
|
} |
|
117
|
2 |
|
return $this->config; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Return the setup handler to ask the user questions |
|
122
|
|
|
* |
|
123
|
|
|
* @return \SebastianFeldmann\CaptainHook\Runner\Configurator\Setup |
|
124
|
|
|
*/ |
|
125
|
3 |
|
private function getHookSetup() |
|
126
|
|
|
{ |
|
127
|
3 |
|
return $this->advanced |
|
128
|
1 |
|
? new Configurator\Setup\Advanced($this->io) |
|
129
|
3 |
|
: new Configurator\Setup\Express($this->io); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Make sure force mode is set if config file exists |
|
134
|
|
|
* |
|
135
|
|
|
* @throws \RuntimeException |
|
136
|
|
|
*/ |
|
137
|
2 |
|
private function ensureForce() |
|
138
|
|
|
{ |
|
139
|
2 |
|
if ($this->config->isLoadedFromFile() && !$this->force) { |
|
140
|
1 |
|
throw new \RuntimeException('Configuration file exists, use -f to overwrite, or -e to extend'); |
|
141
|
|
|
} |
|
142
|
1 |
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Write config to project root |
|
146
|
|
|
* |
|
147
|
|
|
* @param \SebastianFeldmann\CaptainHook\Config $config |
|
148
|
|
|
*/ |
|
149
|
3 |
|
public function writeConfig(Config $config) |
|
150
|
|
|
{ |
|
151
|
3 |
|
$filePath = $this->config->getPath(); |
|
152
|
3 |
|
$file = new Json($filePath); |
|
153
|
3 |
|
$file->write($config->getJsonData()); |
|
154
|
3 |
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|