You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MemoryInfo.php 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace OC;
  3. /**
  4. * Helper class that covers memory info.
  5. */
  6. class MemoryInfo {
  7. const RECOMMENDED_MEMORY_LIMIT = 512 * 1024 * 1024;
  8. /**
  9. * Tests if the memory limit is greater or equal the recommended value.
  10. *
  11. * @return bool
  12. */
  13. public function isMemoryLimitSufficient(): bool {
  14. $memoryLimit = $this->getMemoryLimit();
  15. return $memoryLimit === -1 || $memoryLimit >= self::RECOMMENDED_MEMORY_LIMIT;
  16. }
  17. /**
  18. * Returns the php memory limit.
  19. *
  20. * @return int The memory limit in bytes.
  21. */
  22. public function getMemoryLimit(): int {
  23. $iniValue = trim(ini_get('memory_limit'));
  24. if ($iniValue === '-1') {
  25. return -1;
  26. } else if (is_numeric($iniValue) === true) {
  27. return (int)$iniValue;
  28. } else {
  29. return $this->memoryLimitToBytes($iniValue);
  30. }
  31. }
  32. /**
  33. * Converts the ini memory limit to bytes.
  34. *
  35. * @param string $memoryLimit The "memory_limit" ini value
  36. * @return int
  37. */
  38. private function memoryLimitToBytes(string $memoryLimit): int {
  39. $last = strtolower(substr($memoryLimit, -1));
  40. $memoryLimit = (int)substr($memoryLimit, 0, -1);
  41. // intended fall trough
  42. switch($last) {
  43. case 'g':
  44. $memoryLimit *= 1024;
  45. case 'm':
  46. $memoryLimit *= 1024;
  47. case 'k':
  48. $memoryLimit *= 1024;
  49. }
  50. return $memoryLimit;
  51. }
  52. }