You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.5 KiB

3 years ago
  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 A stream resource
  52. */
  53. public function getStream()
  54. {
  55. return $this->stream;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. protected function doWrite(string $message, bool $newline)
  61. {
  62. if ($newline) {
  63. $message .= \PHP_EOL;
  64. }
  65. @fwrite($this->stream, $message);
  66. fflush($this->stream);
  67. }
  68. /**
  69. * Returns true if the stream supports colorization.
  70. *
  71. * Colorization is disabled if not supported by the stream:
  72. *
  73. * This is tricky on Windows, because Cygwin, Msys2 etc emulate pseudo
  74. * terminals via named pipes, so we can only check the environment.
  75. *
  76. * Reference: Composer\XdebugHandler\Process::supportsColor
  77. * https://github.com/composer/xdebug-handler
  78. *
  79. * @return bool true if the stream supports colorization, false otherwise
  80. */
  81. protected function hasColorSupport()
  82. {
  83. // Follow https://no-color.org/
  84. if (isset($_SERVER['NO_COLOR']) || false !== getenv('NO_COLOR')) {
  85. return false;
  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. return stream_isatty($this->stream);
  98. }
  99. }