StreamOutput.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Symfony\Component\Console\Output;
  3. /*
  4. * This file is part of the Symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * StreamOutput writes the output to a given stream.
  13. *
  14. * Usage:
  15. *
  16. * $output = new StreamOutput(fopen('php://stdout', 'w'));
  17. *
  18. * As `StreamOutput` can use any stream, you can also use a file:
  19. *
  20. * $output = new StreamOutput(fopen('/path/to/output.log', 'a', false));
  21. *
  22. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  23. */
  24. class StreamOutput extends Output
  25. {
  26. protected $stream;
  27. /**
  28. * Constructor.
  29. *
  30. * @param mixed $stream A stream resource
  31. * @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL, self::VERBOSITY_VERBOSE)
  32. * @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing)
  33. *
  34. * @throws \InvalidArgumentException When first argument is not a real stream
  35. */
  36. public function __construct($stream, $verbosity = self::VERBOSITY_NORMAL, $decorated = null)
  37. {
  38. if (!is_resource($stream) || 'stream' !== get_resource_type($stream)) {
  39. throw new \InvalidArgumentException('The StreamOutput class needs a stream as its first argument.');
  40. }
  41. $this->stream = $stream;
  42. if (null === $decorated) {
  43. $decorated = $this->hasColorSupport($decorated);
  44. }
  45. parent::__construct($verbosity, $decorated);
  46. }
  47. /**
  48. * Gets the stream attached to this StreamOutput instance.
  49. *
  50. * @return resource A stream resource
  51. */
  52. public function getStream()
  53. {
  54. return $this->stream;
  55. }
  56. /**
  57. * Writes a message to the output.
  58. *
  59. * @param string $message A message to write to the output
  60. * @param Boolean $newline Whether to add a newline or not
  61. *
  62. * @throws \RuntimeException When unable to write output (should never happen)
  63. */
  64. public function doWrite($message, $newline)
  65. {
  66. if (false === @fwrite($this->stream, $message.($newline ? PHP_EOL : ''))) {
  67. // @codeCoverageIgnoreStart
  68. // should never happen
  69. throw new \RuntimeException('Unable to write output.');
  70. // @codeCoverageIgnoreEnd
  71. }
  72. flush();
  73. }
  74. /**
  75. * Returns true if the stream supports colorization.
  76. *
  77. * Colorization is disabled if not supported by the stream:
  78. *
  79. * - windows without ansicon
  80. * - non tty consoles
  81. *
  82. * @return Boolean true if the stream supports colorization, false otherwise
  83. */
  84. protected function hasColorSupport()
  85. {
  86. // @codeCoverageIgnoreStart
  87. if (DIRECTORY_SEPARATOR == '\\') {
  88. return false !== getenv('ANSICON');
  89. } else {
  90. return function_exists('posix_isatty') && @posix_isatty($this->stream);
  91. }
  92. // @codeCoverageIgnoreEnd
  93. }
  94. }