annotate vendor/symfony/console/Output/StreamOutput.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
children 1fec387a4317
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 * Constructor.
Chris@0 37 *
Chris@0 38 * @param resource $stream A stream resource
Chris@0 39 * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
Chris@0 40 * @param bool|null $decorated Whether to decorate messages (null for auto-guessing)
Chris@0 41 * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
Chris@0 42 *
Chris@0 43 * @throws InvalidArgumentException When first argument is not a real stream
Chris@0 44 */
Chris@0 45 public function __construct($stream, $verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
Chris@0 46 {
Chris@0 47 if (!is_resource($stream) || 'stream' !== get_resource_type($stream)) {
Chris@0 48 throw new InvalidArgumentException('The StreamOutput class needs a stream as its first argument.');
Chris@0 49 }
Chris@0 50
Chris@0 51 $this->stream = $stream;
Chris@0 52
Chris@0 53 if (null === $decorated) {
Chris@0 54 $decorated = $this->hasColorSupport();
Chris@0 55 }
Chris@0 56
Chris@0 57 parent::__construct($verbosity, $decorated, $formatter);
Chris@0 58 }
Chris@0 59
Chris@0 60 /**
Chris@0 61 * Gets the stream attached to this StreamOutput instance.
Chris@0 62 *
Chris@0 63 * @return resource A stream resource
Chris@0 64 */
Chris@0 65 public function getStream()
Chris@0 66 {
Chris@0 67 return $this->stream;
Chris@0 68 }
Chris@0 69
Chris@0 70 /**
Chris@0 71 * {@inheritdoc}
Chris@0 72 */
Chris@0 73 protected function doWrite($message, $newline)
Chris@0 74 {
Chris@0 75 if (false === @fwrite($this->stream, $message) || ($newline && (false === @fwrite($this->stream, PHP_EOL)))) {
Chris@0 76 // should never happen
Chris@0 77 throw new RuntimeException('Unable to write output.');
Chris@0 78 }
Chris@0 79
Chris@0 80 fflush($this->stream);
Chris@0 81 }
Chris@0 82
Chris@0 83 /**
Chris@0 84 * Returns true if the stream supports colorization.
Chris@0 85 *
Chris@0 86 * Colorization is disabled if not supported by the stream:
Chris@0 87 *
Chris@0 88 * - Windows != 10.0.10586 without Ansicon, ConEmu or Mintty
Chris@0 89 * - non tty consoles
Chris@0 90 *
Chris@0 91 * @return bool true if the stream supports colorization, false otherwise
Chris@0 92 */
Chris@0 93 protected function hasColorSupport()
Chris@0 94 {
Chris@0 95 if (DIRECTORY_SEPARATOR === '\\') {
Chris@0 96 return
Chris@0 97 '10.0.10586' === PHP_WINDOWS_VERSION_MAJOR.'.'.PHP_WINDOWS_VERSION_MINOR.'.'.PHP_WINDOWS_VERSION_BUILD
Chris@0 98 || false !== getenv('ANSICON')
Chris@0 99 || 'ON' === getenv('ConEmuANSI')
Chris@0 100 || 'xterm' === getenv('TERM');
Chris@0 101 }
Chris@0 102
Chris@0 103 return function_exists('posix_isatty') && @posix_isatty($this->stream);
Chris@0 104 }
Chris@0 105 }