Environment.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace Drupal\Component\Utility;
  3. /**
  4. * Provides PHP environment helper methods.
  5. */
  6. class Environment {
  7. /**
  8. * Compares the memory required for an operation to the available memory.
  9. *
  10. * @param string $required
  11. * The memory required for the operation, expressed as a number of bytes with
  12. * optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8bytes,
  13. * 9mbytes).
  14. * @param $memory_limit
  15. * (optional) The memory limit for the operation, expressed as a number of
  16. * bytes with optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G,
  17. * 6GiB, 8bytes, 9mbytes). If no value is passed, the current PHP
  18. * memory_limit will be used. Defaults to NULL.
  19. *
  20. * @return bool
  21. * TRUE if there is sufficient memory to allow the operation, or FALSE
  22. * otherwise.
  23. */
  24. public static function checkMemoryLimit($required, $memory_limit = NULL) {
  25. if (!isset($memory_limit)) {
  26. $memory_limit = ini_get('memory_limit');
  27. }
  28. // There is sufficient memory if:
  29. // - No memory limit is set.
  30. // - The memory limit is set to unlimited (-1).
  31. // - The memory limit is greater than or equal to the memory required for
  32. // the operation.
  33. return ((!$memory_limit) || ($memory_limit == -1) || (Bytes::toInt($memory_limit) >= Bytes::toInt($required)));
  34. }
  35. }