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\Console\IO; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\Console\IO; |
15
|
|
|
use CaptainHook\App\Console\IOUtil; |
16
|
|
|
use RuntimeException; |
17
|
|
|
use SebastianFeldmann\Cli\Reader\StandardInput; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Helper\HelperSet; |
20
|
|
|
use Symfony\Component\Console\Output\ConsoleOutputInterface; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
23
|
|
|
use Symfony\Component\Console\Question\Question; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class DefaultIO |
27
|
|
|
* |
28
|
|
|
* @package CaptainHook |
29
|
|
|
* @author Sebastian Feldmann <[email protected]> |
30
|
|
|
* @link https://github.com/captainhook-git/captainhook |
31
|
|
|
* @since Class available since Release 0.9.0 |
32
|
|
|
*/ |
33
|
|
|
class DefaultIO extends Base |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Contents of the STDIN |
37
|
|
|
* |
38
|
|
|
* @var array<string> |
39
|
|
|
*/ |
40
|
|
|
private array $stdIn = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \Symfony\Component\Console\Input\InputInterface |
44
|
|
|
*/ |
45
|
|
|
protected InputInterface $input; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \Symfony\Component\Console\Output\OutputInterface |
49
|
|
|
*/ |
50
|
|
|
protected OutputInterface $output; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var \Symfony\Component\Console\Helper\HelperSet|null |
54
|
|
|
*/ |
55
|
|
|
protected ?HelperSet $helperSet; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var array<int, int> |
59
|
|
|
*/ |
60
|
|
|
private array $verbosityMap; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Constructor |
64
|
|
|
* |
65
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
66
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
67
|
|
|
* @param \Symfony\Component\Console\Helper\HelperSet|null $helperSet |
68
|
|
|
*/ |
69
|
49 |
|
public function __construct(InputInterface $input, OutputInterface $output, ?HelperSet $helperSet = null) |
70
|
|
|
{ |
71
|
49 |
|
$this->input = $input; |
72
|
49 |
|
$this->output = $output; |
73
|
49 |
|
$this->helperSet = $helperSet; |
74
|
49 |
|
$this->verbosityMap = [ |
75
|
49 |
|
IO::QUIET => OutputInterface::VERBOSITY_QUIET, |
76
|
49 |
|
IO::NORMAL => OutputInterface::VERBOSITY_NORMAL, |
77
|
49 |
|
IO::VERBOSE => OutputInterface::VERBOSITY_VERBOSE, |
78
|
49 |
|
IO::VERY_VERBOSE => OutputInterface::VERBOSITY_VERY_VERBOSE, |
79
|
49 |
|
IO::DEBUG => OutputInterface::VERBOSITY_DEBUG |
80
|
49 |
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return the original cli arguments |
85
|
|
|
* |
86
|
|
|
* @return array<string, mixed> |
87
|
|
|
*/ |
88
|
4 |
|
public function getArguments(): array |
89
|
|
|
{ |
90
|
4 |
|
return $this->input->getArguments(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return the original cli argument or a given default |
95
|
|
|
* |
96
|
|
|
* @param string $name |
97
|
|
|
* @param string $default |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
3 |
|
public function getArgument(string $name, string $default = ''): string |
101
|
|
|
{ |
102
|
3 |
|
return (string)($this->getArguments()[$name] ?? $default); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return the piped in standard input |
107
|
|
|
* |
108
|
|
|
* @return string[] |
109
|
|
|
*/ |
110
|
1 |
|
public function getStandardInput(): array |
111
|
|
|
{ |
112
|
1 |
|
if (empty($this->stdIn)) { |
113
|
1 |
|
$this->stdIn = explode(PHP_EOL, trim($this->input->getOption('input'), '"')); |
114
|
|
|
} |
115
|
1 |
|
return $this->stdIn; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritDoc} |
120
|
|
|
*/ |
121
|
1 |
|
public function isInteractive() |
122
|
|
|
{ |
123
|
1 |
|
return $this->input->isInteractive(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritDoc} |
128
|
|
|
*/ |
129
|
12 |
|
public function isVerbose() |
130
|
|
|
{ |
131
|
12 |
|
return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritDoc} |
136
|
|
|
*/ |
137
|
12 |
|
public function isVeryVerbose() |
138
|
|
|
{ |
139
|
12 |
|
return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritDoc} |
144
|
|
|
*/ |
145
|
12 |
|
public function isDebug() |
146
|
|
|
{ |
147
|
12 |
|
return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritDoc} |
152
|
|
|
*/ |
153
|
22 |
|
public function write($messages, $newline = true, $verbosity = self::NORMAL) |
154
|
|
|
{ |
155
|
22 |
|
$this->doWrite($messages, $newline, false, $verbosity); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritDoc} |
160
|
|
|
*/ |
161
|
3 |
|
public function writeError($messages, $newline = true, $verbosity = self::NORMAL) |
162
|
|
|
{ |
163
|
3 |
|
$this->doWrite($messages, $newline, true, $verbosity); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Write to the appropriate user output |
168
|
|
|
* |
169
|
|
|
* @param array<string>|string $messages |
170
|
|
|
* @param bool $newline |
171
|
|
|
* @param bool $stderr |
172
|
|
|
* @param int $verbosity |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
25 |
|
private function doWrite($messages, $newline, $stderr, $verbosity) |
176
|
|
|
{ |
177
|
25 |
|
$sfVerbosity = $this->verbosityMap[$verbosity]; |
178
|
25 |
|
if ($sfVerbosity > $this->output->getVerbosity()) { |
179
|
23 |
|
return; |
180
|
|
|
} |
181
|
|
|
|
182
|
2 |
|
$this->getOutputToWriteTo($stderr)->write($messages, $newline, $sfVerbosity); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritDoc} |
187
|
|
|
*/ |
188
|
2 |
|
public function ask($question, $default = null) |
189
|
|
|
{ |
190
|
2 |
|
if ($this->helperSet === null) { |
191
|
1 |
|
throw new RuntimeException('You must set the helperSet before asking'); |
192
|
|
|
} |
193
|
|
|
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */ |
194
|
1 |
|
$helper = $this->helperSet->get('question'); |
195
|
1 |
|
$question = new Question($question, $default); |
196
|
|
|
|
197
|
1 |
|
return $helper->ask($this->input, $this->getOutputToWriteTo(), $question); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritDoc} |
202
|
|
|
*/ |
203
|
2 |
|
public function askConfirmation($question, $default = true) |
204
|
|
|
{ |
205
|
2 |
|
if ($this->helperSet === null) { |
206
|
1 |
|
throw new RuntimeException('You must set the helperSet before asking'); |
207
|
|
|
} |
208
|
|
|
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */ |
209
|
1 |
|
$helper = $this->helperSet->get('question'); |
210
|
1 |
|
$question = new ConfirmationQuestion($question, $default); |
211
|
|
|
|
212
|
1 |
|
return IOUtil::answerToBool($helper->ask($this->input, $this->getOutputToWriteTo(), $question)); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* {@inheritDoc} |
217
|
|
|
*/ |
218
|
2 |
|
public function askAndValidate($question, $validator, $attempts = null, $default = null) |
219
|
|
|
{ |
220
|
2 |
|
if ($this->helperSet === null) { |
221
|
1 |
|
throw new RuntimeException('You must set the helperSet before asking'); |
222
|
|
|
} |
223
|
|
|
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */ |
224
|
1 |
|
$helper = $this->helperSet->get('question'); |
225
|
1 |
|
$question = new Question($question, $default); |
226
|
1 |
|
$question->setValidator($validator); |
227
|
1 |
|
$question->setMaxAttempts($attempts); |
228
|
|
|
|
229
|
1 |
|
return $helper->ask($this->input, $this->getOutputToWriteTo(), $question); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Return the output to write to |
234
|
|
|
* |
235
|
|
|
* @param bool $stdErr |
236
|
|
|
* @return \Symfony\Component\Console\Output\OutputInterface |
237
|
|
|
*/ |
238
|
5 |
|
private function getOutputToWriteTo($stdErr = false) |
239
|
|
|
{ |
240
|
5 |
|
if ($stdErr && $this->output instanceof ConsoleOutputInterface) { |
241
|
1 |
|
return $this->output->getErrorOutput(); |
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
4 |
|
return $this->output; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|