4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Output;
14 use Symfony\Component\Console\Formatter\OutputFormatterInterface;
15 use Symfony\Component\Console\Formatter\OutputFormatter;
18 * Base class for output classes.
20 * There are five levels of verbosity:
22 * * normal: no option passed (normal output)
23 * * verbose: -v (more output)
24 * * very verbose: -vv (highly extended output)
25 * * debug: -vvv (all debug output)
26 * * quiet: -q (no output)
28 * @author Fabien Potencier <fabien@symfony.com>
30 abstract class Output implements OutputInterface
36 * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
37 * @param bool $decorated Whether to decorate messages
38 * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
40 public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = false, OutputFormatterInterface $formatter = null)
42 $this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity;
43 $this->formatter = $formatter ?: new OutputFormatter();
44 $this->formatter->setDecorated($decorated);
50 public function setFormatter(OutputFormatterInterface $formatter)
52 $this->formatter = $formatter;
58 public function getFormatter()
60 return $this->formatter;
66 public function setDecorated($decorated)
68 $this->formatter->setDecorated($decorated);
74 public function isDecorated()
76 return $this->formatter->isDecorated();
82 public function setVerbosity($level)
84 $this->verbosity = (int) $level;
90 public function getVerbosity()
92 return $this->verbosity;
98 public function isQuiet()
100 return self::VERBOSITY_QUIET === $this->verbosity;
106 public function isVerbose()
108 return self::VERBOSITY_VERBOSE <= $this->verbosity;
114 public function isVeryVerbose()
116 return self::VERBOSITY_VERY_VERBOSE <= $this->verbosity;
122 public function isDebug()
124 return self::VERBOSITY_DEBUG <= $this->verbosity;
130 public function writeln($messages, $options = self::OUTPUT_NORMAL)
132 $this->write($messages, true, $options);
138 public function write($messages, $newline = false, $options = self::OUTPUT_NORMAL)
140 $messages = (array) $messages;
142 $types = self::OUTPUT_NORMAL | self::OUTPUT_RAW | self::OUTPUT_PLAIN;
143 $type = $types & $options ?: self::OUTPUT_NORMAL;
145 $verbosities = self::VERBOSITY_QUIET | self::VERBOSITY_NORMAL | self::VERBOSITY_VERBOSE | self::VERBOSITY_VERY_VERBOSE | self::VERBOSITY_DEBUG;
146 $verbosity = $verbosities & $options ?: self::VERBOSITY_NORMAL;
148 if ($verbosity > $this->getVerbosity()) {
152 foreach ($messages as $message) {
154 case OutputInterface::OUTPUT_NORMAL:
155 $message = $this->formatter->format($message);
157 case OutputInterface::OUTPUT_RAW:
159 case OutputInterface::OUTPUT_PLAIN:
160 $message = strip_tags($this->formatter->format($message));
164 $this->doWrite($message, $newline);
169 * Writes a message to the output.
171 * @param string $message A message to write to the output
172 * @param bool $newline Whether to add a newline or not
174 abstract protected function doWrite($message, $newline);