[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/symfony/console/ -> Terminal.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;
  13  
  14  class Terminal
  15  {
  16      private static $width;
  17      private static $height;
  18      private static $stty;
  19  
  20      /**
  21       * Gets the terminal width.
  22       *
  23       * @return int
  24       */
  25      public function getWidth()
  26      {
  27          $width = getenv('COLUMNS');
  28          if (false !== $width) {
  29              return (int) trim($width);
  30          }
  31  
  32          if (null === self::$width) {
  33              self::initDimensions();
  34          }
  35  
  36          return self::$width ?: 80;
  37      }
  38  
  39      /**
  40       * Gets the terminal height.
  41       *
  42       * @return int
  43       */
  44      public function getHeight()
  45      {
  46          $height = getenv('LINES');
  47          if (false !== $height) {
  48              return (int) trim($height);
  49          }
  50  
  51          if (null === self::$height) {
  52              self::initDimensions();
  53          }
  54  
  55          return self::$height ?: 50;
  56      }
  57  
  58      /**
  59       * @internal
  60       */
  61      public static function hasSttyAvailable(): bool
  62      {
  63          if (null !== self::$stty) {
  64              return self::$stty;
  65          }
  66  
  67          // skip check if exec function is disabled
  68          if (!\function_exists('exec')) {
  69              return false;
  70          }
  71  
  72          exec('stty 2>&1', $output, $exitcode);
  73  
  74          return self::$stty = 0 === $exitcode;
  75      }
  76  
  77      private static function initDimensions()
  78      {
  79          if ('\\' === \DIRECTORY_SEPARATOR) {
  80              if (preg_match('/^(\d+)x(\d+)(?: \((\d+)x(\d+)\))?$/', trim(getenv('ANSICON')), $matches)) {
  81                  // extract [w, H] from "wxh (WxH)"
  82                  // or [w, h] from "wxh"
  83                  self::$width = (int) $matches[1];
  84                  self::$height = isset($matches[4]) ? (int) $matches[4] : (int) $matches[2];
  85              } elseif (!self::hasVt100Support() && self::hasSttyAvailable()) {
  86                  // only use stty on Windows if the terminal does not support vt100 (e.g. Windows 7 + git-bash)
  87                  // testing for stty in a Windows 10 vt100-enabled console will implicitly disable vt100 support on STDOUT
  88                  self::initDimensionsUsingStty();
  89              } elseif (null !== $dimensions = self::getConsoleMode()) {
  90                  // extract [w, h] from "wxh"
  91                  self::$width = (int) $dimensions[0];
  92                  self::$height = (int) $dimensions[1];
  93              }
  94          } else {
  95              self::initDimensionsUsingStty();
  96          }
  97      }
  98  
  99      /**
 100       * Returns whether STDOUT has vt100 support (some Windows 10+ configurations).
 101       */
 102      private static function hasVt100Support(): bool
 103      {
 104          return \function_exists('sapi_windows_vt100_support') && sapi_windows_vt100_support(fopen('php://stdout', 'w'));
 105      }
 106  
 107      /**
 108       * Initializes dimensions using the output of an stty columns line.
 109       */
 110      private static function initDimensionsUsingStty()
 111      {
 112          if ($sttyString = self::getSttyColumns()) {
 113              if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
 114                  // extract [w, h] from "rows h; columns w;"
 115                  self::$width = (int) $matches[2];
 116                  self::$height = (int) $matches[1];
 117              } elseif (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
 118                  // extract [w, h] from "; h rows; w columns"
 119                  self::$width = (int) $matches[2];
 120                  self::$height = (int) $matches[1];
 121              }
 122          }
 123      }
 124  
 125      /**
 126       * Runs and parses mode CON if it's available, suppressing any error output.
 127       *
 128       * @return int[]|null An array composed of the width and the height or null if it could not be parsed
 129       */
 130      private static function getConsoleMode(): ?array
 131      {
 132          $info = self::readFromProcess('mode CON');
 133  
 134          if (null === $info || !preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
 135              return null;
 136          }
 137  
 138          return [(int) $matches[2], (int) $matches[1]];
 139      }
 140  
 141      /**
 142       * Runs and parses stty -a if it's available, suppressing any error output.
 143       */
 144      private static function getSttyColumns(): ?string
 145      {
 146          return self::readFromProcess('stty -a | grep columns');
 147      }
 148  
 149      private static function readFromProcess(string $command): ?string
 150      {
 151          if (!\function_exists('proc_open')) {
 152              return null;
 153          }
 154  
 155          $descriptorspec = [
 156              1 => ['pipe', 'w'],
 157              2 => ['pipe', 'w'],
 158          ];
 159  
 160          $process = proc_open($command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
 161          if (!\is_resource($process)) {
 162              return null;
 163          }
 164  
 165          $info = stream_get_contents($pipes[1]);
 166          fclose($pipes[1]);
 167          fclose($pipes[2]);
 168          proc_close($process);
 169  
 170          return $info;
 171      }
 172  }


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