diff --git a/core/includes/common.inc b/core/includes/common.inc index ae62b90..ac2a4f9 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -16,6 +16,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Render\Markup; use Drupal\Core\Render\Renderer; +use Drupal\Core\StringTranslation\PluralTranslatableMarkup; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\PhpStorage\PhpStorageFactory; use Drupal\Core\StringTranslation\PluralTranslatableMarkup; @@ -254,7 +255,7 @@ function format_size($size, $langcode = NULL) { $args = ['@size' => $value['size']]; switch ($value['unit']) { case 'B': - return \Drupal::translation()->formatPlural($value['size'], '1 byte', '@count bytes', [], $options); + return new PluralTranslatableMarkup($value['size'], '1 byte', '@count bytes', [], $options); case 'KB': return new TranslatableMarkup('@size KB', $args, $options); case 'MB': diff --git a/core/lib/Drupal/Component/Utility/Environment.php b/core/lib/Drupal/Component/Utility/Environment.php index b10b97f..6ab61b4 100644 --- a/core/lib/Drupal/Component/Utility/Environment.php +++ b/core/lib/Drupal/Component/Utility/Environment.php @@ -10,8 +10,8 @@ /** * Provides PHP environment helper methods. * - * @deprecated in Drupal 8.1.x, will be removed before Drupal 9.0.0. - * Use \Drupal\Component\Utility\EnvironmentMemory. + * @deprecated in Drupal 8.1.x, will be removed before Drupal 9.0.0. Use + * \Drupal\Component\Utility\EnvironmentMemory instead. */ class Environment { diff --git a/core/lib/Drupal/Component/Utility/EnvironmentMemory.php b/core/lib/Drupal/Component/Utility/EnvironmentMemory.php index 03901fb..eef2806 100644 --- a/core/lib/Drupal/Component/Utility/EnvironmentMemory.php +++ b/core/lib/Drupal/Component/Utility/EnvironmentMemory.php @@ -13,9 +13,15 @@ class EnvironmentMemory { /** + * Constant representing unlimited memory. + */ + const UNLIMITED = -1; + + /** * Total memory, in bytes, available to PHP. * - * Will be set to -1 when no memory limit is set or it is set to unlimited. + * Will be set to static::UNLIMITED when no memory limit is set or it is set + * to unlimited. * * @var int */ @@ -25,8 +31,8 @@ class EnvironmentMemory { * Gets the total memory, in bytes, available to PHP. * * @return int - * The total memory, in bytes, available to PHP. Returns -1 when no memory - * limit is set or it is set to unlimited. + * The total memory, in bytes, available to PHP. Returns static::UNLIMITED + * when no memory limit is set or it is set to unlimited. */ public function getTotal() { if (!isset($this->total)) { @@ -38,19 +44,21 @@ public function getTotal() { /** * Sets the total memory, in bytes, available to PHP. * - * @param int|string $size + * @param int|string|null $size * (optional) The total memory available to PHP, expressed as a number of * bytes with optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, * 6GiB, 8bytes, 9mbytes). If no value is passed, the current PHP * memory_limit will be used. Defaults to NULL. * * @return $this + * + * @see https://en.wikipedia.org/wiki/Binary_prefix */ public function setTotal($size = NULL) { if (!$size) { $size = ini_get('memory_limit'); } - $this->total = (!$size || (int) $size === -1) ? -1 : Bytes::toInt($size); + $this->total = (!$size || (int) $size === -1) ? static::UNLIMITED : Bytes::toInt($size); } /** @@ -67,11 +75,11 @@ public function getUsed() { * Gets the amount of memory, in bytes, currently free to be used by PHP. * * @return int - * The amount of memory, in bytes, free for use by PHP. Returns -1 when no - * memory limit is set or it is set to unlimited. + * The amount of memory, in bytes, free for use by PHP. Returns + * static::UNLIMITED when no memory limit is set or it is set to unlimited. */ public function getFree() { - return $this->getTotal() === - 1 ? -1 : $this->getTotal() - $this->getUsed(); + return $this->getTotal() === static::UNLIMITED ? static::UNLIMITED : $this->getTotal() - $this->getUsed(); } /** @@ -85,14 +93,16 @@ public function getFree() { * @return bool * TRUE if there is sufficient memory to allow the requirements, or FALSE * otherwise. + * + * @see https://en.wikipedia.org/wiki/Binary_prefix */ public function checkLimit($required) { $memory_limit = $this->getTotal(); // There is sufficient memory if: - // - No memory limit is set, or set to unlimited (-1). + // - No memory limit is set, or set to unlimited (static::UNLIMITED). // - The memory limit is greater than or equal to the memory required for // the operation. - return $memory_limit === -1 || $memory_limit >= Bytes::toInt($required); + return $memory_limit === static::UNLIMITED || $memory_limit >= Bytes::toInt($required); } /** @@ -106,10 +116,12 @@ public function checkLimit($required) { * @return bool * TRUE if there is sufficient remaining memory to allow the requirements, * or FALSE otherwise. + * + * @see https://en.wikipedia.org/wiki/Binary_prefix */ public function checkAvailable($required) { $memory_limit = $this->getTotal(); - return $memory_limit === -1 || $this->getFree() >= Bytes::toInt($required); + return $memory_limit === static::UNLIMITED || $this->getFree() >= Bytes::toInt($required); } } diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php index 5cf4107..9dfa64c 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @@ -518,8 +518,10 @@ public function checkAvailableMemory($width, $height, $bits_per_pixel = 31, $twe * Get last memory check information. * * @return array[] - * An associative array with information about required, free and total - * memory. + * An associative array with the following keys: + * - 'required': information about required memory. + * - 'free': information about unused memory, free for PHP to allocate. + * - 'total': information about total memory available for PHP. */ public function getMemoryInfo() { return $this->memoryInfo; diff --git a/core/tests/Drupal/Tests/Component/Utility/EnvironmentMemoryTest.php b/core/tests/Drupal/Tests/Component/Utility/EnvironmentMemoryTest.php index 3579133..7f243a5 100644 --- a/core/tests/Drupal/Tests/Component/Utility/EnvironmentMemoryTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/EnvironmentMemoryTest.php @@ -28,7 +28,7 @@ class EnvironmentMemoryTest extends UnitTestCase { */ public function testGetTotalFromIni() { $limit = ini_get('memory_limit'); - $limit = (!$limit || $limit == -1) ? -1 : Bytes::toInt($limit); + $limit = (!$limit || $limit == -1) ? EnvironmentMemory::UNLIMITED : Bytes::toInt($limit); $memory = new EnvironmentMemory(); $this->assertEquals($limit, $memory->getTotal()); } @@ -82,7 +82,7 @@ public function providerTestCheckLimit() { // Minimal amount of memory should be available. ['30MB', NULL, TRUE], // Exceed a custom (unlimited) memory limit. - [$twice_avail_memory, -1, TRUE], + [$twice_avail_memory, EnvironmentMemory::UNLIMITED, TRUE], // Exceed a custom memory limit. ['30MB', '16MB', FALSE], // Available = required. @@ -98,7 +98,7 @@ public function providerTestCheckLimit() { */ public function testCheckAvailableCurrent() { $limit = ini_get('memory_limit'); - $limit = (!$limit || $limit == -1) ? -1 : Bytes::toInt($limit); + $limit = (!$limit || $limit == -1) ? EnvironmentMemory::UNLIMITED : Bytes::toInt($limit); if ($limit > 0) { $memory = new EnvironmentMemory(); $free_memory = $limit - memory_get_usage(TRUE);