|
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\Assert\Assert; |
|
15
|
|
|
use Webmozart\Console\Api\Formatter\Formatter; |
|
16
|
|
|
use Webmozart\Console\Api\Formatter\Style; |
|
17
|
|
|
use Webmozart\Console\Formatter\AnsiFormatter; |
|
18
|
|
|
use Webmozart\Console\Formatter\NullFormatter; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The console output. |
|
22
|
|
|
* |
|
23
|
|
|
* This class wraps an output stream and adds convenience functionality for |
|
24
|
|
|
* writing that stream. |
|
25
|
|
|
* |
|
26
|
|
|
* @since 1.0 |
|
27
|
|
|
* |
|
28
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class Output implements Formatter |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var OutputStream |
|
34
|
|
|
*/ |
|
35
|
|
|
private $stream; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var bool |
|
39
|
|
|
*/ |
|
40
|
|
|
private $quiet = false; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var int |
|
44
|
|
|
*/ |
|
45
|
|
|
private $verbosity = 0; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var Formatter |
|
49
|
|
|
*/ |
|
50
|
|
|
private $formatter; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var bool |
|
54
|
|
|
*/ |
|
55
|
|
|
private $formatOutput; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Creates an output for the given output stream. |
|
59
|
|
|
* |
|
60
|
|
|
* @param OutputStream $stream The output stream. |
|
61
|
|
|
* @param Formatter|null $formatter The formatter for formatting text |
|
62
|
|
|
* written to the output stream. |
|
63
|
|
|
*/ |
|
64
|
261 |
|
public function __construct(OutputStream $stream, Formatter $formatter = null) |
|
65
|
|
|
{ |
|
66
|
261 |
|
$this->stream = $stream; |
|
67
|
|
|
|
|
68
|
261 |
|
$this->setFormatter($formatter ?: new NullFormatter()); |
|
69
|
261 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Writes a string to the output stream. |
|
73
|
|
|
* |
|
74
|
|
|
* The string is formatted before it is written to the output stream. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $string The string to write. |
|
77
|
|
|
* @param int $flags The flags. If one of of {@link IO::VERBOSE}, |
|
|
|
|
|
|
78
|
|
|
* {@link IO::VERY_VERBOSE} and {@link IO::DEBUG} is |
|
79
|
|
|
* passed, the output is only written if the |
|
80
|
|
|
* verbosity level is the given level or higher. |
|
81
|
|
|
* |
|
82
|
|
|
* @throws IOException If writing fails or if the output stream is closed. |
|
83
|
|
|
*/ |
|
84
|
109 |
|
public function write($string, $flags = null) |
|
85
|
|
|
{ |
|
86
|
109 |
|
if ($this->mayWrite($flags)) { |
|
87
|
109 |
|
$formatted = $this->formatOutput ? $this->format($string) : $this->removeFormat($string); |
|
88
|
109 |
|
$this->stream->write($formatted); |
|
89
|
|
|
} |
|
90
|
109 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Writes a line of text to the output stream. |
|
94
|
|
|
* |
|
95
|
|
|
* The string is formatted before it is written to the output stream. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $string The string to write. A newline is appended. |
|
98
|
|
|
* @param int $flags The flags. If one of of {@link IO::VERBOSE}, |
|
|
|
|
|
|
99
|
|
|
* {@link IO::VERY_VERBOSE} and {@link IO::DEBUG} is |
|
100
|
|
|
* passed, the output is only written if the |
|
101
|
|
|
* verbosity level is the given level or higher. |
|
102
|
|
|
* |
|
103
|
|
|
* @throws IOException If writing fails or if the output stream is closed. |
|
104
|
|
|
*/ |
|
105
|
15 |
|
public function writeLine($string, $flags = null) |
|
106
|
|
|
{ |
|
107
|
15 |
|
if ($this->mayWrite($flags)) { |
|
108
|
15 |
|
$string = rtrim($string, PHP_EOL); |
|
109
|
15 |
|
$formatted = $this->formatOutput ? $this->format($string) : $this->removeFormat($string); |
|
110
|
15 |
|
$this->stream->write($formatted.PHP_EOL); |
|
111
|
|
|
} |
|
112
|
15 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Writes a string to the output stream without formatting. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $string The string to write. |
|
118
|
|
|
* @param int $flags The flags. If one of of {@link IO::VERBOSE}, |
|
|
|
|
|
|
119
|
|
|
* {@link IO::VERY_VERBOSE} and {@link IO::DEBUG} is |
|
120
|
|
|
* passed, the output is only written if the |
|
121
|
|
|
* verbosity level is the given level or higher. |
|
122
|
|
|
* |
|
123
|
|
|
* @throws IOException If writing fails or if the output stream is closed. |
|
124
|
|
|
*/ |
|
125
|
17 |
|
public function writeRaw($string, $flags = null) |
|
126
|
|
|
{ |
|
127
|
17 |
|
if ($this->mayWrite($flags)) { |
|
128
|
17 |
|
$this->stream->write($string); |
|
129
|
|
|
} |
|
130
|
17 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Writes a line of text to the output stream without formatting. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $string The string to write. A newline is appended. |
|
136
|
|
|
* @param int $flags The flags. If one of of {@link IO::VERBOSE}, |
|
|
|
|
|
|
137
|
|
|
* {@link IO::VERY_VERBOSE} and {@link IO::DEBUG} is |
|
138
|
|
|
* passed, the output is only written if the |
|
139
|
|
|
* verbosity level is the given level or higher. |
|
140
|
|
|
* |
|
141
|
|
|
* @throws IOException If writing fails or if the standard output is closed. |
|
142
|
|
|
*/ |
|
143
|
9 |
|
public function writeLineRaw($string, $flags = null) |
|
144
|
|
|
{ |
|
145
|
9 |
|
if ($this->mayWrite($flags)) { |
|
146
|
9 |
|
$this->stream->write(rtrim($string, PHP_EOL).PHP_EOL); |
|
147
|
|
|
} |
|
148
|
9 |
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Forces all pending text to be written out. |
|
152
|
|
|
* |
|
153
|
|
|
* @throws IOException If flushing fails or if the output stream is closed. |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public function flush() |
|
156
|
|
|
{ |
|
157
|
1 |
|
$this->stream->flush(); |
|
158
|
1 |
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Closes the output. |
|
162
|
|
|
*/ |
|
163
|
1 |
|
public function close() |
|
164
|
|
|
{ |
|
165
|
1 |
|
$this->stream->close(); |
|
166
|
1 |
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Returns whether the output is closed. |
|
170
|
|
|
* |
|
171
|
|
|
* @return bool Returns `true` if the output is closed and `false` |
|
172
|
|
|
* otherwise. |
|
173
|
|
|
*/ |
|
174
|
|
|
public function isClosed() |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->stream->isClosed(); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Sets the underlying stream. |
|
181
|
|
|
* |
|
182
|
|
|
* @param OutputStream $stream The output stream. |
|
183
|
|
|
*/ |
|
184
|
6 |
|
public function setStream(OutputStream $stream) |
|
185
|
|
|
{ |
|
186
|
6 |
|
$this->stream = $stream; |
|
187
|
6 |
|
$this->formatOutput = $stream->supportsAnsi() || !($this->formatter instanceof AnsiFormatter); |
|
188
|
6 |
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Returns the underlying stream. |
|
192
|
|
|
* |
|
193
|
|
|
* @return OutputStream The output stream. |
|
194
|
|
|
*/ |
|
195
|
100 |
|
public function getStream() |
|
196
|
|
|
{ |
|
197
|
100 |
|
return $this->stream; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Sets the output formatter. |
|
202
|
|
|
* |
|
203
|
|
|
* @param Formatter $formatter The output formatter. |
|
204
|
|
|
*/ |
|
205
|
261 |
|
public function setFormatter(Formatter $formatter) |
|
206
|
|
|
{ |
|
207
|
261 |
|
$this->formatter = $formatter; |
|
208
|
261 |
|
$this->formatOutput = $this->stream->supportsAnsi() || !($formatter instanceof AnsiFormatter); |
|
209
|
261 |
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Returns the output formatter. |
|
213
|
|
|
* |
|
214
|
|
|
* @return Formatter The output formatter. |
|
215
|
|
|
*/ |
|
216
|
22 |
|
public function getFormatter() |
|
217
|
|
|
{ |
|
218
|
22 |
|
return $this->formatter; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Sets the verbosity level of the output. |
|
223
|
|
|
* |
|
224
|
|
|
* @param int $verbosity One of the constants {@link NORMAL}, {@link VERBOSE}, |
|
225
|
|
|
* {@link VERY_VERBOSE} or {@link DEBUG}. Only output |
|
226
|
|
|
* with the given verbosity level or smaller will be |
|
227
|
|
|
* written out. |
|
228
|
|
|
*/ |
|
229
|
161 |
|
public function setVerbosity($verbosity) |
|
230
|
|
|
{ |
|
231
|
161 |
|
Assert::oneOf($verbosity, array(IO::NORMAL, IO::VERBOSE, IO::VERY_VERBOSE, IO::DEBUG), 'The verbosity must be one of IO::NORMAL, IO::VERBOSE, IO::VERY_VERBOSE and IO::DEBUG.'); |
|
232
|
|
|
|
|
233
|
161 |
|
$this->verbosity = (int) $verbosity; |
|
234
|
161 |
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Returns the current verbosity level. |
|
238
|
|
|
* |
|
239
|
|
|
* @return int One of the verbosity constants. |
|
240
|
|
|
*/ |
|
241
|
|
|
public function getVerbosity() |
|
242
|
|
|
{ |
|
243
|
|
|
return $this->verbosity; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Returns whether the verbosity level is {@link IO::VERBOSE} or greater. |
|
248
|
|
|
* |
|
249
|
|
|
* @return bool Returns `true` if the verbosity level is {@link IO::VERBOSE} |
|
250
|
|
|
* or greater and `false` otherwise. |
|
251
|
|
|
*/ |
|
252
|
12 |
|
public function isVerbose() |
|
253
|
|
|
{ |
|
254
|
12 |
|
return $this->verbosity >= IO::VERBOSE; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Returns whether the verbosity level is {@link IO::VERY_VERBOSE} or greater. |
|
259
|
|
|
* |
|
260
|
|
|
* @return bool Returns `true` if the verbosity level is |
|
261
|
|
|
* {@link IO::VERY_VERBOSE} or greater and `false` otherwise. |
|
262
|
|
|
*/ |
|
263
|
9 |
|
public function isVeryVerbose() |
|
264
|
|
|
{ |
|
265
|
9 |
|
return $this->verbosity >= IO::VERY_VERBOSE; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Returns whether the verbosity level is {@link IO::DEBUG}. |
|
270
|
|
|
* |
|
271
|
|
|
* @return bool Returns `true` if the verbosity level is {@link IO::DEBUG} |
|
272
|
|
|
* and `false` otherwise. |
|
273
|
|
|
*/ |
|
274
|
8 |
|
public function isDebug() |
|
275
|
|
|
{ |
|
276
|
8 |
|
return IO::DEBUG === $this->verbosity; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Sets whether output should be suppressed completely. |
|
281
|
|
|
* |
|
282
|
|
|
* @param bool $quiet Pass `true` to suppress all output and `false` |
|
283
|
|
|
* otherwise. |
|
284
|
|
|
*/ |
|
285
|
7 |
|
public function setQuiet($quiet) |
|
286
|
|
|
{ |
|
287
|
7 |
|
$this->quiet = (bool) $quiet; |
|
288
|
7 |
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Returns whether output is suppressed completely. |
|
292
|
|
|
* |
|
293
|
|
|
* @return bool Returns `true` if all output is suppressed and `false` |
|
294
|
|
|
* otherwise. |
|
295
|
|
|
*/ |
|
296
|
7 |
|
public function isQuiet() |
|
297
|
|
|
{ |
|
298
|
7 |
|
return $this->quiet; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* {@inheritdoc} |
|
303
|
|
|
*/ |
|
304
|
122 |
|
public function format($string, Style $style = null) |
|
305
|
|
|
{ |
|
306
|
122 |
|
return $this->formatter->format($string, $style); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* {@inheritdoc} |
|
311
|
|
|
*/ |
|
312
|
82 |
|
public function removeFormat($string) |
|
313
|
|
|
{ |
|
314
|
82 |
|
return $this->formatter->removeFormat($string); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Returns whether an output may be written for the given flags. |
|
319
|
|
|
* |
|
320
|
|
|
* @param int $flags The flags. |
|
321
|
|
|
* |
|
322
|
|
|
* @return bool Returns `true` if the output may be written and `false` |
|
323
|
|
|
* otherwise. |
|
324
|
|
|
*/ |
|
325
|
148 |
|
protected function mayWrite($flags) |
|
326
|
|
|
{ |
|
327
|
148 |
|
if ($this->quiet) { |
|
328
|
|
|
return false; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
148 |
|
if ($flags & IO::VERBOSE) { |
|
332
|
16 |
|
return $this->verbosity >= IO::VERBOSE; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
148 |
|
if ($flags & IO::VERY_VERBOSE) { |
|
336
|
16 |
|
return $this->verbosity >= IO::VERY_VERBOSE; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
148 |
|
if ($flags & IO::DEBUG) { |
|
340
|
16 |
|
return $this->verbosity >= IO::DEBUG; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
148 |
|
return true; |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
|
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.