[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/symfony/console/Output/ -> StreamOutput.php (source)

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


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer