annotate vendor/symfony/console/Output/StreamOutput.php @ 16:c2387f117808

Routine composer update
author Chris Cannam
date Tue, 10 Jul 2018 15:07:59 +0100
parents 1fec387a4317
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Console\Output;
Chris@0 13
Chris@0 14 use Symfony\Component\Console\Exception\InvalidArgumentException;
Chris@0 15 use Symfony\Component\Console\Exception\RuntimeException;
Chris@0 16 use Symfony\Component\Console\Formatter\OutputFormatterInterface;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * StreamOutput writes the output to a given stream.
Chris@0 20 *
Chris@0 21 * Usage:
Chris@0 22 *
Chris@0 23 * $output = new StreamOutput(fopen('php://stdout', 'w'));
Chris@0 24 *
Chris@0 25 * As `StreamOutput` can use any stream, you can also use a file:
Chris@0 26 *
Chris@0 27 * $output = new StreamOutput(fopen('/path/to/output.log', 'a', false));
Chris@0 28 *
Chris@0 29 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 30 */
Chris@0 31 class StreamOutput extends Output
Chris@0 32 {
Chris@0 33 private $stream;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * @param resource $stream A stream resource
Chris@0 37 * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
Chris@0 38 * @param bool|null $decorated Whether to decorate messages (null for auto-guessing)
Chris@0 39 * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
Chris@0 40 *
Chris@0 41 * @throws InvalidArgumentException When first argument is not a real stream
Chris@0 42 */
Chris@0 43 public function __construct($stream, $verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
Chris@0 44 {
Chris@0 45 if (!is_resource($stream) || 'stream' !== get_resource_type($stream)) {
Chris@0 46 throw new InvalidArgumentException('The StreamOutput class needs a stream as its first argument.');
Chris@0 47 }
Chris@0 48
Chris@0 49 $this->stream = $stream;
Chris@0 50
Chris@0 51 if (null === $decorated) {
Chris@0 52 $decorated = $this->hasColorSupport();
Chris@0 53 }
Chris@0 54
Chris@0 55 parent::__construct($verbosity, $decorated, $formatter);
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * Gets the stream attached to this StreamOutput instance.
Chris@0 60 *
Chris@0 61 * @return resource A stream resource
Chris@0 62 */
Chris@0 63 public function getStream()
Chris@0 64 {
Chris@0 65 return $this->stream;
Chris@0 66 }
Chris@0 67
Chris@0 68 /**
Chris@0 69 * {@inheritdoc}
Chris@0 70 */
Chris@0 71 protected function doWrite($message, $newline)
Chris@0 72 {
Chris@0 73 if (false === @fwrite($this->stream, $message) || ($newline && (false === @fwrite($this->stream, PHP_EOL)))) {
Chris@0 74 // should never happen
Chris@0 75 throw new RuntimeException('Unable to write output.');
Chris@0 76 }
Chris@0 77
Chris@0 78 fflush($this->stream);
Chris@0 79 }
Chris@0 80
Chris@0 81 /**
Chris@0 82 * Returns true if the stream supports colorization.
Chris@0 83 *
Chris@0 84 * Colorization is disabled if not supported by the stream:
Chris@0 85 *
Chris@16 86 * This is tricky on Windows, because Cygwin, Msys2 etc emulate pseudo
Chris@16 87 * terminals via named pipes, so we can only check the environment.
Chris@16 88 *
Chris@16 89 * Reference: Composer\XdebugHandler\Process::supportsColor
Chris@16 90 * https://github.com/composer/xdebug-handler
Chris@0 91 *
Chris@0 92 * @return bool true if the stream supports colorization, false otherwise
Chris@0 93 */
Chris@0 94 protected function hasColorSupport()
Chris@0 95 {
Chris@0 96 if (DIRECTORY_SEPARATOR === '\\') {
Chris@16 97 return (function_exists('sapi_windows_vt100_support')
Chris@16 98 && @sapi_windows_vt100_support($this->stream))
Chris@0 99 || false !== getenv('ANSICON')
Chris@0 100 || 'ON' === getenv('ConEmuANSI')
Chris@0 101 || 'xterm' === getenv('TERM');
Chris@0 102 }
Chris@0 103
Chris@16 104 if (function_exists('stream_isatty')) {
Chris@16 105 return @stream_isatty($this->stream);
Chris@16 106 }
Chris@16 107
Chris@16 108 if (function_exists('posix_isatty')) {
Chris@16 109 return @posix_isatty($this->stream);
Chris@16 110 }
Chris@16 111
Chris@16 112 $stat = @fstat($this->stream);
Chris@16 113 // Check if formatted mode is S_IFCHR
Chris@16 114 return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
Chris@0 115 }
Chris@0 116 }