annotate vendor/symfony/console/Output/StreamOutput.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
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@17 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@17 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@17 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@17 73 if ($newline) {
Chris@17 74 $message .= PHP_EOL;
Chris@17 75 }
Chris@17 76
Chris@17 77 if (false === @fwrite($this->stream, $message)) {
Chris@0 78 // should never happen
Chris@0 79 throw new RuntimeException('Unable to write output.');
Chris@0 80 }
Chris@0 81
Chris@0 82 fflush($this->stream);
Chris@0 83 }
Chris@0 84
Chris@0 85 /**
Chris@0 86 * Returns true if the stream supports colorization.
Chris@0 87 *
Chris@0 88 * Colorization is disabled if not supported by the stream:
Chris@0 89 *
Chris@16 90 * This is tricky on Windows, because Cygwin, Msys2 etc emulate pseudo
Chris@16 91 * terminals via named pipes, so we can only check the environment.
Chris@16 92 *
Chris@16 93 * Reference: Composer\XdebugHandler\Process::supportsColor
Chris@16 94 * https://github.com/composer/xdebug-handler
Chris@0 95 *
Chris@0 96 * @return bool true if the stream supports colorization, false otherwise
Chris@0 97 */
Chris@0 98 protected function hasColorSupport()
Chris@0 99 {
Chris@17 100 if ('Hyper' === getenv('TERM_PROGRAM')) {
Chris@17 101 return true;
Chris@17 102 }
Chris@17 103
Chris@17 104 if (\DIRECTORY_SEPARATOR === '\\') {
Chris@17 105 return (\function_exists('sapi_windows_vt100_support')
Chris@16 106 && @sapi_windows_vt100_support($this->stream))
Chris@0 107 || false !== getenv('ANSICON')
Chris@0 108 || 'ON' === getenv('ConEmuANSI')
Chris@0 109 || 'xterm' === getenv('TERM');
Chris@0 110 }
Chris@0 111
Chris@17 112 if (\function_exists('stream_isatty')) {
Chris@16 113 return @stream_isatty($this->stream);
Chris@16 114 }
Chris@16 115
Chris@17 116 if (\function_exists('posix_isatty')) {
Chris@16 117 return @posix_isatty($this->stream);
Chris@16 118 }
Chris@16 119
Chris@16 120 $stat = @fstat($this->stream);
Chris@16 121 // Check if formatted mode is S_IFCHR
Chris@16 122 return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
Chris@0 123 }
Chris@0 124 }