StreamOutput.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\Formatter\OutputFormatterInterface;
  12. /**
  13. * StreamOutput writes the output to a given stream.
  14. *
  15. * Usage:
  16. *
  17. * $output = new StreamOutput(fopen('php://stdout', 'w'));
  18. *
  19. * As `StreamOutput` can use any stream, you can also use a file:
  20. *
  21. * $output = new StreamOutput(fopen('/path/to/output.log', 'a', false));
  22. *
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. *
  25. * @api
  26. */
  27. class StreamOutput extends Output
  28. {
  29. private $stream;
  30. /**
  31. * Constructor.
  32. *
  33. * @param mixed $stream A stream resource
  34. * @param integer $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
  35. * @param Boolean|null $decorated Whether to decorate messages (null for auto-guessing)
  36. * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
  37. *
  38. * @throws \InvalidArgumentException When first argument is not a real stream
  39. *
  40. * @api
  41. */
  42. public function __construct($stream, $verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
  43. {
  44. if (!is_resource($stream) || 'stream' !== get_resource_type($stream)) {
  45. throw new \InvalidArgumentException('The StreamOutput class needs a stream as its first argument.');
  46. }
  47. $this->stream = $stream;
  48. if (null === $decorated) {
  49. $decorated = $this->hasColorSupport();
  50. }
  51. parent::__construct($verbosity, $decorated, $formatter);
  52. }
  53. /**
  54. * Gets the stream attached to this StreamOutput instance.
  55. *
  56. * @return resource A stream resource
  57. */
  58. public function getStream()
  59. {
  60. return $this->stream;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function doWrite($message, $newline)
  66. {
  67. if (false === @fwrite($this->stream, $message.($newline ? PHP_EOL : ''))) {
  68. // @codeCoverageIgnoreStart
  69. // should never happen
  70. throw new \RuntimeException('Unable to write output.');
  71. // @codeCoverageIgnoreEnd
  72. }
  73. fflush($this->stream);
  74. }
  75. /**
  76. * Returns true if the stream supports colorization.
  77. *
  78. * Colorization is disabled if not supported by the stream:
  79. *
  80. * - windows without ansicon and ConEmu
  81. * - non tty consoles
  82. *
  83. * @return Boolean true if the stream supports colorization, false otherwise
  84. */
  85. protected function hasColorSupport()
  86. {
  87. // @codeCoverageIgnoreStart
  88. if (DIRECTORY_SEPARATOR == '\\') {
  89. return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI');
  90. }
  91. return function_exists('posix_isatty') && @posix_isatty($this->stream);
  92. // @codeCoverageIgnoreEnd
  93. }
  94. }