diff options
Diffstat (limited to 'vendor/symfony/console/Output/StreamOutput.php')
| -rw-r--r-- | vendor/symfony/console/Output/StreamOutput.php | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/vendor/symfony/console/Output/StreamOutput.php b/vendor/symfony/console/Output/StreamOutput.php new file mode 100644 index 0000000..b46f4d2 --- /dev/null +++ b/vendor/symfony/console/Output/StreamOutput.php | |||
| @@ -0,0 +1,122 @@ | |||
| 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\Exception\InvalidArgumentException; | ||
| 15 | use Symfony\Component\Console\Formatter\OutputFormatterInterface; | ||
| 16 | |||
| 17 | /** | ||
| 18 | * StreamOutput writes the output to a given stream. | ||
| 19 | * | ||
| 20 | * Usage: | ||
| 21 | * | ||
| 22 | * $output = new StreamOutput(fopen('php://stdout', 'w')); | ||
| 23 | * | ||
| 24 | * As `StreamOutput` can use any stream, you can also use a file: | ||
| 25 | * | ||
| 26 | * $output = new StreamOutput(fopen('/path/to/output.log', 'a', false)); | ||
| 27 | * | ||
| 28 | * @author Fabien Potencier <fabien@symfony.com> | ||
| 29 | */ | ||
| 30 | class StreamOutput extends Output | ||
| 31 | { | ||
| 32 | /** @var resource */ | ||
| 33 | private $stream; | ||
| 34 | |||
| 35 | /** | ||
| 36 | * @param resource $stream A stream resource | ||
| 37 | * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface) | ||
| 38 | * @param bool|null $decorated Whether to decorate messages (null for auto-guessing) | ||
| 39 | * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter) | ||
| 40 | * | ||
| 41 | * @throws InvalidArgumentException When first argument is not a real stream | ||
| 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 | * Gets the stream attached to this StreamOutput instance. | ||
| 58 | * | ||
| 59 | * @return resource | ||
| 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 | * Returns true if the stream supports colorization. | ||
| 79 | * | ||
| 80 | * Colorization is disabled if not supported by the stream: | ||
| 81 | * | ||
| 82 | * This is tricky on Windows, because Cygwin, Msys2 etc emulate pseudo | ||
| 83 | * terminals via named pipes, so we can only check the environment. | ||
| 84 | * | ||
| 85 | * Reference: Composer\XdebugHandler\Process::supportsColor | ||
| 86 | * https://github.com/composer/xdebug-handler | ||
| 87 | * | ||
| 88 | * @return bool true if the stream supports colorization, false otherwise | ||
| 89 | */ | ||
| 90 | protected function hasColorSupport(): bool | ||
| 91 | { | ||
| 92 | // Follow https://no-color.org/ | ||
| 93 | if ('' !== ($_SERVER['NO_COLOR'] ?? getenv('NO_COLOR') ?: '')) { | ||
| 94 | return false; | ||
| 95 | } | ||
| 96 | |||
| 97 | // Detect msysgit/mingw and assume this is a tty because detection | ||
| 98 | // does not work correctly, see https://github.com/composer/composer/issues/9690 | ||
| 99 | if (!@stream_isatty($this->stream) && !\in_array(strtoupper((string) getenv('MSYSTEM')), ['MINGW32', 'MINGW64'], true)) { | ||
| 100 | return false; | ||
| 101 | } | ||
| 102 | |||
| 103 | if ('\\' === \DIRECTORY_SEPARATOR && @sapi_windows_vt100_support($this->stream)) { | ||
| 104 | return true; | ||
| 105 | } | ||
| 106 | |||
| 107 | if ('Hyper' === getenv('TERM_PROGRAM') | ||
| 108 | || false !== getenv('COLORTERM') | ||
| 109 | || false !== getenv('ANSICON') | ||
| 110 | || 'ON' === getenv('ConEmuANSI') | ||
| 111 | ) { | ||
| 112 | return true; | ||
| 113 | } | ||
| 114 | |||
| 115 | if ('dumb' === $term = (string) getenv('TERM')) { | ||
| 116 | return false; | ||
| 117 | } | ||
| 118 | |||
| 119 | // See https://github.com/chalk/supports-color/blob/d4f413efaf8da045c5ab440ed418ef02dbb28bf1/index.js#L157 | ||
| 120 | return preg_match('/^((screen|xterm|vt100|vt220|putty|rxvt|ansi|cygwin|linux).*)|(.*-256(color)?(-bce)?)$/', $term); | ||
| 121 | } | ||
| 122 | } | ||
