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 three levels of verbosity:
22 * * normal: no option passed (normal output - information)
23 * * verbose: -v (more output - debug)
24 * * quiet: -q (no output)
26 * @author Fabien Potencier <fabien@symfony.com>
30 abstract class Output implements OutputInterface
38 * @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL, self::VERBOSITY_VERBOSE)
39 * @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing)
40 * @param OutputFormatterInterface $formatter Output formatter instance
44 public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
46 $this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity;
47 $this->formatter = null === $formatter ? new OutputFormatter() : $formatter;
48 $this->formatter->setDecorated((Boolean) $decorated);
52 * Sets output formatter.
54 * @param OutputFormatterInterface $formatter
58 public function setFormatter(OutputFormatterInterface $formatter)
60 $this->formatter = $formatter;
64 * Returns current output formatter instance.
66 * @return OutputFormatterInterface
70 public function getFormatter()
72 return $this->formatter;
76 * Sets the decorated flag.
78 * @param Boolean $decorated Whether to decorate the messages or not
82 public function setDecorated($decorated)
84 $this->formatter->setDecorated((Boolean) $decorated);
88 * Gets the decorated flag.
90 * @return Boolean true if the output will decorate messages, false otherwise
94 public function isDecorated()
96 return $this->formatter->isDecorated();
100 * Sets the verbosity of the output.
102 * @param integer $level The level of verbosity
106 public function setVerbosity($level)
108 $this->verbosity = (int) $level;
112 * Gets the current verbosity of the output.
114 * @return integer The current level of verbosity
118 public function getVerbosity()
120 return $this->verbosity;
124 * Writes a message to the output and adds a newline at the end.
126 * @param string|array $messages The message as an array of lines of a single string
127 * @param integer $type The type of output
131 public function writeln($messages, $type = 0)
133 $this->write($messages, true, $type);
137 * Writes a message to the output.
139 * @param string|array $messages The message as an array of lines of a single string
140 * @param Boolean $newline Whether to add a newline or not
141 * @param integer $type The type of output
143 * @throws \InvalidArgumentException When unknown output type is given
147 public function write($messages, $newline = false, $type = 0)
149 if (self::VERBOSITY_QUIET === $this->verbosity) {
153 $messages = (array) $messages;
155 foreach ($messages as $message) {
157 case OutputInterface::OUTPUT_NORMAL:
158 $message = $this->formatter->format($message);
160 case OutputInterface::OUTPUT_RAW:
162 case OutputInterface::OUTPUT_PLAIN:
163 $message = strip_tags($this->formatter->format($message));
166 throw new \InvalidArgumentException(sprintf('Unknown output type given (%s)', $type));
169 $this->doWrite($message, $newline);
174 * Writes a message to the output.
176 * @param string $message A message to write to the output
177 * @param Boolean $newline Whether to add a newline or not
179 abstract protected function doWrite($message, $newline);