Skip to content

[Console] fix Output classes #8037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 15, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/Symfony/Component/Console/Output/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ class ConsoleOutput extends StreamOutput implements ConsoleOutputInterface
/**
* Constructor.
*
* @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL,
* self::VERBOSITY_VERBOSE)
* @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing)
* @param OutputFormatterInterface $formatter Output formatter instance
* @param integer $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
* @param Boolean|null $decorated Whether to decorate messages (null for auto-guessing)
* @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
*
* @api
*/
Expand All @@ -55,32 +54,44 @@ public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = nu
$this->stderr = new StreamOutput(fopen('php://stderr', 'w'), $verbosity, $decorated, $formatter);
}

/**
* {@inheritdoc}
*/
public function setDecorated($decorated)
{
parent::setDecorated($decorated);
$this->stderr->setDecorated($decorated);
}

/**
* {@inheritdoc}
*/
public function setFormatter(OutputFormatterInterface $formatter)
{
parent::setFormatter($formatter);
$this->stderr->setFormatter($formatter);
}

/**
* {@inheritdoc}
*/
public function setVerbosity($level)
{
parent::setVerbosity($level);
$this->stderr->setVerbosity($level);
}

/**
* @return OutputInterface
* {@inheritdoc}
*/
public function getErrorOutput()
{
return $this->stderr;
}

/**
* {@inheritdoc}
*/
public function setErrorOutput(OutputInterface $error)
{
$this->stderr = $error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@
interface ConsoleOutputInterface extends OutputInterface
{
/**
* Gets the OutputInterface for errors.
*
* @return OutputInterface
*/
public function getErrorOutput();

/**
* Sets the OutputInterface used for errors.
*
* @param OutputInterface $error
*/
public function setErrorOutput(OutputInterface $error);
}
71 changes: 65 additions & 6 deletions src/Symfony/Component/Console/Output/NullOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,83 @@

namespace Symfony\Component\Console\Output;

use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;

/**
* NullOutput suppresses all output.
*
* $output = new NullOutput();
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Tobias Schultze <http://tobion.de>
*
* @api
*/
class NullOutput extends Output
class NullOutput implements OutputInterface
{
/**
* Writes a message to the output.
*
* @param string $message A message to write to the output
* @param Boolean $newline Whether to add a newline or not
* {@inheritdoc}
*/
public function setFormatter(OutputFormatterInterface $formatter)
{
// do nothing
}

/**
* {@inheritdoc}
*/
public function getFormatter()
{
// to comply with the interface we must return a OutputFormatterInterface
return new OutputFormatter();
}

/**
* {@inheritdoc}
*/
public function setDecorated($decorated)
{
// do nothing
}

/**
* {@inheritdoc}
*/
public function isDecorated()
{
return false;
}

/**
* {@inheritdoc}
*/
public function setVerbosity($level)
{
// do nothing
}

/**
* {@inheritdoc}
*/
public function getVerbosity()
{
return self::VERBOSITY_QUIET;
}

/**
* {@inheritdoc}
*/
public function writeln($messages, $type = self::OUTPUT_NORMAL)
{
// do nothing
}

/**
* {@inheritdoc}
*/
protected function doWrite($message, $newline)
public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
{
// do nothing
}
}
77 changes: 21 additions & 56 deletions src/Symfony/Component/Console/Output/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
/**
* Base class for output classes.
*
* There are three levels of verbosity:
* There are five levels of verbosity:
*
* * normal: no option passed (normal output - information)
* * verbose: -v (more output - debug)
* * normal: no option passed (normal output)
* * verbose: -v (more output)
* * very verbose: -vv (highly extended output)
* * debug: -vvv (all debug output)
* * quiet: -q (no output)
*
* @author Fabien Potencier <fabien@symfony.com>
Expand All @@ -35,116 +37,79 @@ abstract class Output implements OutputInterface
/**
* Constructor.
*
* @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL, self::VERBOSITY_VERBOSE)
* @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing)
* @param OutputFormatterInterface $formatter Output formatter instance
* @param integer $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
* @param Boolean $decorated Whether to decorate messages
* @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
*
* @api
*/
public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = false, OutputFormatterInterface $formatter = null)
{
$this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity;
$this->formatter = null === $formatter ? new OutputFormatter() : $formatter;
$this->formatter->setDecorated((Boolean) $decorated);
$this->formatter->setDecorated($decorated);
}

/**
* Sets output formatter.
*
* @param OutputFormatterInterface $formatter
*
* @api
* {@inheritdoc}
*/
public function setFormatter(OutputFormatterInterface $formatter)
{
$this->formatter = $formatter;
}

/**
* Returns current output formatter instance.
*
* @return OutputFormatterInterface
*
* @api
* {@inheritdoc}
*/
public function getFormatter()
{
return $this->formatter;
}

/**
* Sets the decorated flag.
*
* @param Boolean $decorated Whether to decorate the messages or not
*
* @api
* {@inheritdoc}
*/
public function setDecorated($decorated)
{
$this->formatter->setDecorated((Boolean) $decorated);
$this->formatter->setDecorated($decorated);
}

/**
* Gets the decorated flag.
*
* @return Boolean true if the output will decorate messages, false otherwise
*
* @api
* {@inheritdoc}
*/
public function isDecorated()
{
return $this->formatter->isDecorated();
}

/**
* Sets the verbosity of the output.
*
* @param integer $level The level of verbosity
*
* @api
* {@inheritdoc}
*/
public function setVerbosity($level)
{
$this->verbosity = (int) $level;
}

/**
* Gets the current verbosity of the output.
*
* @return integer The current level of verbosity
*
* @api
* {@inheritdoc}
*/
public function getVerbosity()
{
return $this->verbosity;
}

/**
* Writes a message to the output and adds a newline at the end.
*
* @param string|array $messages The message as an array of lines or a single string
* @param integer $type The type of output
*
* @api
* {@inheritdoc}
*/
public function writeln($messages, $type = 0)
public function writeln($messages, $type = self::OUTPUT_NORMAL)
{
$this->write($messages, true, $type);
}

/**
* Writes a message to the output.
*
* @param string|array $messages The message as an array of lines or a single string
* @param Boolean $newline Whether to add a newline or not
* @param integer $type The type of output
*
* @throws \InvalidArgumentException When unknown output type is given
*
* @api
* {@inheritdoc}
*/
public function write($messages, $newline = false, $type = 0)
public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
{
if (self::VERBOSITY_QUIET === $this->verbosity) {
return;
Expand Down
18 changes: 10 additions & 8 deletions src/Symfony/Component/Console/Output/OutputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,31 @@ interface OutputInterface
* Writes a message to the output.
*
* @param string|array $messages The message as an array of lines or a single string
* @param Boolean $newline Whether to add a newline or not
* @param integer $type The type of output (0: normal, 1: raw, 2: plain)
* @param Boolean $newline Whether to add a newline
* @param integer $type The type of output (one of the OUTPUT constants)
*
* @throws \InvalidArgumentException When unknown output type is given
*
* @api
*/
public function write($messages, $newline = false, $type = 0);
public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL);

/**
* 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 integer $type The type of output (0: normal, 1: raw, 2: plain)
* @param integer $type The type of output (one of the OUTPUT constants)
*
* @throws \InvalidArgumentException When unknown output type is given
*
* @api
*/
public function writeln($messages, $type = 0);
public function writeln($messages, $type = self::OUTPUT_NORMAL);

/**
* Sets the verbosity of the output.
*
* @param integer $level The level of verbosity
* @param integer $level The level of verbosity (one of the VERBOSITY constants)
*
* @api
*/
Expand All @@ -67,7 +69,7 @@ public function setVerbosity($level);
/**
* Gets the current verbosity of the output.
*
* @return integer The current level of verbosity
* @return integer The current level of verbosity (one of the VERBOSITY constants)
*
* @api
*/
Expand All @@ -76,7 +78,7 @@ public function getVerbosity();
/**
* Sets the decorated flag.
*
* @param Boolean $decorated Whether to decorate the messages or not
* @param Boolean $decorated Whether to decorate the messages
*
* @api
*/
Expand Down
Loading