| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Console\Output; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Exception\InvalidArgumentException; |
| 15 |
use Symfony\Component\Console\Formatter\OutputFormatterInterface; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class StreamOutput extends Output |
| 31 |
{ |
| 32 |
|
| 33 |
private $stream; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function __construct($stream, int $verbosity = self::VERBOSITY_NORMAL, ?bool $decorated = null, ?OutputFormatterInterface $formatter = null) |
| 44 |
{ |
| 45 |
if (!\is_resource($stream) || 'stream' !== get_resource_type($stream)) { |
| 46 |
throw new InvalidArgumentException('The StreamOutput class needs a stream as its first argument.'); |
| 47 |
} |
| 48 |
|
| 49 |
$this->stream = $stream; |
| 50 |
|
| 51 |
$decorated ??= $this->hasColorSupport(); |
| 52 |
|
| 53 |
parent::__construct($verbosity, $decorated, $formatter); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function getStream() |
| 62 |
{ |
| 63 |
return $this->stream; |
| 64 |
} |
| 65 |
|
| 66 |
protected function doWrite(string $message, bool $newline): void |
| 67 |
{ |
| 68 |
if ($newline) { |
| 69 |
$message .= \PHP_EOL; |
| 70 |
} |
| 71 |
|
| 72 |
@fwrite($this->stream, $message); |
| 73 |
|
| 74 |
fflush($this->stream); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
protected function hasColorSupport(): bool |
| 91 |
{ |
| 92 |
|
| 93 |
if ('' !== (($_SERVER['NO_COLOR'] ?? getenv('NO_COLOR'))[0] ?? '')) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
if ('' !== (($_SERVER['FORCE_COLOR'] ?? getenv('FORCE_COLOR'))[0] ?? '')) { |
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
if (!@stream_isatty($this->stream) && !\in_array(strtoupper((string) getenv('MSYSTEM')), ['MINGW32', 'MINGW64'], true)) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
if ('\\' === \DIRECTORY_SEPARATOR && @sapi_windows_vt100_support($this->stream)) { |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
if ('Hyper' === getenv('TERM_PROGRAM') |
| 113 |
|| false !== getenv('COLORTERM') |
| 114 |
|| false !== getenv('ANSICON') |
| 115 |
|| 'ON' === getenv('ConEmuANSI') |
| 116 |
) { |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
if ('dumb' === $term = (string) getenv('TERM')) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
return preg_match('/^((screen|xterm|vt100|vt220|putty|rxvt|ansi|cygwin|linux).*)|(.*-256(color)?(-bce)?)$/', $term); |
| 126 |
} |
| 127 |
} |