Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace OpenSpout\Reader\XLSX\Manager\SharedStringsCaching;
   6  
   7  /**
   8   * @internal
   9   */
  10  final class MemoryLimit
  11  {
  12      private string $memoryLimit;
  13  
  14      public function __construct(string $memoryLimit)
  15      {
  16          $this->memoryLimit = $memoryLimit;
  17      }
  18  
  19      /**
  20       * Returns the PHP "memory_limit" in Kilobytes.
  21       */
  22      public function getMemoryLimitInKB(): float
  23      {
  24          $memoryLimitFormatted = strtolower(trim($this->memoryLimit));
  25  
  26          // No memory limit
  27          if ('-1' === $memoryLimitFormatted) {
  28              return -1;
  29          }
  30  
  31          if (1 === preg_match('/(\d+)([bkmgt])b?/', $memoryLimitFormatted, $matches)) {
  32              $amount = (int) $matches[1];
  33              $unit = $matches[2];
  34  
  35              switch ($unit) {
  36                  case 'b': return $amount / 1024;
  37  
  38                  case 'k': return $amount;
  39  
  40                  case 'm': return $amount * 1024;
  41  
  42                  case 'g': return $amount * 1024 * 1024;
  43  
  44                  case 't': return $amount * 1024 * 1024 * 1024;
  45              }
  46          }
  47  
  48          return -1;
  49      }
  50  }