StreamOutput.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL,
  35. * self::VERBOSITY_VERBOSE)
  36. * @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing)
  37. * @param OutputFormatter $formatter Output formatter instance
  38. *
  39. * @throws \InvalidArgumentException When first argument is not a real stream
  40. *
  41. * @api
  42. */
  43. public function __construct($stream, $verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
  44. {
  45. if (!is_resource($stream) || 'stream' !== get_resource_type($stream)) {
  46. throw new \InvalidArgumentException('The StreamOutput class needs a stream as its first argument.');
  47. }
  48. $this->stream = $stream;
  49. if (null === $decorated) {
  50. $decorated = $this->hasColorSupport($decorated);
  51. }
  52. parent::__construct($verbosity, $decorated, $formatter);
  53. }
  54. /**
  55. * Gets the stream attached to this StreamOutput instance.
  56. *
  57. * @return resource A stream resource
  58. */
  59. public function getStream()
  60. {
  61. return $this->stream;
  62. }
  63. /**
  64. * Writes a message to the output.
  65. *
  66. * @param string $message A message to write to the output
  67. * @param Boolean $newline Whether to add a newline or not
  68. *
  69. * @throws \RuntimeException When unable to write output (should never happen)
  70. */
  71. public function doWrite($message, $newline)
  72. {
  73. if (false === @fwrite($this->stream, $message.($newline ? PHP_EOL : ''))) {
  74. // @codeCoverageIgnoreStart
  75. // should never happen
  76. throw new \RuntimeException('Unable to write output.');
  77. // @codeCoverageIgnoreEnd
  78. }
  79. fflush($this->stream);
  80. }
  81. /**
  82. * Returns true if the stream supports colorization.
  83. *
  84. * Colorization is disabled if not supported by the stream:
  85. *
  86. * - windows without ansicon
  87. * - non tty consoles
  88. *
  89. * @return Boolean true if the stream supports colorization, false otherwise
  90. */
  91. protected function hasColorSupport()
  92. {
  93. // @codeCoverageIgnoreStart
  94. if (DIRECTORY_SEPARATOR == '\\') {
  95. return false !== getenv('ANSICON');
  96. }
  97. return function_exists('posix_isatty') && @posix_isatty($this->stream);
  98. // @codeCoverageIgnoreEnd
  99. }
  100. }