4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Output;
14 use Symfony\Component\Console\Exception\InvalidArgumentException;
15 use Symfony\Component\Console\Exception\RuntimeException;
16 use Symfony\Component\Console\Formatter\OutputFormatterInterface;
19 * StreamOutput writes the output to a given stream.
23 * $output = new StreamOutput(fopen('php://stdout', 'w'));
25 * As `StreamOutput` can use any stream, you can also use a file:
27 * $output = new StreamOutput(fopen('/path/to/output.log', 'a', false));
29 * @author Fabien Potencier <fabien@symfony.com>
31 class StreamOutput extends Output
36 * @param resource $stream A stream resource
37 * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
38 * @param bool|null $decorated Whether to decorate messages (null for auto-guessing)
39 * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
41 * @throws InvalidArgumentException When first argument is not a real stream
43 public function __construct($stream, $verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
45 if (!\is_resource($stream) || 'stream' !== get_resource_type($stream)) {
46 throw new InvalidArgumentException('The StreamOutput class needs a stream as its first argument.');
49 $this->stream = $stream;
51 if (null === $decorated) {
52 $decorated = $this->hasColorSupport();
55 parent::__construct($verbosity, $decorated, $formatter);
59 * Gets the stream attached to this StreamOutput instance.
61 * @return resource A stream resource
63 public function getStream()
71 protected function doWrite($message, $newline)
77 if (false === @fwrite($this->stream, $message)) {
78 // should never happen
79 throw new RuntimeException('Unable to write output.');
82 fflush($this->stream);
86 * Returns true if the stream supports colorization.
88 * Colorization is disabled if not supported by the stream:
90 * This is tricky on Windows, because Cygwin, Msys2 etc emulate pseudo
91 * terminals via named pipes, so we can only check the environment.
93 * Reference: Composer\XdebugHandler\Process::supportsColor
94 * https://github.com/composer/xdebug-handler
96 * @return bool true if the stream supports colorization, false otherwise
98 protected function hasColorSupport()
100 if ('Hyper' === getenv('TERM_PROGRAM')) {
104 if (\DIRECTORY_SEPARATOR === '\\') {
105 return (\function_exists('sapi_windows_vt100_support')
106 && @sapi_windows_vt100_support($this->stream))
107 || false !== getenv('ANSICON')
108 || 'ON' === getenv('ConEmuANSI')
109 || 'xterm' === getenv('TERM');
112 if (\function_exists('stream_isatty')) {
113 return @stream_isatty($this->stream);
116 if (\function_exists('posix_isatty')) {
117 return @posix_isatty($this->stream);
120 $stat = @fstat($this->stream);
121 // Check if formatted mode is S_IFCHR
122 return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;