| 148 |
lars |
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 int $verbosity;
|
|
|
33 |
private OutputFormatterInterface $formatter;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* @param int|null $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(?int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = false, OutputFormatterInterface $formatter = null)
|
|
|
41 |
{
|
|
|
42 |
$this->verbosity = $verbosity ?? self::VERBOSITY_NORMAL;
|
|
|
43 |
$this->formatter = $formatter ?? new OutputFormatter();
|
|
|
44 |
$this->formatter->setDecorated($decorated);
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
public function setFormatter(OutputFormatterInterface $formatter)
|
|
|
48 |
{
|
|
|
49 |
$this->formatter = $formatter;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
public function getFormatter(): OutputFormatterInterface
|
|
|
53 |
{
|
|
|
54 |
return $this->formatter;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
public function setDecorated(bool $decorated)
|
|
|
58 |
{
|
|
|
59 |
$this->formatter->setDecorated($decorated);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
public function isDecorated(): bool
|
|
|
63 |
{
|
|
|
64 |
return $this->formatter->isDecorated();
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
public function setVerbosity(int $level)
|
|
|
68 |
{
|
|
|
69 |
$this->verbosity = $level;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public function getVerbosity(): int
|
|
|
73 |
{
|
|
|
74 |
return $this->verbosity;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
public function isQuiet(): bool
|
|
|
78 |
{
|
|
|
79 |
return self::VERBOSITY_QUIET === $this->verbosity;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
public function isVerbose(): bool
|
|
|
83 |
{
|
|
|
84 |
return self::VERBOSITY_VERBOSE <= $this->verbosity;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
public function isVeryVerbose(): bool
|
|
|
88 |
{
|
|
|
89 |
return self::VERBOSITY_VERY_VERBOSE <= $this->verbosity;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
public function isDebug(): bool
|
|
|
93 |
{
|
|
|
94 |
return self::VERBOSITY_DEBUG <= $this->verbosity;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
public function writeln(string|iterable $messages, int $options = self::OUTPUT_NORMAL)
|
|
|
98 |
{
|
|
|
99 |
$this->write($messages, true, $options);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
public function write(string|iterable $messages, bool $newline = false, int $options = self::OUTPUT_NORMAL)
|
|
|
103 |
{
|
|
|
104 |
if (!is_iterable($messages)) {
|
|
|
105 |
$messages = [$messages];
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
$types = self::OUTPUT_NORMAL | self::OUTPUT_RAW | self::OUTPUT_PLAIN;
|
|
|
109 |
$type = $types & $options ?: self::OUTPUT_NORMAL;
|
|
|
110 |
|
|
|
111 |
$verbosities = self::VERBOSITY_QUIET | self::VERBOSITY_NORMAL | self::VERBOSITY_VERBOSE | self::VERBOSITY_VERY_VERBOSE | self::VERBOSITY_DEBUG;
|
|
|
112 |
$verbosity = $verbosities & $options ?: self::VERBOSITY_NORMAL;
|
|
|
113 |
|
|
|
114 |
if ($verbosity > $this->getVerbosity()) {
|
|
|
115 |
return;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
foreach ($messages as $message) {
|
|
|
119 |
switch ($type) {
|
|
|
120 |
case OutputInterface::OUTPUT_NORMAL:
|
|
|
121 |
$message = $this->formatter->format($message);
|
|
|
122 |
break;
|
|
|
123 |
case OutputInterface::OUTPUT_RAW:
|
|
|
124 |
break;
|
|
|
125 |
case OutputInterface::OUTPUT_PLAIN:
|
|
|
126 |
$message = strip_tags($this->formatter->format($message));
|
|
|
127 |
break;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
$this->doWrite($message ?? '', $newline);
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
/**
|
|
|
135 |
* Writes a message to the output.
|
|
|
136 |
*/
|
|
|
137 |
abstract protected function doWrite(string $message, bool $newline);
|
|
|
138 |
}
|