annotate core/lib/Drupal/Component/Utility/Environment.php @ 17:129ea1e6d783

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