Debian Codegraph @

vendor/symfony/console/Output/Output.php

raw
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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 six 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 (only output errors)
27 * * silent: --silent (no output)
28 *
29 * @author Fabien Potencier <[email protected]>
30 */
31 abstract class Output implements OutputInterface
32 {
33 private int $verbosity;
34 private OutputFormatterInterface $formatter;
35
36 /**
37 * @param int|null $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
38 * @param bool $decorated Whether to decorate messages
39 * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
40 */
41 public function __construct(?int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = false, ?OutputFormatterInterface $formatter = null)
42 {
43 $this->verbosity = $verbosity ?? self::VERBOSITY_NORMAL;
44 $this->formatter = $formatter ?? new OutputFormatter();
45 $this->formatter->setDecorated($decorated);
46 }
47
48 public function setFormatter(OutputFormatterInterface $formatter): void
49 {
50 $this->formatter = $formatter;
51 }
52
53 public function getFormatter(): OutputFormatterInterface
54 {
55 return $this->formatter;
56 }
57
58 public function setDecorated(bool $decorated): void
59 {
60 $this->formatter->setDecorated($decorated);
61 }
62
63 public function isDecorated(): bool
64 {
65 return $this->formatter->isDecorated();
66 }
67
68 public function setVerbosity(int $level): void
69 {
70 $this->verbosity = $level;
71 }
72
73 public function getVerbosity(): int
74 {
75 return $this->verbosity;
76 }
77
78 public function isSilent(): bool
79 {
80 return self::VERBOSITY_SILENT === $this->verbosity;
81 }
82
83 public function isQuiet(): bool
84 {
85 return self::VERBOSITY_QUIET === $this->verbosity;
86 }
87
88 public function isVerbose(): bool
89 {
90 return self::VERBOSITY_VERBOSE <= $this->verbosity;
91 }
92
93 public function isVeryVerbose(): bool
94 {
95 return self::VERBOSITY_VERY_VERBOSE <= $this->verbosity;
96 }
97
98 public function isDebug(): bool
99 {
100 return self::VERBOSITY_DEBUG <= $this->verbosity;
101 }
102
103 public function writeln(string|iterable $messages, int $options = self::OUTPUT_NORMAL): void
104 {
105 $this->write($messages, true, $options);
106 }
107
108 public function write(string|iterable $messages, bool $newline = false, int $options = self::OUTPUT_NORMAL): void
109 {
110 if (!is_iterable($messages)) {
111 $messages = [$messages];
112 }
113
114 $types = self::OUTPUT_NORMAL | self::OUTPUT_RAW | self::OUTPUT_PLAIN;
115 $type = $types & $options ?: self::OUTPUT_NORMAL;
116
117 $verbosities = self::VERBOSITY_QUIET | self::VERBOSITY_NORMAL | self::VERBOSITY_VERBOSE | self::VERBOSITY_VERY_VERBOSE | self::VERBOSITY_DEBUG;
118 $verbosity = $verbosities & $options ?: self::VERBOSITY_NORMAL;
119
120 if ($verbosity > $this->getVerbosity()) {
121 return;
122 }
123
124 foreach ($messages as $message) {
125 switch ($type) {
126 case OutputInterface::OUTPUT_NORMAL:
127 $message = $this->formatter->format($message);
128 break;
129 case OutputInterface::OUTPUT_RAW:
130 break;
131 case OutputInterface::OUTPUT_PLAIN:
132 $message = strip_tags($this->formatter->format($message));
133 break;
134 }
135
136 $this->doWrite($message ?? '', $newline);
137 }
138 }
139
140 /**
141 * Writes a message to the output.
142 */
143 abstract protected function doWrite(string $message, bool $newline): void;
144 }