StreamOutput.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. private $stream;
  29. /**
  30. * @param resource $stream A stream resource
  31. * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
  32. * @param bool|null $decorated Whether to decorate messages (null for auto-guessing)
  33. * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
  34. *
  35. * @throws InvalidArgumentException When first argument is not a real stream
  36. */
  37. public function __construct($stream, int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = null, OutputFormatterInterface $formatter = null)
  38. {
  39. if (!\is_resource($stream) || 'stream' !== get_resource_type($stream)) {
  40. throw new InvalidArgumentException('The StreamOutput class needs a stream as its first argument.');
  41. }
  42. $this->stream = $stream;
  43. if (null === $decorated) {
  44. $decorated = $this->hasColorSupport();
  45. }
  46. parent::__construct($verbosity, $decorated, $formatter);
  47. }
  48. /**
  49. * Gets the stream attached to this StreamOutput instance.
  50. *
  51. * @return resource
  52. */
  53. public function getStream()
  54. {
  55. return $this->stream;
  56. }
  57. protected function doWrite(string $message, bool $newline)
  58. {
  59. if ($newline) {
  60. $message .= \PHP_EOL;
  61. }
  62. @fwrite($this->stream, $message);
  63. fflush($this->stream);
  64. }
  65. /**
  66. * Returns true if the stream supports colorization.
  67. *
  68. * Colorization is disabled if not supported by the stream:
  69. *
  70. * This is tricky on Windows, because Cygwin, Msys2 etc emulate pseudo
  71. * terminals via named pipes, so we can only check the environment.
  72. *
  73. * Reference: Composer\XdebugHandler\Process::supportsColor
  74. * https://github.com/composer/xdebug-handler
  75. *
  76. * @return bool true if the stream supports colorization, false otherwise
  77. */
  78. protected function hasColorSupport()
  79. {
  80. // Follow https://no-color.org/
  81. if (isset($_SERVER['NO_COLOR']) || false !== getenv('NO_COLOR')) {
  82. return false;
  83. }
  84. if (\DIRECTORY_SEPARATOR === '\\'
  85. && \function_exists('sapi_windows_vt100_support')
  86. && @sapi_windows_vt100_support($this->stream)
  87. ) {
  88. return true;
  89. }
  90. return 'Hyper' === getenv('TERM_PROGRAM')
  91. || false !== getenv('ANSICON')
  92. || 'ON' === getenv('ConEmuANSI')
  93. || str_starts_with((string) getenv('TERM'), 'xterm')
  94. || stream_isatty($this->stream);
  95. }
  96. }