StreamOutput.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\Exception\RuntimeException;
  13. use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  14. /**
  15. * StreamOutput writes the output to a given stream.
  16. *
  17. * Usage:
  18. *
  19. * $output = new StreamOutput(fopen('php://stdout', 'w'));
  20. *
  21. * As `StreamOutput` can use any stream, you can also use a file:
  22. *
  23. * $output = new StreamOutput(fopen('/path/to/output.log', 'a', false));
  24. *
  25. * @author Fabien Potencier <fabien@symfony.com>
  26. */
  27. class StreamOutput extends Output
  28. {
  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. if (null === $decorated) {
  45. $decorated = $this->hasColorSupport();
  46. }
  47. parent::__construct($verbosity, $decorated, $formatter);
  48. }
  49. /**
  50. * Gets the stream attached to this StreamOutput instance.
  51. *
  52. * @return resource A stream resource
  53. */
  54. public function getStream()
  55. {
  56. return $this->stream;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. protected function doWrite($message, $newline)
  62. {
  63. if ($newline) {
  64. $message .= PHP_EOL;
  65. }
  66. if (false === @fwrite($this->stream, $message)) {
  67. // should never happen
  68. throw new RuntimeException('Unable to write output.');
  69. }
  70. fflush($this->stream);
  71. }
  72. /**
  73. * Returns true if the stream supports colorization.
  74. *
  75. * Colorization is disabled if not supported by the stream:
  76. *
  77. * This is tricky on Windows, because Cygwin, Msys2 etc emulate pseudo
  78. * terminals via named pipes, so we can only check the environment.
  79. *
  80. * Reference: Composer\XdebugHandler\Process::supportsColor
  81. * https://github.com/composer/xdebug-handler
  82. *
  83. * @return bool true if the stream supports colorization, false otherwise
  84. */
  85. protected function hasColorSupport()
  86. {
  87. if ('Hyper' === getenv('TERM_PROGRAM')) {
  88. return true;
  89. }
  90. if (\DIRECTORY_SEPARATOR === '\\') {
  91. return (\function_exists('sapi_windows_vt100_support')
  92. && @sapi_windows_vt100_support($this->stream))
  93. || false !== getenv('ANSICON')
  94. || 'ON' === getenv('ConEmuANSI')
  95. || 'xterm' === getenv('TERM');
  96. }
  97. if (\function_exists('stream_isatty')) {
  98. return @stream_isatty($this->stream);
  99. }
  100. if (\function_exists('posix_isatty')) {
  101. return @posix_isatty($this->stream);
  102. }
  103. $stat = @fstat($this->stream);
  104. // Check if formatted mode is S_IFCHR
  105. return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
  106. }
  107. }