Convert Output::write's type to an options arg where verbosity can be passed in as well
This commit is contained in:
parent
a9555fb98f
commit
749fba54f9
@ -98,7 +98,7 @@ class NullOutput implements OutputInterface
|
|||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function writeln($messages, $type = self::OUTPUT_NORMAL)
|
public function writeln($messages, $options = self::OUTPUT_NORMAL)
|
||||||
{
|
{
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
@ -106,7 +106,7 @@ class NullOutput implements OutputInterface
|
|||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
|
public function write($messages, $newline = false, $options = self::OUTPUT_NORMAL)
|
||||||
{
|
{
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
|
@ -121,22 +121,28 @@ abstract class Output implements OutputInterface
|
|||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function writeln($messages, $type = self::OUTPUT_NORMAL)
|
public function writeln($messages, $options = self::OUTPUT_NORMAL)
|
||||||
{
|
{
|
||||||
$this->write($messages, true, $type);
|
$this->write($messages, true, $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
|
public function write($messages, $newline = false, $options = self::OUTPUT_NORMAL)
|
||||||
{
|
{
|
||||||
if (self::VERBOSITY_QUIET === $this->verbosity) {
|
$messages = (array) $messages;
|
||||||
|
|
||||||
|
$types = self::OUTPUT_NORMAL | self::OUTPUT_RAW | self::OUTPUT_PLAIN;
|
||||||
|
$type = $types & $options ?: self::OUTPUT_NORMAL;
|
||||||
|
|
||||||
|
$verbosities = self::VERBOSITY_QUIET | self::VERBOSITY_NORMAL | self::VERBOSITY_VERBOSE | self::VERBOSITY_VERY_VERBOSE | self::VERBOSITY_DEBUG;
|
||||||
|
$verbosity = $verbosities & $options ?: self::VERBOSITY_NORMAL;
|
||||||
|
|
||||||
|
if ($verbosity > $this->getVerbosity()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$messages = (array) $messages;
|
|
||||||
|
|
||||||
foreach ($messages as $message) {
|
foreach ($messages as $message) {
|
||||||
switch ($type) {
|
switch ($type) {
|
||||||
case OutputInterface::OUTPUT_NORMAL:
|
case OutputInterface::OUTPUT_NORMAL:
|
||||||
@ -147,8 +153,6 @@ abstract class Output implements OutputInterface
|
|||||||
case OutputInterface::OUTPUT_PLAIN:
|
case OutputInterface::OUTPUT_PLAIN:
|
||||||
$message = strip_tags($this->formatter->format($message));
|
$message = strip_tags($this->formatter->format($message));
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
throw new \InvalidArgumentException(sprintf('Unknown output type given (%s)', $type));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->doWrite($message, $newline);
|
$this->doWrite($message, $newline);
|
||||||
|
@ -22,40 +22,36 @@ use Symfony\Component\Console\Formatter\OutputFormatterInterface;
|
|||||||
*/
|
*/
|
||||||
interface OutputInterface
|
interface OutputInterface
|
||||||
{
|
{
|
||||||
const VERBOSITY_QUIET = 0;
|
const VERBOSITY_QUIET = 16;
|
||||||
const VERBOSITY_NORMAL = 1;
|
const VERBOSITY_NORMAL = 32;
|
||||||
const VERBOSITY_VERBOSE = 2;
|
const VERBOSITY_VERBOSE = 64;
|
||||||
const VERBOSITY_VERY_VERBOSE = 3;
|
const VERBOSITY_VERY_VERBOSE = 128;
|
||||||
const VERBOSITY_DEBUG = 4;
|
const VERBOSITY_DEBUG = 256;
|
||||||
|
|
||||||
const OUTPUT_NORMAL = 0;
|
const OUTPUT_NORMAL = 1;
|
||||||
const OUTPUT_RAW = 1;
|
const OUTPUT_RAW = 2;
|
||||||
const OUTPUT_PLAIN = 2;
|
const OUTPUT_PLAIN = 4;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes a message to the output.
|
* Writes a message to the output.
|
||||||
*
|
*
|
||||||
* @param string|array $messages The message as an array of lines or a single string
|
* @param string|array $messages The message as an array of lines or a single string
|
||||||
* @param bool $newline Whether to add a newline
|
* @param bool $newline Whether to add a newline
|
||||||
* @param int $type The type of output (one of the OUTPUT constants)
|
* @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
|
||||||
*
|
|
||||||
* @throws \InvalidArgumentException When unknown output type is given
|
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL);
|
public function write($messages, $newline = false, $options = 0);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes a message to the output and adds a newline at the end.
|
* Writes a message to the output and adds a newline at the end.
|
||||||
*
|
*
|
||||||
* @param string|array $messages The message as an array of lines of a single string
|
* @param string|array $messages The message as an array of lines of a single string
|
||||||
* @param int $type The type of output (one of the OUTPUT constants)
|
* @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
|
||||||
*
|
|
||||||
* @throws \InvalidArgumentException When unknown output type is given
|
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
public function writeln($messages, $type = self::OUTPUT_NORMAL);
|
public function writeln($messages, $options = 0);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the verbosity of the output.
|
* Sets the verbosity of the output.
|
||||||
|
@ -116,16 +116,6 @@ class OutputTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals("\033[33;41;5mfoo\033[39;49;25m\n", $output->output, '->writeln() decorates the output');
|
$this->assertEquals("\033[33;41;5mfoo\033[39;49;25m\n", $output->output, '->writeln() decorates the output');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException \InvalidArgumentException
|
|
||||||
* @expectedExceptionMessage Unknown output type given (24)
|
|
||||||
*/
|
|
||||||
public function testWriteWithInvalidOutputType()
|
|
||||||
{
|
|
||||||
$output = new TestOutput();
|
|
||||||
$output->writeln('<foo>foo</foo>', 24);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testWriteWithInvalidStyle()
|
public function testWriteWithInvalidStyle()
|
||||||
{
|
{
|
||||||
$output = new TestOutput();
|
$output = new TestOutput();
|
||||||
@ -138,6 +128,35 @@ class OutputTest extends \PHPUnit_Framework_TestCase
|
|||||||
$output->writeln('<bar>foo</bar>');
|
$output->writeln('<bar>foo</bar>');
|
||||||
$this->assertEquals("<bar>foo</bar>\n", $output->output, '->writeln() do nothing when a style does not exist');
|
$this->assertEquals("<bar>foo</bar>\n", $output->output, '->writeln() do nothing when a style does not exist');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider verbosityProvider
|
||||||
|
*/
|
||||||
|
public function testWriteWithVerbosityOption($verbosity, $expected, $msg)
|
||||||
|
{
|
||||||
|
$output = new TestOutput();
|
||||||
|
|
||||||
|
$output->setVerbosity($verbosity);
|
||||||
|
$output->clear();
|
||||||
|
$output->write('1', false);
|
||||||
|
$output->write('2', false, Output::VERBOSITY_QUIET);
|
||||||
|
$output->write('3', false, Output::VERBOSITY_NORMAL);
|
||||||
|
$output->write('4', false, Output::VERBOSITY_VERBOSE);
|
||||||
|
$output->write('5', false, Output::VERBOSITY_VERY_VERBOSE);
|
||||||
|
$output->write('6', false, Output::VERBOSITY_DEBUG);
|
||||||
|
$this->assertEquals($expected, $output->output, $msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function verbosityProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(Output::VERBOSITY_QUIET, '2', '->write() in QUIET mode only outputs when an explicit QUIET verbosity is passed'),
|
||||||
|
array(Output::VERBOSITY_NORMAL, '123', '->write() in NORMAL mode outputs anything below an explicit VERBOSE verbosity'),
|
||||||
|
array(Output::VERBOSITY_VERBOSE, '1234', '->write() in VERBOSE mode outputs anything below an explicit VERY_VERBOSE verbosity'),
|
||||||
|
array(Output::VERBOSITY_VERY_VERBOSE, '12345', '->write() in VERY_VERBOSE mode outputs anything below an explicit DEBUG verbosity'),
|
||||||
|
array(Output::VERBOSITY_DEBUG, '123456', '->write() in DEBUG mode outputs everything'),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class TestOutput extends Output
|
class TestOutput extends Output
|
||||||
|
Reference in New Issue
Block a user