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