|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the webmozart/console package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[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 Webmozart\Console\Api\IO; |
|
13
|
|
|
|
|
14
|
|
|
use Webmozart\Console\Api\Formatter\Formatter; |
|
15
|
|
|
use Webmozart\Console\Api\Formatter\Style; |
|
16
|
|
|
use Webmozart\Console\UI\Rectangle; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Provides methods to access the console input and output. |
|
20
|
|
|
* |
|
21
|
|
|
* @since 1.0 |
|
22
|
|
|
* |
|
23
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class IO implements Formatter |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Flag: Always write data. |
|
29
|
|
|
*/ |
|
30
|
|
|
const NORMAL = 0; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Flag: Only write if the verbosity is "verbose" or greater. |
|
34
|
|
|
*/ |
|
35
|
|
|
const VERBOSE = 1; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Flag: Only write if the verbosity is "very verbose" or greater. |
|
39
|
|
|
*/ |
|
40
|
|
|
const VERY_VERBOSE = 2; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Flag: Only write if the verbosity is "debug". |
|
44
|
|
|
*/ |
|
45
|
|
|
const DEBUG = 4; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var Input |
|
49
|
|
|
*/ |
|
50
|
|
|
private $input; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var Output |
|
54
|
|
|
*/ |
|
55
|
|
|
private $output; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var Output |
|
59
|
|
|
*/ |
|
60
|
|
|
private $errorOutput; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var Rectangle |
|
64
|
|
|
*/ |
|
65
|
|
|
private $terminalDimensions; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Creates an I/O based on the given input and outputs. |
|
69
|
|
|
* |
|
70
|
|
|
* @param Input $input The standard input. |
|
71
|
|
|
* @param Output $output The standard output. |
|
72
|
|
|
* @param Output $errorOutput The error output. |
|
73
|
|
|
*/ |
|
74
|
231 |
|
public function __construct(Input $input, Output $output, Output $errorOutput) |
|
75
|
|
|
{ |
|
76
|
231 |
|
$this->input = $input; |
|
77
|
231 |
|
$this->output = $output; |
|
78
|
231 |
|
$this->errorOutput = $errorOutput; |
|
79
|
231 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Returns the standard input. |
|
83
|
|
|
* |
|
84
|
|
|
* @return Input The input. |
|
85
|
|
|
*/ |
|
86
|
4 |
|
public function getInput() |
|
87
|
|
|
{ |
|
88
|
4 |
|
return $this->input; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Returns the standard output. |
|
93
|
|
|
* |
|
94
|
|
|
* @return Output The output. |
|
95
|
|
|
*/ |
|
96
|
94 |
|
public function getOutput() |
|
97
|
|
|
{ |
|
98
|
94 |
|
return $this->output; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns the error output. |
|
103
|
|
|
* |
|
104
|
|
|
* @return Output The error output. |
|
105
|
|
|
*/ |
|
106
|
7 |
|
public function getErrorOutput() |
|
107
|
|
|
{ |
|
108
|
7 |
|
return $this->errorOutput; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Reads the given amount of characters from the standard input. |
|
113
|
|
|
* |
|
114
|
|
|
* @param int $length The number of characters to read. |
|
115
|
|
|
* @param string $default The default to return if interaction is disabled. |
|
|
|
|
|
|
116
|
|
|
* |
|
117
|
|
|
* @return string The characters read from the input. |
|
|
|
|
|
|
118
|
|
|
* |
|
119
|
|
|
* @throws IOException If reading fails or if the standard input is closed. |
|
120
|
|
|
*/ |
|
121
|
2 |
|
public function read($length, $default = null) |
|
122
|
|
|
{ |
|
123
|
2 |
|
return $this->input->read($length, $default); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Reads a line from the standard input. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $default The default to return if interaction is disabled. |
|
|
|
|
|
|
130
|
|
|
* @param int $length The maximum number of characters to read. If |
|
|
|
|
|
|
131
|
|
|
* `null`, all characters up to the first newline are |
|
132
|
|
|
* returned. |
|
133
|
|
|
* |
|
134
|
|
|
* @return string The characters read from the input. |
|
|
|
|
|
|
135
|
|
|
* |
|
136
|
|
|
* @throws IOException If reading fails or if the standard input is closed. |
|
137
|
|
|
*/ |
|
138
|
11 |
|
public function readLine($default = null, $length = null) |
|
139
|
|
|
{ |
|
140
|
11 |
|
return $this->input->readLine($default, $length); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Writes a string to the standard output. |
|
145
|
|
|
* |
|
146
|
|
|
* The string is formatted before it is written to the output. |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $string The string to write. |
|
149
|
|
|
* @param int $flags The flags. One of {@link VERBOSE}, |
|
|
|
|
|
|
150
|
|
|
* {@link VERY_VERBOSE} and {@link DEBUG}. |
|
151
|
|
|
* |
|
152
|
|
|
* @throws IOException If writing fails or if the standard output is closed. |
|
153
|
|
|
*/ |
|
154
|
100 |
|
public function write($string, $flags = null) |
|
155
|
|
|
{ |
|
156
|
100 |
|
$this->output->write($string, $flags); |
|
157
|
100 |
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Writes a line of text to the standard output. |
|
161
|
|
|
* |
|
162
|
|
|
* The string is formatted before it is written to the output. |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $string The string to write. A newline is appended. |
|
165
|
|
|
* @param int $flags The flags. One of {@link VERBOSE}, |
|
|
|
|
|
|
166
|
|
|
* {@link VERY_VERBOSE} and {@link DEBUG}. |
|
167
|
|
|
* |
|
168
|
|
|
* @throws IOException If writing fails or if the standard output is closed. |
|
169
|
|
|
*/ |
|
170
|
|
|
public function writeLine($string, $flags = null) |
|
171
|
|
|
{ |
|
172
|
|
|
$this->output->writeLine($string, $flags); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Writes a string to the standard output without formatting. |
|
177
|
|
|
* |
|
178
|
|
|
* @param string $string The string to write. |
|
179
|
|
|
* @param int $flags The flags. One of {@link VERBOSE}, |
|
|
|
|
|
|
180
|
|
|
* {@link VERY_VERBOSE} and {@link DEBUG}. |
|
181
|
|
|
* |
|
182
|
|
|
* @throws IOException If writing fails or if the standard output is closed. |
|
183
|
|
|
*/ |
|
184
|
12 |
|
public function writeRaw($string, $flags = null) |
|
185
|
|
|
{ |
|
186
|
12 |
|
$this->output->writeRaw($string, $flags); |
|
187
|
12 |
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Writes a line of text to the standard output without formatting. |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $string The string to write. A newline is appended. |
|
193
|
|
|
* @param int $flags The flags. One of {@link VERBOSE}, |
|
|
|
|
|
|
194
|
|
|
* {@link VERY_VERBOSE} and {@link DEBUG}. |
|
195
|
|
|
* |
|
196
|
|
|
* @throws IOException If writing fails or if the standard output is closed. |
|
197
|
|
|
*/ |
|
198
|
|
|
public function writeLineRaw($string, $flags = null) |
|
199
|
|
|
{ |
|
200
|
|
|
$this->output->writeLineRaw($string, $flags); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Writes a string to the error output. |
|
205
|
|
|
* |
|
206
|
|
|
* The string is formatted before it is written to the output. |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $string The string to write. |
|
209
|
|
|
* @param int $flags The flags. One of {@link VERBOSE}, |
|
|
|
|
|
|
210
|
|
|
* {@link VERY_VERBOSE} and {@link DEBUG}. |
|
211
|
|
|
* |
|
212
|
|
|
* @throws IOException If writing fails or if the error output is closed. |
|
213
|
|
|
*/ |
|
214
|
9 |
|
public function error($string, $flags = null) |
|
215
|
|
|
{ |
|
216
|
9 |
|
$this->errorOutput->write($string, $flags); |
|
217
|
9 |
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Writes a line of text to the error output. |
|
221
|
|
|
* |
|
222
|
|
|
* The string is formatted before it is written to the output. |
|
223
|
|
|
* |
|
224
|
|
|
* @param string $string The string to write. A newline is appended. |
|
225
|
|
|
* @param int $flags The flags. One of {@link VERBOSE}, |
|
|
|
|
|
|
226
|
|
|
* {@link VERY_VERBOSE} and {@link DEBUG}. |
|
227
|
|
|
* |
|
228
|
|
|
* @throws IOException If writing fails or if the error output is closed. |
|
229
|
|
|
*/ |
|
230
|
6 |
|
public function errorLine($string, $flags = null) |
|
231
|
|
|
{ |
|
232
|
6 |
|
$this->errorOutput->writeLine($string, $flags); |
|
233
|
6 |
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Writes a string to the error output without formatting. |
|
237
|
|
|
* |
|
238
|
|
|
* @param string $string The string to write. |
|
239
|
|
|
* @param int $flags The flags. One of {@link VERBOSE}, |
|
|
|
|
|
|
240
|
|
|
* {@link VERY_VERBOSE} and {@link DEBUG}. |
|
241
|
|
|
* |
|
242
|
|
|
* @throws IOException If writing fails or if the error output is closed. |
|
243
|
|
|
*/ |
|
244
|
|
|
public function errorRaw($string, $flags = null) |
|
245
|
|
|
{ |
|
246
|
|
|
$this->errorOutput->writeRaw($string, $flags); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Writes a line of text to the error output without formatting. |
|
251
|
|
|
* |
|
252
|
|
|
* @param string $string The string to write. A newline is appended. |
|
253
|
|
|
* @param int $flags The flags. One of {@link VERBOSE}, |
|
|
|
|
|
|
254
|
|
|
* {@link VERY_VERBOSE} and {@link DEBUG}. |
|
255
|
|
|
* |
|
256
|
|
|
* @throws IOException If writing fails or if the error output is closed. |
|
257
|
|
|
*/ |
|
258
|
2 |
|
public function errorLineRaw($string, $flags = null) |
|
259
|
|
|
{ |
|
260
|
2 |
|
$this->errorOutput->writeLineRaw($string, $flags); |
|
261
|
2 |
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Flushes the outputs and forces all pending text to be written out. |
|
265
|
|
|
* |
|
266
|
|
|
* @throws IOException If flushing fails or if the outputs are closed. |
|
267
|
|
|
*/ |
|
268
|
|
|
public function flush() |
|
269
|
|
|
{ |
|
270
|
|
|
$this->output->flush(); |
|
271
|
|
|
$this->errorOutput->flush(); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Closes the input and the outputs. |
|
276
|
|
|
*/ |
|
277
|
|
|
public function close() |
|
278
|
|
|
{ |
|
279
|
|
|
$this->input->close(); |
|
280
|
|
|
$this->output->close(); |
|
281
|
|
|
$this->errorOutput->close(); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Enables or disables interaction with the user. |
|
286
|
|
|
* |
|
287
|
|
|
* @param bool $interactive Whether the I/O may interact with the user. If |
|
288
|
|
|
* set to `false`, all calls to {@link read()} and |
|
289
|
|
|
* {@link readLine()} will immediately return the |
|
290
|
|
|
* default value. |
|
291
|
|
|
*/ |
|
292
|
2 |
|
public function setInteractive($interactive) |
|
293
|
|
|
{ |
|
294
|
2 |
|
$this->input->setInteractive($interactive); |
|
295
|
2 |
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* Returns whether the user may be asked for input. |
|
299
|
|
|
* |
|
300
|
|
|
* @return bool Returns `true` if the user may be asked for input and |
|
301
|
|
|
* `false` otherwise. |
|
302
|
|
|
*/ |
|
303
|
2 |
|
public function isInteractive() |
|
304
|
|
|
{ |
|
305
|
2 |
|
return $this->input->isInteractive(); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Sets the verbosity of the output. |
|
310
|
|
|
* |
|
311
|
|
|
* @param int $verbosity One of the constants {@link NORMAL}, {@link VERBOSE}, |
|
312
|
|
|
* {@link VERY_VERBOSE} or {@link DEBUG}. Only output |
|
313
|
|
|
* with the given verbosity level or smaller will be |
|
314
|
|
|
* passed through. |
|
315
|
|
|
*/ |
|
316
|
145 |
|
public function setVerbosity($verbosity) |
|
317
|
|
|
{ |
|
318
|
145 |
|
$this->output->setVerbosity($verbosity); |
|
319
|
145 |
|
$this->errorOutput->setVerbosity($verbosity); |
|
320
|
145 |
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* Returns whether the verbosity is {@link VERBOSE} or greater. |
|
324
|
|
|
* |
|
325
|
|
|
* @return bool Returns `true` if the verbosity is {@link VERBOSE} or |
|
326
|
|
|
* greater and `false` otherwise. |
|
327
|
|
|
*/ |
|
328
|
12 |
|
public function isVerbose() |
|
329
|
|
|
{ |
|
330
|
12 |
|
return $this->output->isVerbose(); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* Returns whether the verbosity is {@link VERY_VERBOSE} or greater. |
|
335
|
|
|
* |
|
336
|
|
|
* @return bool Returns `true` if the verbosity is {@link VERY_VERBOSE} or |
|
337
|
|
|
* greater and `false` otherwise. |
|
338
|
|
|
*/ |
|
339
|
9 |
|
public function isVeryVerbose() |
|
340
|
|
|
{ |
|
341
|
9 |
|
return $this->output->isVeryVerbose(); |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
/** |
|
345
|
|
|
* Returns whether the verbosity is {@link DEBUG}. |
|
346
|
|
|
* |
|
347
|
|
|
* @return bool Returns `true` if the verbosity is {@link DEBUG} and `false` |
|
348
|
|
|
* otherwise. |
|
349
|
|
|
*/ |
|
350
|
8 |
|
public function isDebug() |
|
351
|
|
|
{ |
|
352
|
8 |
|
return $this->output->isDebug(); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* Returns the current verbosity level. |
|
357
|
|
|
* |
|
358
|
|
|
* @return int One of the verbosity constants. |
|
359
|
|
|
*/ |
|
360
|
|
|
public function getVerbosity() |
|
361
|
|
|
{ |
|
362
|
|
|
return $this->output->getVerbosity(); |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* Sets whether all output should be suppressed. |
|
367
|
|
|
* |
|
368
|
|
|
* @param bool $quiet Pass `true` to suppress all output and `false` |
|
369
|
|
|
* otherwise. |
|
370
|
|
|
*/ |
|
371
|
7 |
|
public function setQuiet($quiet) |
|
372
|
|
|
{ |
|
373
|
7 |
|
$this->output->setQuiet($quiet); |
|
374
|
7 |
|
$this->errorOutput->setQuiet($quiet); |
|
375
|
7 |
|
} |
|
376
|
|
|
|
|
377
|
|
|
/** |
|
378
|
|
|
* Returns whether all output is suppressed. |
|
379
|
|
|
* |
|
380
|
|
|
* @return bool Returns `true` if all output is suppressed and `false` |
|
381
|
|
|
* otherwise. |
|
382
|
|
|
*/ |
|
383
|
7 |
|
public function isQuiet() |
|
384
|
|
|
{ |
|
385
|
7 |
|
return $this->output->isQuiet(); |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
/** |
|
389
|
|
|
* Sets the dimensions of the terminal. |
|
390
|
|
|
* |
|
391
|
|
|
* @param Rectangle $dimensions The terminal dimensions. |
|
392
|
|
|
*/ |
|
393
|
|
|
public function setTerminalDimensions(Rectangle $dimensions) |
|
394
|
|
|
{ |
|
395
|
|
|
$this->terminalDimensions = $dimensions; |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
/** |
|
399
|
|
|
* Returns the dimensions of the terminal. |
|
400
|
|
|
* |
|
401
|
|
|
* @return Rectangle The terminal dimensions. |
|
402
|
|
|
*/ |
|
403
|
88 |
|
public function getTerminalDimensions() |
|
404
|
|
|
{ |
|
405
|
88 |
|
if (!$this->terminalDimensions) { |
|
406
|
88 |
|
$this->terminalDimensions = $this->getDefaultTerminalDimensions(); |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
88 |
|
return $this->terminalDimensions; |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
/** |
|
413
|
|
|
* Sets the output formatter. |
|
414
|
|
|
* |
|
415
|
|
|
* @param Formatter $formatter The output formatter. |
|
416
|
|
|
*/ |
|
417
|
|
|
public function setFormatter(Formatter $formatter) |
|
418
|
|
|
{ |
|
419
|
|
|
$this->output->setFormatter($formatter); |
|
420
|
|
|
$this->errorOutput->setFormatter($formatter); |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
/** |
|
424
|
|
|
* Returns the output formatter. |
|
425
|
|
|
* |
|
426
|
|
|
* @return Formatter The output formatter. |
|
427
|
|
|
*/ |
|
428
|
22 |
|
public function getFormatter() |
|
429
|
|
|
{ |
|
430
|
22 |
|
return $this->output->getFormatter(); |
|
431
|
|
|
} |
|
432
|
|
|
|
|
433
|
|
|
/** |
|
434
|
|
|
* {@inheritdoc} |
|
435
|
|
|
*/ |
|
436
|
23 |
|
public function format($string, Style $style = null) |
|
437
|
|
|
{ |
|
438
|
23 |
|
return $this->output->format($string, $style); |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
/** |
|
442
|
|
|
* {@inheritdoc} |
|
443
|
|
|
*/ |
|
444
|
80 |
|
public function removeFormat($string) |
|
445
|
|
|
{ |
|
446
|
80 |
|
return $this->output->removeFormat($string); |
|
447
|
|
|
} |
|
448
|
|
|
|
|
449
|
|
|
/** |
|
450
|
|
|
* Returns the default terminal dimensions. |
|
451
|
|
|
* |
|
452
|
|
|
* @return Rectangle The terminal dimensions. |
|
453
|
|
|
*/ |
|
454
|
73 |
|
protected function getDefaultTerminalDimensions() |
|
455
|
|
|
{ |
|
456
|
73 |
|
return new Rectangle(80, 20); |
|
457
|
|
|
} |
|
458
|
|
|
} |
|
459
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.