|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Console\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterStyle; |
|
8
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
|
12
|
|
|
use Symfony\Component\Console\Question\Question; |
|
|
|
|
|
|
13
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Trait expect command to set $output and $input scopes. |
|
17
|
|
|
*/ |
|
18
|
|
|
trait HelpersTrait |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* OutputInterface is the interface implemented by all Output classes. Only exists when command |
|
22
|
|
|
* are being executed. |
|
23
|
|
|
* @var SymfonyStyle|null |
|
24
|
|
|
*/ |
|
25
|
|
|
protected ?OutputInterface $output = null; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* InputInterface is the interface implemented by all input classes. Only exists when command |
|
29
|
|
|
* are being executed. |
|
30
|
|
|
*/ |
|
31
|
|
|
protected ?InputInterface $input = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Check if verbosity level of output is higher or equal to VERBOSITY_VERBOSE. |
|
35
|
|
|
*/ |
|
36
|
14 |
|
protected function isVerbose(): bool |
|
37
|
|
|
{ |
|
38
|
14 |
|
return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE; |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Determine if the input option is present. |
|
43
|
|
|
*/ |
|
44
|
33 |
|
protected function hasOption(string $name): bool |
|
45
|
|
|
{ |
|
46
|
33 |
|
return $this->input->hasOption($name); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Input option. |
|
51
|
|
|
*/ |
|
52
|
51 |
|
protected function option(string $name): mixed |
|
53
|
|
|
{ |
|
54
|
51 |
|
return $this->input->getOption($name); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Determine if the input argument is present. |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function hasArgument(string $name): bool |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->input->hasArgument($name); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Input argument. |
|
67
|
|
|
*/ |
|
68
|
53 |
|
protected function argument(string $name): mixed |
|
69
|
|
|
{ |
|
70
|
53 |
|
return $this->input->getArgument($name); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Asks for confirmation. |
|
75
|
|
|
*/ |
|
76
|
2 |
|
protected function confirm(string $question, bool $default = false): bool |
|
77
|
|
|
{ |
|
78
|
2 |
|
return $this->output->confirm($question, $default); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Asks a question. |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function ask(string $question, string $default = null): mixed |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->output->ask($question, $default); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Asks a multiple choice question. |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function choiceQuestion( |
|
93
|
|
|
string $question, |
|
94
|
|
|
array $choices, |
|
95
|
|
|
mixed $default = null, |
|
96
|
|
|
int $attempts = null, |
|
97
|
|
|
bool $multiselect = false |
|
98
|
|
|
): mixed { |
|
99
|
|
|
$question = new ChoiceQuestion($question, $choices, $default); |
|
100
|
|
|
|
|
101
|
|
|
$question->setMaxAttempts($attempts)->setMultiselect($multiselect); |
|
102
|
|
|
|
|
103
|
|
|
return $this->output->askQuestion($question); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Prompt the user for input but hide the answer from the console. |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function secret(string $question, bool $fallback = true): mixed |
|
110
|
|
|
{ |
|
111
|
|
|
$question = new Question($question); |
|
112
|
|
|
|
|
113
|
|
|
$question->setHidden(true)->setHiddenFallback($fallback); |
|
114
|
|
|
|
|
115
|
|
|
return $this->output->askQuestion($question); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Write a string as information output. |
|
120
|
|
|
*/ |
|
121
|
19 |
|
protected function info(string $string): void |
|
122
|
|
|
{ |
|
123
|
19 |
|
$this->line($string, 'info'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Write a string as comment output. |
|
128
|
|
|
*/ |
|
129
|
4 |
|
protected function comment(string $string): void |
|
130
|
|
|
{ |
|
131
|
4 |
|
$this->line($string, 'comment'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Write a string as question output. |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function question(string $string): void |
|
138
|
|
|
{ |
|
139
|
|
|
$this->line($string, 'question'); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Write a string as error output. |
|
144
|
|
|
*/ |
|
145
|
9 |
|
protected function error(string $string): void |
|
146
|
|
|
{ |
|
147
|
9 |
|
$this->line($string, 'error'); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Write a string as warning output. |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function warning(string $string): void |
|
154
|
|
|
{ |
|
155
|
|
|
if (!$this->output->getFormatter()->hasStyle('warning')) { |
|
156
|
|
|
$style = new OutputFormatterStyle('yellow'); |
|
157
|
|
|
$this->output->getFormatter()->setStyle('warning', $style); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$this->line($string, 'warning'); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Write a string in an alert box. |
|
165
|
|
|
*/ |
|
166
|
2 |
|
protected function alert(string $string): void |
|
167
|
|
|
{ |
|
168
|
2 |
|
$length = \mb_strlen(\strip_tags($string)) + 12; |
|
169
|
2 |
|
$stringLines = explode("\n", wordwrap($string, 300)); |
|
170
|
|
|
|
|
171
|
2 |
|
$this->comment(\str_repeat('*', $length)); |
|
172
|
2 |
|
foreach ($stringLines as $line) { |
|
173
|
2 |
|
$this->comment('* ' . $line . ' *'); |
|
174
|
|
|
} |
|
175
|
2 |
|
$this->comment(\str_repeat('*', $length)); |
|
176
|
|
|
|
|
177
|
2 |
|
$this->newLine(); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Write a string as standard output. |
|
182
|
|
|
*/ |
|
183
|
24 |
|
protected function line(string $string, string $style = null): void |
|
184
|
|
|
{ |
|
185
|
24 |
|
$styled = $style ? "<$style>$string</$style>" : $string; |
|
186
|
|
|
|
|
187
|
24 |
|
$this->writeln( |
|
188
|
24 |
|
messages: $styled |
|
189
|
24 |
|
); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Write a blank line. |
|
194
|
|
|
*/ |
|
195
|
21 |
|
protected function newLine(int $count = 1): void |
|
196
|
|
|
{ |
|
197
|
21 |
|
$this->output->newLine($count); |
|
|
|
|
|
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Identical to write function but provides ability to format message. Does not add new line. |
|
202
|
|
|
*/ |
|
203
|
19 |
|
protected function sprintf(string $format, mixed ...$args): void |
|
204
|
|
|
{ |
|
205
|
19 |
|
$this->write( |
|
206
|
19 |
|
messages: \sprintf($format, ...$args), |
|
207
|
19 |
|
newline: false |
|
208
|
19 |
|
); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Writes a message to the output. |
|
213
|
|
|
* |
|
214
|
|
|
* @param string|iterable $messages The message as an array of lines or a single string |
|
215
|
|
|
* @param bool $newline Whether to add a newline |
|
216
|
|
|
* |
|
217
|
|
|
* @throws \InvalidArgumentException When unknown output type is given |
|
218
|
|
|
*/ |
|
219
|
34 |
|
protected function write(string|iterable $messages, bool $newline = false): void |
|
220
|
|
|
{ |
|
221
|
34 |
|
$this->output->write(messages: $messages, newline: $newline); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Writes a message to the output and adds a newline at the end. |
|
226
|
|
|
* |
|
227
|
|
|
* @param string|iterable<mixed, string> $messages The message as an array of lines of a single string |
|
228
|
|
|
* |
|
229
|
|
|
* @throws \InvalidArgumentException When unknown output type is given |
|
230
|
|
|
*/ |
|
231
|
55 |
|
protected function writeln(string|iterable $messages): void |
|
232
|
|
|
{ |
|
233
|
55 |
|
$this->output->writeln(messages: $messages); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Table helper instance with configured header and pre-defined set of rows. |
|
238
|
|
|
*/ |
|
239
|
20 |
|
protected function table(array $headers, array $rows = [], string $style = 'default'): Table |
|
240
|
|
|
{ |
|
241
|
20 |
|
$table = new Table($this->output); |
|
242
|
|
|
|
|
243
|
20 |
|
return $table->setHeaders($headers)->setRows($rows)->setStyle($style); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths