Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Console\Output; Chris@0: Chris@0: use Symfony\Component\Console\Exception\InvalidArgumentException; Chris@0: use Symfony\Component\Console\Exception\RuntimeException; Chris@0: use Symfony\Component\Console\Formatter\OutputFormatterInterface; Chris@0: Chris@0: /** Chris@0: * StreamOutput writes the output to a given stream. Chris@0: * Chris@0: * Usage: Chris@0: * Chris@17: * $output = new StreamOutput(fopen('php://stdout', 'w')); Chris@0: * Chris@0: * As `StreamOutput` can use any stream, you can also use a file: Chris@0: * Chris@17: * $output = new StreamOutput(fopen('/path/to/output.log', 'a', false)); Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class StreamOutput extends Output Chris@0: { Chris@0: private $stream; Chris@0: Chris@0: /** Chris@0: * @param resource $stream A stream resource Chris@0: * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface) Chris@0: * @param bool|null $decorated Whether to decorate messages (null for auto-guessing) Chris@0: * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter) Chris@0: * Chris@0: * @throws InvalidArgumentException When first argument is not a real stream Chris@0: */ Chris@0: public function __construct($stream, $verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null) Chris@0: { Chris@17: if (!\is_resource($stream) || 'stream' !== get_resource_type($stream)) { Chris@0: throw new InvalidArgumentException('The StreamOutput class needs a stream as its first argument.'); Chris@0: } Chris@0: Chris@0: $this->stream = $stream; Chris@0: Chris@0: if (null === $decorated) { Chris@0: $decorated = $this->hasColorSupport(); Chris@0: } Chris@0: Chris@0: parent::__construct($verbosity, $decorated, $formatter); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the stream attached to this StreamOutput instance. Chris@0: * Chris@0: * @return resource A stream resource Chris@0: */ Chris@0: public function getStream() Chris@0: { Chris@0: return $this->stream; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doWrite($message, $newline) Chris@0: { Chris@17: if ($newline) { Chris@17: $message .= PHP_EOL; Chris@17: } Chris@17: Chris@17: if (false === @fwrite($this->stream, $message)) { Chris@0: // should never happen Chris@0: throw new RuntimeException('Unable to write output.'); Chris@0: } Chris@0: Chris@0: fflush($this->stream); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true if the stream supports colorization. Chris@0: * Chris@0: * Colorization is disabled if not supported by the stream: Chris@0: * Chris@16: * This is tricky on Windows, because Cygwin, Msys2 etc emulate pseudo Chris@16: * terminals via named pipes, so we can only check the environment. Chris@16: * Chris@16: * Reference: Composer\XdebugHandler\Process::supportsColor Chris@16: * https://github.com/composer/xdebug-handler Chris@0: * Chris@0: * @return bool true if the stream supports colorization, false otherwise Chris@0: */ Chris@0: protected function hasColorSupport() Chris@0: { Chris@17: if ('Hyper' === getenv('TERM_PROGRAM')) { Chris@17: return true; Chris@17: } Chris@17: Chris@17: if (\DIRECTORY_SEPARATOR === '\\') { Chris@17: return (\function_exists('sapi_windows_vt100_support') Chris@16: && @sapi_windows_vt100_support($this->stream)) Chris@0: || false !== getenv('ANSICON') Chris@0: || 'ON' === getenv('ConEmuANSI') Chris@0: || 'xterm' === getenv('TERM'); Chris@0: } Chris@0: Chris@17: if (\function_exists('stream_isatty')) { Chris@16: return @stream_isatty($this->stream); Chris@16: } Chris@16: Chris@17: if (\function_exists('posix_isatty')) { Chris@16: return @posix_isatty($this->stream); Chris@16: } Chris@16: Chris@16: $stat = @fstat($this->stream); Chris@16: // Check if formatted mode is S_IFCHR Chris@16: return $stat ? 0020000 === ($stat['mode'] & 0170000) : false; Chris@0: } Chris@0: }