StreamOutput.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Symfony\Components\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. * @package symfony
  23. * @subpackage console
  24. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  25. */
  26. class StreamOutput extends Output
  27. {
  28. protected $stream;
  29. /**
  30. * Constructor.
  31. *
  32. * @param mixed $stream A stream resource
  33. * @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL, self::VERBOSITY_VERBOSE)
  34. * @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing)
  35. */
  36. public function __construct($stream, $verbosity = self::VERBOSITY_NORMAL, $decorated = null)
  37. {
  38. if (!is_resource($stream) || 'stream' !== get_resource_type($stream))
  39. {
  40. throw new \InvalidArgumentException('The StreamOutput class needs a stream as its first argument.');
  41. }
  42. $this->stream = $stream;
  43. if (null === $decorated)
  44. {
  45. $decorated = $this->hasColorSupport($decorated);
  46. }
  47. parent::__construct($verbosity, $decorated);
  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. * Writes a message to the output.
  60. *
  61. * @param string $message A message to write to the output
  62. */
  63. public function doWrite($message)
  64. {
  65. if (false === @fwrite($this->stream, $message.PHP_EOL))
  66. {
  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. {
  89. return false !== getenv('ANSICON');
  90. }
  91. else
  92. {
  93. return function_exists('posix_isatty') && @posix_isatty($this->stream);
  94. }
  95. // @codeCoverageIgnoreEnd
  96. }
  97. }