[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/console/Helper/ -> Helper.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   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\Helper;
  13  
  14  use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  15  
  16  /**
  17   * Helper is the base class for all helper classes.
  18   *
  19   * @author Fabien Potencier <fabien@symfony.com>
  20   */
  21  abstract class Helper implements HelperInterface
  22  {
  23      protected $helperSet = null;
  24  
  25      /**
  26       * {@inheritdoc}
  27       */
  28      public function setHelperSet(HelperSet $helperSet = null)
  29      {
  30          $this->helperSet = $helperSet;
  31      }
  32  
  33      /**
  34       * {@inheritdoc}
  35       */
  36      public function getHelperSet()
  37      {
  38          return $this->helperSet;
  39      }
  40  
  41      /**
  42       * Returns the length of a string, using mb_strwidth if it is available.
  43       *
  44       * @param string $string The string to check its length
  45       *
  46       * @return int The length of the string
  47       */
  48      public static function strlen($string)
  49      {
  50          if (false === $encoding = mb_detect_encoding($string, null, true)) {
  51              return \strlen($string);
  52          }
  53  
  54          return mb_strwidth($string, $encoding);
  55      }
  56  
  57      public static function formatTime($secs)
  58      {
  59          static $timeFormats = array(
  60              array(0, '< 1 sec'),
  61              array(1, '1 sec'),
  62              array(2, 'secs', 1),
  63              array(60, '1 min'),
  64              array(120, 'mins', 60),
  65              array(3600, '1 hr'),
  66              array(7200, 'hrs', 3600),
  67              array(86400, '1 day'),
  68              array(172800, 'days', 86400),
  69          );
  70  
  71          foreach ($timeFormats as $index => $format) {
  72              if ($secs >= $format[0]) {
  73                  if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0])
  74                      || $index == \count($timeFormats) - 1
  75                  ) {
  76                      if (2 == \count($format)) {
  77                          return $format[1];
  78                      }
  79  
  80                      return floor($secs / $format[2]).' '.$format[1];
  81                  }
  82              }
  83          }
  84      }
  85  
  86      public static function formatMemory($memory)
  87      {
  88          if ($memory >= 1024 * 1024 * 1024) {
  89              return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024);
  90          }
  91  
  92          if ($memory >= 1024 * 1024) {
  93              return sprintf('%.1f MiB', $memory / 1024 / 1024);
  94          }
  95  
  96          if ($memory >= 1024) {
  97              return sprintf('%d KiB', $memory / 1024);
  98          }
  99  
 100          return sprintf('%d B', $memory);
 101      }
 102  
 103      public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, $string)
 104      {
 105          return self::strlen(self::removeDecoration($formatter, $string));
 106      }
 107  
 108      public static function removeDecoration(OutputFormatterInterface $formatter, $string)
 109      {
 110          $isDecorated = $formatter->isDecorated();
 111          $formatter->setDecorated(false);
 112          // remove <...> formatting
 113          $string = $formatter->format($string);
 114          // remove already formatted characters
 115          $string = preg_replace("/\033\[[^m]*m/", '', $string);
 116          $formatter->setDecorated($isDecorated);
 117  
 118          return $string;
 119      }
 120  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1