StreamOutput.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Output;
  11. use Symfony\Component\Console\Exception\InvalidArgumentException;
  12. use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  13. /**
  14. * StreamOutput writes the output to a given stream.
  15. *
  16. * Usage:
  17. *
  18. * $output = new StreamOutput(fopen('php://stdout', 'w'));
  19. *
  20. * As `StreamOutput` can use any stream, you can also use a file:
  21. *
  22. * $output = new StreamOutput(fopen('/path/to/output.log', 'a', false));
  23. *
  24. * @author Fabien Potencier <fabien@symfony.com>
  25. */
  26. class StreamOutput extends Output
  27. {
  28. /** @var resource */
  29. private $stream;
  30. /**
  31. * @param resource $stream A stream resource
  32. * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
  33. * @param bool|null $decorated Whether to decorate messages (null for auto-guessing)
  34. * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
  35. *
  36. * @throws InvalidArgumentException When first argument is not a real stream
  37. */
  38. public function __construct($stream, int $verbosity = self::VERBOSITY_NORMAL, ?bool $decorated = null, ?OutputFormatterInterface $formatter = null)
  39. {
  40. if (!\is_resource($stream) || 'stream' !== get_resource_type($stream)) {
  41. throw new InvalidArgumentException('The StreamOutput class needs a stream as its first argument.');
  42. }
  43. $this->stream = $stream;
  44. $decorated ??= $this->hasColorSupport();
  45. parent::__construct($verbosity, $decorated, $formatter);
  46. }
  47. /**
  48. * Gets the stream attached to this StreamOutput instance.
  49. *
  50. * @return resource
  51. */
  52. public function getStream()
  53. {
  54. return $this->stream;
  55. }
  56. /**
  57. * @return void
  58. */
  59. protected function doWrite(string $message, bool $newline)
  60. {
  61. if ($newline) {
  62. $message .= \PHP_EOL;
  63. }
  64. @fwrite($this->stream, $message);
  65. fflush($this->stream);
  66. }
  67. /**
  68. * Returns true if the stream supports colorization.
  69. *
  70. * Colorization is disabled if not supported by the stream:
  71. *
  72. * This is tricky on Windows, because Cygwin, Msys2 etc emulate pseudo
  73. * terminals via named pipes, so we can only check the environment.
  74. *
  75. * Reference: Composer\XdebugHandler\Process::supportsColor
  76. * https://github.com/composer/xdebug-handler
  77. *
  78. * @return bool true if the stream supports colorization, false otherwise
  79. */
  80. protected function hasColorSupport(): bool
  81. {
  82. // Follow https://no-color.org/
  83. if ('' !== (($_SERVER['NO_COLOR'] ?? getenv('NO_COLOR'))[0] ?? '')) {
  84. return false;
  85. }
  86. // Detect msysgit/mingw and assume this is a tty because detection
  87. // does not work correctly, see https://github.com/composer/composer/issues/9690
  88. if (!@stream_isatty($this->stream) && !\in_array(strtoupper((string) getenv('MSYSTEM')), ['MINGW32', 'MINGW64'], true)) {
  89. return false;
  90. }
  91. if ('\\' === \DIRECTORY_SEPARATOR && @sapi_windows_vt100_support($this->stream)) {
  92. return true;
  93. }
  94. if ('Hyper' === getenv('TERM_PROGRAM')
  95. || false !== getenv('COLORTERM')
  96. || false !== getenv('ANSICON')
  97. || 'ON' === getenv('ConEmuANSI')
  98. ) {
  99. return true;
  100. }
  101. if ('dumb' === $term = (string) getenv('TERM')) {
  102. return false;
  103. }
  104. // See https://github.com/chalk/supports-color/blob/d4f413efaf8da045c5ab440ed418ef02dbb28bf1/index.js#L157
  105. return preg_match('/^((screen|xterm|vt100|vt220|putty|rxvt|ansi|cygwin|linux).*)|(.*-256(color)?(-bce)?)$/', $term);
  106. }
  107. }