| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Console\Output; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Formatter\OutputFormatter; |
| 15 |
use Symfony\Component\Console\Formatter\OutputFormatterInterface; |
| 16 |
|
| 17 |
/** |
| 18 |
* Base class for output classes. |
| 19 |
* |
| 20 |
* There are five levels of verbosity: |
| 21 |
* |
| 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) |
| 27 |
* |
| 28 |
* @author Fabien Potencier <fabien@symfony.com> |
| 29 |
*/ |
| 30 |
abstract class Output implements OutputInterface |
| 31 |
{ |
| 32 |
private $verbosity; |
| 33 |
private $formatter; |
| 34 |
|
| 35 |
/** |
| 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) |
| 39 |
*/ |
| 40 |
public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = false, OutputFormatterInterface $formatter = null) |
| 41 |
{ |
| 42 |
$this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity; |
| 43 |
$this->formatter = $formatter ?: new OutputFormatter(); |
| 44 |
$this->formatter->setDecorated($decorated); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* {@inheritdoc} |
| 49 |
*/ |
| 50 |
public function setFormatter(OutputFormatterInterface $formatter) |
| 51 |
{ |
| 52 |
$this->formatter = $formatter; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* {@inheritdoc} |
| 57 |
*/ |
| 58 |
public function getFormatter() |
| 59 |
{ |
| 60 |
return $this->formatter; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* {@inheritdoc} |
| 65 |
*/ |
| 66 |
public function setDecorated($decorated) |
| 67 |
{ |
| 68 |
$this->formatter->setDecorated($decorated); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* {@inheritdoc} |
| 73 |
*/ |
| 74 |
public function isDecorated() |
| 75 |
{ |
| 76 |
return $this->formatter->isDecorated(); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* {@inheritdoc} |
| 81 |
*/ |
| 82 |
public function setVerbosity($level) |
| 83 |
{ |
| 84 |
$this->verbosity = (int) $level; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* {@inheritdoc} |
| 89 |
*/ |
| 90 |
public function getVerbosity() |
| 91 |
{ |
| 92 |
return $this->verbosity; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* {@inheritdoc} |
| 97 |
*/ |
| 98 |
public function isQuiet() |
| 99 |
{ |
| 100 |
return self::VERBOSITY_QUIET === $this->verbosity; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* {@inheritdoc} |
| 105 |
*/ |
| 106 |
public function isVerbose() |
| 107 |
{ |
| 108 |
return self::VERBOSITY_VERBOSE <= $this->verbosity; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* {@inheritdoc} |
| 113 |
*/ |
| 114 |
public function isVeryVerbose() |
| 115 |
{ |
| 116 |
return self::VERBOSITY_VERY_VERBOSE <= $this->verbosity; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* {@inheritdoc} |
| 121 |
*/ |
| 122 |
public function isDebug() |
| 123 |
{ |
| 124 |
return self::VERBOSITY_DEBUG <= $this->verbosity; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* {@inheritdoc} |
| 129 |
*/ |
| 130 |
public function writeln($messages, $options = self::OUTPUT_NORMAL) |
| 131 |
{ |
| 132 |
$this->write($messages, true, $options); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* {@inheritdoc} |
| 137 |
*/ |
| 138 |
public function write($messages, $newline = false, $options = self::OUTPUT_NORMAL) |
| 139 |
{ |
| 140 |
$messages = (array) $messages; |
| 141 |
|
| 142 |
$types = self::OUTPUT_NORMAL | self::OUTPUT_RAW | self::OUTPUT_PLAIN; |
| 143 |
$type = $types & $options ?: self::OUTPUT_NORMAL; |
| 144 |
|
| 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; |
| 147 |
|
| 148 |
if ($verbosity > $this->getVerbosity()) { |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
foreach ($messages as $message) { |
| 153 |
switch ($type) { |
| 154 |
case OutputInterface::OUTPUT_NORMAL: |
| 155 |
$message = $this->formatter->format($message); |
| 156 |
break; |
| 157 |
case OutputInterface::OUTPUT_RAW: |
| 158 |
break; |
| 159 |
case OutputInterface::OUTPUT_PLAIN: |
| 160 |
$message = strip_tags($this->formatter->format($message)); |
| 161 |
break; |
| 162 |
} |
| 163 |
|
| 164 |
$this->doWrite($message, $newline); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Writes a message to the output. |
| 170 |
* |
| 171 |
* @param string $message A message to write to the output |
| 172 |
* @param bool $newline Whether to add a newline or not |
| 173 |
*/ |
| 174 |
abstract protected function doWrite($message, $newline); |
| 175 |
} |
| 176 |
|