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\Adapter; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterInterface; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
use Webmozart\Console\Api\IO\IO; |
17
|
|
|
use Webmozart\Console\Formatter\AnsiFormatter; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Adapts an {@link IO} instance to Symfony's {@link OutputInterface} API. |
21
|
|
|
* |
22
|
|
|
* @since 1.0 |
23
|
|
|
* |
24
|
|
|
* @author Bernhard Schussek <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class IOOutput implements OutputInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var IO |
30
|
|
|
*/ |
31
|
|
|
private $io; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
private $decorated; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Creates a new composite output. |
40
|
|
|
* |
41
|
|
|
* @param IO $io The I/O. |
42
|
|
|
*/ |
43
|
32 |
|
public function __construct(IO $io) |
44
|
|
|
{ |
45
|
32 |
|
$this->io = $io; |
46
|
32 |
|
$this->decorated = $this->io->getFormatter() instanceof AnsiFormatter; |
47
|
32 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns the standard output. |
51
|
|
|
* |
52
|
|
|
* @return IO The standard output. |
53
|
|
|
*/ |
54
|
|
|
public function getIO() |
55
|
|
|
{ |
56
|
|
|
return $this->io; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
18 |
|
public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL) |
63
|
|
|
{ |
64
|
18 |
|
foreach ((array) $messages as $message) { |
65
|
18 |
|
if ($newline) { |
66
|
2 |
|
$this->doWriteLine($message, $type); |
67
|
|
|
} else { |
68
|
18 |
|
$this->doWrite($message, $type); |
69
|
|
|
} |
70
|
|
|
} |
71
|
18 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
2 |
|
public function writeln($messages, $type = self::OUTPUT_NORMAL) |
77
|
|
|
{ |
78
|
2 |
|
foreach ((array) $messages as $message) { |
79
|
2 |
|
$this->doWriteLine($message, $type); |
80
|
|
|
} |
81
|
2 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
5 |
|
public function setVerbosity($level) |
87
|
|
|
{ |
88
|
|
|
switch ($level) { |
89
|
5 |
|
case self::VERBOSITY_QUIET: |
90
|
1 |
|
$this->io->setQuiet(true); |
91
|
1 |
|
break; |
92
|
4 |
|
case self::VERBOSITY_NORMAL: |
93
|
1 |
|
$this->io->setQuiet(false); |
94
|
1 |
|
$this->io->setVerbosity(IO::NORMAL); |
95
|
1 |
|
break; |
96
|
3 |
|
case self::VERBOSITY_VERBOSE: |
97
|
1 |
|
$this->io->setQuiet(false); |
98
|
1 |
|
$this->io->setVerbosity(IO::VERBOSE); |
99
|
1 |
|
break; |
100
|
2 |
|
case self::VERBOSITY_VERY_VERBOSE: |
101
|
1 |
|
$this->io->setQuiet(false); |
102
|
1 |
|
$this->io->setVerbosity(IO::VERY_VERBOSE); |
103
|
1 |
|
break; |
104
|
1 |
|
case self::VERBOSITY_DEBUG: |
105
|
1 |
|
$this->io->setQuiet(false); |
106
|
1 |
|
$this->io->setVerbosity(IO::DEBUG); |
107
|
1 |
|
break; |
108
|
|
|
} |
109
|
5 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
5 |
|
public function getVerbosity() |
115
|
|
|
{ |
116
|
5 |
|
if ($this->isQuiet()) { |
117
|
1 |
|
return self::VERBOSITY_QUIET; |
118
|
|
|
} |
119
|
|
|
|
120
|
4 |
|
if ($this->isDebug()) { |
121
|
1 |
|
return self::VERBOSITY_DEBUG; |
122
|
|
|
} |
123
|
|
|
|
124
|
3 |
|
if ($this->isVeryVerbose()) { |
125
|
1 |
|
return self::VERBOSITY_VERY_VERBOSE; |
126
|
|
|
} |
127
|
|
|
|
128
|
2 |
|
if ($this->isVerbose()) { |
129
|
1 |
|
return self::VERBOSITY_VERBOSE; |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
return self::VERBOSITY_NORMAL; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
5 |
|
public function isQuiet() |
139
|
|
|
{ |
140
|
5 |
|
return $this->io->isQuiet(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
2 |
|
public function isVerbose() |
147
|
|
|
{ |
148
|
2 |
|
return $this->io->isVerbose(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
3 |
|
public function isVeryVerbose() |
155
|
|
|
{ |
156
|
3 |
|
return $this->io->isVeryVerbose(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
4 |
|
public function isDebug() |
163
|
|
|
{ |
164
|
4 |
|
return $this->io->isDebug(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritdoc} |
169
|
|
|
*/ |
170
|
1 |
|
public function setDecorated($decorated) |
171
|
|
|
{ |
172
|
1 |
|
$this->decorated = $decorated; |
173
|
1 |
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* {@inheritdoc} |
177
|
|
|
*/ |
178
|
1 |
|
public function isDecorated() |
179
|
|
|
{ |
180
|
1 |
|
return $this->decorated; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
|
|
public function setFormatter(OutputFormatterInterface $formatter) |
187
|
|
|
{ |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritdoc} |
192
|
|
|
*/ |
193
|
1 |
|
public function getFormatter() |
194
|
|
|
{ |
195
|
1 |
|
return new FormatterAdapter($this->io); |
196
|
|
|
} |
197
|
|
|
|
198
|
4 |
View Code Duplication |
private function doWriteLine($message, $type) |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
switch ($type) { |
201
|
4 |
|
case self::OUTPUT_PLAIN: |
202
|
|
|
$this->io->writeLine($this->io->removeFormat($message)); |
203
|
|
|
break; |
204
|
4 |
|
case self::OUTPUT_RAW: |
205
|
|
|
$this->io->writeLineRaw($message); |
206
|
|
|
break; |
207
|
|
|
default: |
208
|
4 |
|
$this->io->writeLine($message); |
209
|
4 |
|
break; |
210
|
|
|
} |
211
|
4 |
|
} |
212
|
|
|
|
213
|
16 |
View Code Duplication |
private function doWrite($message, $type) |
|
|
|
|
214
|
|
|
{ |
215
|
|
|
switch ($type) { |
216
|
16 |
|
case self::OUTPUT_PLAIN: |
217
|
1 |
|
$this->io->write($this->io->removeFormat($message)); |
218
|
1 |
|
break; |
219
|
15 |
|
case self::OUTPUT_RAW: |
220
|
13 |
|
$this->io->writeRaw($message); |
221
|
13 |
|
break; |
222
|
|
|
default: |
223
|
2 |
|
$this->io->write($message); |
224
|
2 |
|
break; |
225
|
|
|
} |
226
|
16 |
|
} |
227
|
|
|
} |
228
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.