8000 Convert Output::write's type to an options arg where verbosity can be… · Seldaek/symfony@749fba5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 749fba5

Browse files
committed
Convert Output::write's type to an options arg where verbosity can be passed in as well
1 parent a9555fb commit 749fba5

File tree

4 files changed

+55
-36
lines changed

4 files changed

+55
-36
lines changed

src/Symfony/Component/Console/Output/NullOutput.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ public function isDebug()
9898
/**
9999
* {@inheritdoc}
100100
*/
101-
public function writeln($messages, $type = self::OUTPUT_NORMAL)
101+
public function writeln($messages, $options = self::OUTPUT_NORMAL)
102102
{
103103
// do nothing
104104
}
105105

106106
/**
107107
* {@inheritdoc}
108108
*/
109-
public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
109+
public function write($messages, $newline = false, $options = self::OUTPUT_NORMAL)
110110
{
111111
// do nothing
112112
}

src/Symfony/Component/Console/Output/Output.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,28 @@ public function isDebug()
121121
/**
122122
* {@inheritdoc}
123123
*/
124-
public function writeln($messages, $type = self::OUTPUT_NORMAL)
124+
public function writeln($messages, $options = self::OUTPUT_NORMAL)
125125
{
126-
$this->write($messages, true, $type);
126+
$this->write($messages, true, $options);
127127
}
128128

129129
/**
130130
* {@inheritdoc}
131131
*/
132-
public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
132+
public function write($messages, $newline = false, $options = self::OUTPUT_NORMAL)
133133
{
134-
if (self::VERBOSITY_QUIET === $this->verbosity) {
134+
$messages = (array) $messages;
135+
136+
$types = self::OUTPUT_NORMAL | self::OUTPUT_RAW | self::OUTPUT_PLAIN;
137+
$type = $types & $options ?: self::OUTPUT_NORMAL;
138+
139+
$verbosities = self::VERBOSITY_QUIET | self::VERBOSITY_NORMAL | self::VERBOSITY_VERBOSE | self::VERBOSITY_VERY_VERBOSE | self::VERBOSITY_DEBUG;
140+
$verbosity = $verbosities & $options ?: self::VERBOSITY_NORMAL;
141+
142+
if ($verbosity > $this->getVerbosity()) {
135143
return;
136144
}
137145

138-
$messages = (array) $messages;
139-
140146
foreach ($messages as $message) {
141147
switch ($type) {
142148
case OutputInterface::OUTPUT_NORMAL:
@@ -147,8 +153,6 @@ public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
147153
case OutputInterface::OUTPUT_PLAIN:
148154
$message = strip_tags($this->formatter->format($message));
149155
break;
150-
default:
151-
throw new \InvalidArgumentException(sprintf('Unknown output type given (%s)', $type));
152156
}
153157

154158
$this->doWrite($message, $newline);

src/Symfony/Component/Console/Output/OutputInterface.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,36 @@
2222
*/
2323
interface OutputInterface
2424
{
25-
const VERBOSITY_QUIET = 0;
26-
const VERBOSITY_NORMAL = 1;
27-
const VERBOSITY_VERBOSE = 2;
28-
const VERBOSITY_VERY_VERBOSE = 3;
29-
const VERBOSITY_DEBUG = 4;
25+
const VERBOSITY_QUIET = 16;
26+
const VERBOSITY_NORMAL = 32;
27+
const VERBOSITY_VERBOSE = 64;
28+
const VERBOSITY_VERY_VERBOSE = 128;
29+
const VERBOSITY_DEBUG = 256;
3030

31-
const OUTPUT_NORMAL = 0;
32-
const OUTPUT_RAW = 1;
33-
const OUTPUT_PLAIN = 2;
31+
const OUTPUT_NORMAL = 1;
32+
const OUTPUT_RAW = 2;
33+
const OUTPUT_PLAIN = 4;
3434

3535
/**
3636
* Writes a message to the output.
3737
*
3838
* @param string|array $messages The message as an array of lines or a single string
3939
* @param bool $newline Whether to add a newline
40-
* @param int $type The type of output (one of the OUTPUT constants)
41-
*
42-
* @throws \InvalidArgumentException When unknown output type is given
40+
* @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
4341
*
4442
* @api
4543
*/
46-
public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL);
44+
public function write($messages, $newline = false, $options = 0);
4745

4846
/**
4947
* Writes a message to the output and adds a newline at the end.
5048
*
5149
* @param string|array $messages The message as an array of lines of a single string
52-
* @param int $type The type of output (one of the OUTPUT constants)
53-
*
54-
* @throws \InvalidArgumentException When unknown output type is given
50+
* @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
5551
*
5652
* @api
5753
*/
58-
public function writeln($messages, $type = self::OUTPUT_NORMAL);
54+
public function writeln($messages, $options = 0);
5955

6056
/**
6157
* Sets the verbosity of the output.

src/Symfony/Component/Console/Tests/Output/OutputTest.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,6 @@ public function testWriteDecoratedMessage()
116116
$this->assertEquals("\033[33;41;5mfoo\033[39;49;25m\n", $output->output, '->writeln() decorates the output');
117117
}
118118

119-
/**
120-
* @expectedException \InvalidArgumentException
121-
* @expectedExceptionMessage Unknown output type given (24)
122-
*/
123-
public function testWriteWithInvalidOutputType()
124-
{
125-
$output = new TestOutput();
126-
$output->writeln('<foo>foo</foo>', 24);
127-
}
128-
129119
public function testWriteWithInvalidStyle()
130120
{
131121
$output = new TestOutput();
@@ -138,6 +128,35 @@ public function testWriteWithInvalidStyle()
138128
$output->writeln('<bar>foo</bar>');
139129
$this->assertEquals("<bar>foo</bar>\n", $output->output, '->writeln() do nothing when a style does not exist');
140130
}
131+
132+
/**
133+
* @dataProvider verbosityProvider
134+
*/
135+
public function testWriteWithVerbosityOption($verbosity, $expected, $msg)
136+
{
137+
$output = new TestOutput();
138+
139+
$output->setVerbosity($verbosity);
140+
$output->clear();
141+
$output->write('1', false);
142+
$output->write('2', false, Output::VERBOSITY_QUIET);
143+
$output->write('3', false, Output::VERBOSITY_NORMAL);
144+
$output->write('4', false, Output::VERBOSITY_VERBOSE);
145+
$output->write('5', false, Output::VERBOSITY_VERY_VERBOSE);
146+
$output->write('6', false, Output::VERBOSITY_DEBUG);
147+
$this->assertEquals($expected, $output->output, $msg);
148+
}
149+
150+
public function verbosityProvider()
151+
{
152+
return array(
153+
array(Output::VERBOSITY_QUIET, '2', '->write() in QUIET mode only outputs when an explicit QUIET verbosity is passed'),
154+
array(Output::VERBOSITY_NORMAL, '123', '->write() in NORMAL mode outputs anything below an explicit VERBOSE verbosity'),
155+
array(Output::VERBOSITY_VERBOSE, '1234', '->write() in VERBOSE mode outputs anything below an explicit VERY_VERBOSE verbosity'),
156+
array(Output::VERBOSITY_VERY_VERBOSE, '12345', '->write() in VERY_VERBOSE mode outputs anything below an explicit DEBUG verbosity'),
157+
array(Output::VERBOSITY_DEBUG, '123456', '->write() in DEBUG mode outputs everything'),
158+
);
159+
}
141160
}
142161

143162
class TestOutput extends Output

0 commit comments

Comments
 (0)
0