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
38 * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
39 * @param bool $decorated Whether to decorate messages
40 * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
42 public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = false, OutputFormatterInterface $formatter = null)
44 $this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity;
45 $this->formatter = $formatter ?: new OutputFormatter();
46 $this->formatter->setDecorated($decorated);
52 public function setFormatter(OutputFormatterInterface $formatter)
54 $this->formatter = $formatter;
60 public function getFormatter()
62 return $this->formatter;
68 public function setDecorated($decorated)
70 $this->formatter->setDecorated($decorated);
76 public function isDecorated()
78 return $this->formatter->isDecorated();
84 public function setVerbosity($level)
86 $this->verbosity = (int) $level;
92 public function getVerbosity()
94 return $this->verbosity;
100 public function isQuiet()
102 return self::VERBOSITY_QUIET === $this->verbosity;
108 public function isVerbose()
110 return self::VERBOSITY_VERBOSE <= $this->verbosity;
116 public function isVeryVerbose()
118 return self::VERBOSITY_VERY_VERBOSE <= $this->verbosity;
124 public function isDebug()
126 return self::VERBOSITY_DEBUG <= $this->verbosity;
132 public function writeln($messages, $options = self::OUTPUT_NORMAL)
134 $this->write($messages, true, $options);
140 public function write($messages, $newline = false, $options = self::OUTPUT_NORMAL)
142 $messages = (array) $messages;
144 $types = self::OUTPUT_NORMAL | self::OUTPUT_RAW | self::OUTPUT_PLAIN;
145 $type = $types & $options ?: self::OUTPUT_NORMAL;
147 $verbosities = self::VERBOSITY_QUIET | self::VERBOSITY_NORMAL | self::VERBOSITY_VERBOSE | self::VERBOSITY_VERY_VERBOSE | self::VERBOSITY_DEBUG;
148 $verbosity = $verbosities & $options ?: self::VERBOSITY_NORMAL;
150 if ($verbosity > $this->getVerbosity()) {
154 foreach ($messages as $message) {
156 case OutputInterface::OUTPUT_NORMAL:
157 $message = $this->formatter->format($message);
159 case OutputInterface::OUTPUT_RAW:
161 case OutputInterface::OUTPUT_PLAIN:
162 $message = strip_tags($this->formatter->format($message));
166 $this->doWrite($message, $newline);
171 * Writes a message to the output.
173 * @param string $message A message to write to the output
174 * @param bool $newline Whether to add a newline or not
176 abstract protected function doWrite($message, $newline);