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@0: * $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@0: * $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: * Constructor. 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@0: 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@0: if (false === @fwrite($this->stream, $message) || ($newline && (false === @fwrite($this->stream, PHP_EOL)))) { 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@0: * - Windows != 10.0.10586 without Ansicon, ConEmu or Mintty Chris@0: * - non tty consoles Chris@0: * Chris@0: * @return bool true if the stream supports colorization, false otherwise Chris@0: */ Chris@0: protected function hasColorSupport() Chris@0: { Chris@0: if (DIRECTORY_SEPARATOR === '\\') { Chris@0: return Chris@0: '10.0.10586' === PHP_WINDOWS_VERSION_MAJOR.'.'.PHP_WINDOWS_VERSION_MINOR.'.'.PHP_WINDOWS_VERSION_BUILD Chris@0: || false !== getenv('ANSICON') Chris@0: || 'ON' === getenv('ConEmuANSI') Chris@0: || 'xterm' === getenv('TERM'); Chris@0: } Chris@0: Chris@0: return function_exists('posix_isatty') && @posix_isatty($this->stream); Chris@0: } Chris@0: }