Debian Codegraph @

vendor/symfony/console/Output/ConsoleOutput.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\OutputFormatterInterface;
15
16 /**
17 * ConsoleOutput is the default class for all CLI output. It uses STDOUT and STDERR.
18 *
19 * This class is a convenient wrapper around `StreamOutput` for both STDOUT and STDERR.
20 *
21 * $output = new ConsoleOutput();
22 *
23 * This is equivalent to:
24 *
25 * $output = new StreamOutput(fopen('php://stdout', 'w'));
26 * $stdErr = new StreamOutput(fopen('php://stderr', 'w'));
27 *
28 * @author Fabien Potencier <[email protected]>
29 */
30 class ConsoleOutput extends StreamOutput implements ConsoleOutputInterface
31 {
32 private OutputInterface $stderr;
33 private array $consoleSectionOutputs = [];
34
35 /**
36 * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
37 * @param bool|null $decorated Whether to decorate messages (null for auto-guessing)
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 = null, ?OutputFormatterInterface $formatter = null)
41 {
42 parent::__construct($this->openOutputStream(), $verbosity, $decorated, $formatter);
43
44 if (null === $formatter) {
45 // for BC reasons, stdErr has it own Formatter only when user don't inject a specific formatter.
46 $this->stderr = new StreamOutput($this->openErrorStream(), $verbosity, $decorated);
47
48 return;
49 }
50
51 $actualDecorated = $this->isDecorated();
52 $this->stderr = new StreamOutput($this->openErrorStream(), $verbosity, $decorated, $this->getFormatter());
53
54 if (null === $decorated) {
55 $this->setDecorated($actualDecorated && $this->stderr->isDecorated());
56 }
57 }
58
59 /**
60 * Creates a new output section.
61 */
62 public function section(): ConsoleSectionOutput
63 {
64 return new ConsoleSectionOutput($this->getStream(), $this->consoleSectionOutputs, $this->getVerbosity(), $this->isDecorated(), $this->getFormatter());
65 }
66
67 public function setDecorated(bool $decorated): void
68 {
69 parent::setDecorated($decorated);
70 $this->stderr->setDecorated($decorated);
71 }
72
73 public function setFormatter(OutputFormatterInterface $formatter): void
74 {
75 parent::setFormatter($formatter);
76 $this->stderr->setFormatter($formatter);
77 }
78
79 public function setVerbosity(int $level): void
80 {
81 parent::setVerbosity($level);
82 $this->stderr->setVerbosity($level);
83 }
84
85 public function getErrorOutput(): OutputInterface
86 {
87 return $this->stderr;
88 }
89
90 public function setErrorOutput(OutputInterface $error): void
91 {
92 $this->stderr = $error;
93 }
94
95 /**
96 * Returns true if current environment supports writing console output to
97 * STDOUT.
98 */
99 protected function hasStdoutSupport(): bool
100 {
101 return false === $this->isRunningOS400();
102 }
103
104 /**
105 * Returns true if current environment supports writing console output to
106 * STDERR.
107 */
108 protected function hasStderrSupport(): bool
109 {
110 return false === $this->isRunningOS400();
111 }
112
113 /**
114 * Checks if current executing environment is IBM iSeries (OS400), which
115 * doesn't properly convert character-encodings between ASCII to EBCDIC.
116 */
117 private function isRunningOS400(): bool
118 {
119 $checks = [
120 \function_exists('php_uname') ? php_uname('s') : '',
121 getenv('OSTYPE'),
122 \PHP_OS,
123 ];
124
125 return false !== stripos(implode(';', $checks), 'OS400');
126 }
127
128 /**
129 * @return resource
130 */
131 private function openOutputStream()
132 {
133 static $stdout;
134
135 if ($stdout) {
136 return $stdout;
137 }
138
139 if (!$this->hasStdoutSupport()) {
140 return $stdout = fopen('php://output', 'w');
141 }
142
143 // Use STDOUT when possible to prevent from opening too many file descriptors
144 if (!\defined('STDOUT')) {
145 return $stdout = @fopen('php://stdout', 'w') ?: fopen('php://output', 'w');
146 }
147
148 // On Windows, STDOUT is opened in text mode; reopen in binary mode to prevent \n to \r\n conversion
149 if ('\\' === \DIRECTORY_SEPARATOR) {
150 return $stdout = @fopen('php://stdout', 'w') ?: \STDOUT;
151 }
152
153 return $stdout = \STDOUT;
154 }
155
156 /**
157 * @return resource
158 */
159 private function openErrorStream()
160 {
161 static $stderr;
162
163 if ($stderr) {
164 return $stderr;
165 }
166
167 if (!$this->hasStderrSupport()) {
168 return $stderr = fopen('php://output', 'w');
169 }
170
171 // Use STDERR when possible to prevent from opening too many file descriptors
172 if (!\defined('STDERR')) {
173 return $stderr = @fopen('php://stderr', 'w') ?: fopen('php://output', 'w');
174 }
175
176 // On Windows, STDERR is opened in text mode; reopen in binary mode to prevent \n → \r\n conversion
177 if ('\\' === \DIRECTORY_SEPARATOR) {
178 return $stderr = @fopen('php://stderr', 'w') ?: \STDERR;
179 }
180
181 return $stderr ??= \STDERR;
182 }
183 }