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\Exception; |
16
|
|
|
use CaptainHook\App\Hook\Util; |
17
|
|
|
use CaptainHook\App\Runner; |
18
|
|
|
use RuntimeException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Editor |
22
|
|
|
* |
23
|
|
|
* @package CaptainHook |
24
|
|
|
* @author Sebastian Feldmann <[email protected]> |
25
|
|
|
* @link https://github.com/captainhook-git/captainhook |
26
|
|
|
* @since Class available since Release 4.2.0 |
27
|
|
|
*/ |
28
|
|
|
class Editor extends Runner |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $hookToEdit; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The name of the change that will be applied to the configuration |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $change; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set the hook that should be changed |
44
|
|
|
* |
45
|
|
|
* @param string $hook |
46
|
|
|
* @return \CaptainHook\App\Runner\Config\Editor |
47
|
|
|
* @throws \CaptainHook\App\Exception\InvalidHookName |
48
|
|
|
*/ |
49
|
8 |
|
public function setHook(string $hook): Editor |
50
|
|
|
{ |
51
|
8 |
|
if (!Util::isValid($hook)) { |
52
|
1 |
|
throw new Exception\InvalidHookName('Invalid hook name \'' . $hook . '\''); |
53
|
|
|
} |
54
|
7 |
|
$this->hookToEdit = $hook; |
55
|
7 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set the name of the change that should be performed on the configuration |
60
|
|
|
* |
61
|
|
|
* @param string $change |
62
|
|
|
* @return \CaptainHook\App\Runner\Config\Editor |
63
|
|
|
*/ |
64
|
7 |
|
public function setChange(string $change): Editor |
65
|
|
|
{ |
66
|
7 |
|
$this->change = $change; |
67
|
7 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Executes the Runner |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
* @throws \RuntimeException |
75
|
|
|
*/ |
76
|
9 |
|
public function run(): void |
77
|
|
|
{ |
78
|
9 |
|
if (!$this->config->isLoadedFromFile()) { |
79
|
3 |
|
throw new RuntimeException('No configuration to edit'); |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
$this->checkHook(); |
83
|
5 |
|
$this->checkChange(); |
84
|
|
|
|
85
|
4 |
|
$change = $this->createChange(); |
86
|
3 |
|
$change->applyTo($this->config); |
87
|
3 |
|
Config\Util::writeToDisk($this->config); |
88
|
|
|
|
89
|
3 |
|
$this->io->write('Configuration successfully updated'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Create a config edit command |
94
|
|
|
* |
95
|
|
|
* @return \CaptainHook\App\Runner\Config\Change |
96
|
|
|
* @throws \RuntimeException |
97
|
|
|
*/ |
98
|
4 |
|
private function createChange(): Change |
99
|
|
|
{ |
100
|
|
|
/** @var class-string<\CaptainHook\App\Runner\Config\Change> $changeClass */ |
101
|
4 |
|
$changeClass = '\\CaptainHook\\App\\Runner\\Config\\Change\\' . $this->change; |
102
|
4 |
|
if (!class_exists($changeClass)) { |
103
|
1 |
|
throw new RuntimeException('Invalid change requested'); |
104
|
|
|
} |
105
|
|
|
|
106
|
3 |
|
return new $changeClass($this->io, $this->hookToEdit); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Makes sure a hook is selected |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
* @throws \RuntimeException |
114
|
|
|
*/ |
115
|
6 |
|
private function checkHook(): void |
116
|
|
|
{ |
117
|
6 |
|
if (empty($this->hookToEdit)) { |
118
|
1 |
|
throw new RuntimeException('No hook set'); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Makes sure a command is set |
124
|
|
|
* |
125
|
|
|
* @return void |
126
|
|
|
* @throws \RuntimeException |
127
|
|
|
*/ |
128
|
5 |
|
private function checkChange(): void |
129
|
|
|
{ |
130
|
5 |
|
if (empty($this->change)) { |
131
|
1 |
|
throw new RuntimeException('No change set'); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|