summaryrefslogtreecommitdiff
path: root/vendor/symfony/console/Output/ConsoleOutput.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/console/Output/ConsoleOutput.php')
-rw-r--r--vendor/symfony/console/Output/ConsoleOutput.php153
1 files changed, 153 insertions, 0 deletions
diff --git a/vendor/symfony/console/Output/ConsoleOutput.php b/vendor/symfony/console/Output/ConsoleOutput.php
new file mode 100644
index 0000000..2ad3dbc
--- /dev/null
+++ b/vendor/symfony/console/Output/ConsoleOutput.php
@@ -0,0 +1,153 @@
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
12namespace Symfony\Component\Console\Output;
13
14use 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 <fabien@symfony.com>
29 */
30class 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 if (!$this->hasStdoutSupport()) {
134 return fopen('php://output', 'w');
135 }
136
137 // Use STDOUT when possible to prevent from opening too many file descriptors
138 return \defined('STDOUT') ? \STDOUT : (@fopen('php://stdout', 'w') ?: fopen('php://output', 'w'));
139 }
140
141 /**
142 * @return resource
143 */
144 private function openErrorStream()
145 {
146 if (!$this->hasStderrSupport()) {
147 return fopen('php://output', 'w');
148 }
149
150 // Use STDERR when possible to prevent from opening too many file descriptors
151 return \defined('STDERR') ? \STDERR : (@fopen('php://stderr', 'w') ?: fopen('php://output', 'w'));
152 }
153}