Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Console\Output; Chris@0: Chris@17: use Symfony\Component\Console\Formatter\OutputFormatter; Chris@0: use Symfony\Component\Console\Formatter\OutputFormatterInterface; Chris@0: Chris@0: /** Chris@0: * Base class for output classes. Chris@0: * Chris@0: * There are five levels of verbosity: Chris@0: * Chris@0: * * normal: no option passed (normal output) Chris@0: * * verbose: -v (more output) Chris@0: * * very verbose: -vv (highly extended output) Chris@0: * * debug: -vvv (all debug output) Chris@0: * * quiet: -q (no output) Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: abstract class Output implements OutputInterface Chris@0: { Chris@0: private $verbosity; Chris@0: private $formatter; Chris@0: Chris@0: /** Chris@0: * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface) Chris@0: * @param bool $decorated Whether to decorate messages Chris@0: * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter) Chris@0: */ Chris@0: public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = false, OutputFormatterInterface $formatter = null) Chris@0: { Chris@0: $this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity; Chris@0: $this->formatter = $formatter ?: new OutputFormatter(); Chris@0: $this->formatter->setDecorated($decorated); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setFormatter(OutputFormatterInterface $formatter) Chris@0: { Chris@0: $this->formatter = $formatter; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getFormatter() Chris@0: { Chris@0: return $this->formatter; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setDecorated($decorated) Chris@0: { Chris@0: $this->formatter->setDecorated($decorated); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isDecorated() Chris@0: { Chris@0: return $this->formatter->isDecorated(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setVerbosity($level) Chris@0: { Chris@0: $this->verbosity = (int) $level; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getVerbosity() Chris@0: { Chris@0: return $this->verbosity; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isQuiet() Chris@0: { Chris@0: return self::VERBOSITY_QUIET === $this->verbosity; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isVerbose() Chris@0: { Chris@0: return self::VERBOSITY_VERBOSE <= $this->verbosity; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isVeryVerbose() Chris@0: { Chris@0: return self::VERBOSITY_VERY_VERBOSE <= $this->verbosity; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isDebug() Chris@0: { Chris@0: return self::VERBOSITY_DEBUG <= $this->verbosity; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function writeln($messages, $options = self::OUTPUT_NORMAL) Chris@0: { Chris@0: $this->write($messages, true, $options); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function write($messages, $newline = false, $options = self::OUTPUT_NORMAL) Chris@0: { Chris@0: $messages = (array) $messages; Chris@0: Chris@0: $types = self::OUTPUT_NORMAL | self::OUTPUT_RAW | self::OUTPUT_PLAIN; Chris@0: $type = $types & $options ?: self::OUTPUT_NORMAL; Chris@0: Chris@0: $verbosities = self::VERBOSITY_QUIET | self::VERBOSITY_NORMAL | self::VERBOSITY_VERBOSE | self::VERBOSITY_VERY_VERBOSE | self::VERBOSITY_DEBUG; Chris@0: $verbosity = $verbosities & $options ?: self::VERBOSITY_NORMAL; Chris@0: Chris@0: if ($verbosity > $this->getVerbosity()) { Chris@0: return; Chris@0: } Chris@0: Chris@0: foreach ($messages as $message) { Chris@0: switch ($type) { Chris@0: case OutputInterface::OUTPUT_NORMAL: Chris@0: $message = $this->formatter->format($message); Chris@0: break; Chris@0: case OutputInterface::OUTPUT_RAW: Chris@0: break; Chris@0: case OutputInterface::OUTPUT_PLAIN: Chris@0: $message = strip_tags($this->formatter->format($message)); Chris@0: break; Chris@0: } Chris@0: Chris@0: $this->doWrite($message, $newline); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Writes a message to the output. Chris@0: * Chris@0: * @param string $message A message to write to the output Chris@0: * @param bool $newline Whether to add a newline or not Chris@0: */ Chris@0: abstract protected function doWrite($message, $newline); Chris@0: }