| 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\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 |
*/ |
| 30 |
class ConsoleOutput extends StreamOutput implements ConsoleOutputInterface |
| 31 |
{ |
| 32 |
private $stderr; |
| 33 |
|
| 34 |
/** |
| 35 |
* @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface) |
| 36 |
* @param bool|null $decorated Whether to decorate messages (null for auto-guessing) |
| 37 |
* @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter) |
| 38 |
*/ |
| 39 |
public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null) |
| 40 |
{ |
| 41 |
parent::__construct($this->openOutputStream(), $verbosity, $decorated, $formatter); |
| 42 |
|
| 43 |
$actualDecorated = $this->isDecorated(); |
| 44 |
$this->stderr = new StreamOutput($this->openErrorStream(), $verbosity, $decorated, $this->getFormatter()); |
| 45 |
|
| 46 |
if (null === $decorated) { |
| 47 |
$this->setDecorated($actualDecorated && $this->stderr->isDecorated()); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* {@inheritdoc} |
| 53 |
*/ |
| 54 |
public function setDecorated($decorated) |
| 55 |
{ |
| 56 |
parent::setDecorated($decorated); |
| 57 |
$this->stderr->setDecorated($decorated); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* {@inheritdoc} |
| 62 |
*/ |
| 63 |
public function setFormatter(OutputFormatterInterface $formatter) |
| 64 |
{ |
| 65 |
parent::setFormatter($formatter); |
| 66 |
$this->stderr->setFormatter($formatter); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* {@inheritdoc} |
| 71 |
*/ |
| 72 |
public function setVerbosity($level) |
| 73 |
{ |
| 74 |
parent::setVerbosity($level); |
| 75 |
$this->stderr->setVerbosity($level); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* {@inheritdoc} |
| 80 |
*/ |
| 81 |
public function getErrorOutput() |
| 82 |
{ |
| 83 |
return $this->stderr; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* {@inheritdoc} |
| 88 |
*/ |
| 89 |
public function setErrorOutput(OutputInterface $error) |
| 90 |
{ |
| 91 |
$this->stderr = $error; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Returns true if current environment supports writing console output to |
| 96 |
* STDOUT. |
| 97 |
* |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
protected function hasStdoutSupport() |
| 101 |
{ |
| 102 |
return false === $this->isRunningOS400(); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Returns true if current environment supports writing console output to |
| 107 |
* STDERR. |
| 108 |
* |
| 109 |
* @return bool |
| 110 |
*/ |
| 111 |
protected function hasStderrSupport() |
| 112 |
{ |
| 113 |
return false === $this->isRunningOS400(); |
| 114 |
} |
| 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 |
* @return bool |
| 121 |
*/ |
| 122 |
private function isRunningOS400() |
| 123 |
{ |
| 124 |
$checks = [ |
| 125 |
\function_exists('php_uname') ? php_uname('s') : '', |
| 126 |
getenv('OSTYPE'), |
| 127 |
\PHP_OS, |
| 128 |
]; |
| 129 |
|
| 130 |
return false !== stripos(implode(';', $checks), 'OS400'); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* @return resource |
| 135 |
*/ |
| 136 |
private function openOutputStream() |
| 137 |
{ |
| 138 |
if (!$this->hasStdoutSupport()) { |
| 139 |
return fopen('php://output', 'w'); |
| 140 |
} |
| 141 |
|
| 142 |
return @fopen('php://stdout', 'w') ?: fopen('php://output', 'w'); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* @return resource |
| 147 |
*/ |
| 148 |
private function openErrorStream() |
| 149 |
{ |
| 150 |
return fopen($this->hasStderrSupport() ? 'php://stderr' : 'php://output', 'w'); |
| 151 |
} |
| 152 |
} |
| 153 |
|