diff --git a/core/includes/common.inc b/core/includes/common.inc index 65dfd95..f6f6f07 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -250,11 +250,12 @@ function check_url($uri) { */ function format_size($size, $langcode = NULL) { $options = ['langcode' => $langcode]; + /** @var \Drupal\Component\Utility\BytesUnitAndSize $value */ $value = Bytes::toUnitAndSize($size); - $args = ['@size' => $value['size']]; - switch ($value['unit']) { + $args = ['@size' => $value->getSize()]; + switch ($value->getUnit()) { case 'B': - return new PluralTranslatableMarkup($value['size'], '1 byte', '@count bytes', [], $options); + return new PluralTranslatableMarkup($value->getSize(), '1 byte', '@count bytes', [], $options); case 'KB': return new TranslatableMarkup('@size KB', $args, $options); case 'MB': diff --git a/core/lib/Drupal/Component/Utility/Bytes.php b/core/lib/Drupal/Component/Utility/Bytes.php index 1b7d8c6..2a5ea86 100644 --- a/core/lib/Drupal/Component/Utility/Bytes.php +++ b/core/lib/Drupal/Component/Utility/Bytes.php @@ -55,17 +55,15 @@ public static function toInt($size) { * @param float $size * A quantity of bytes. * - * @return array - * An associative array with the following keys: - * - 'unit': unit of measurement (string) - * - 'size': size (float, rounded to 2 decimals). - * An empty array if the input value is negative. + * @return \Drupal\Component\Utility\BytesUnitAndSize|null + * NULL if the input value is negative, a + * \Drupal\Component\Utility\BytesUnitAndSize value object otherwise. * * @see format_size */ public static function toUnitAndSize($size) { if ($size < 0) { - return []; + return NULL; } foreach (['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] as $unit) { if (round($size, 2) < self::KILOBYTE) { @@ -73,7 +71,7 @@ public static function toUnitAndSize($size) { } $size = $size / self::KILOBYTE; } - return ['unit' => $unit, 'size' => round($size, 2)]; + return new BytesUnitAndSize(round($size, 2), $unit); } } diff --git a/core/lib/Drupal/Component/Utility/BytesUnitAndSize.php b/core/lib/Drupal/Component/Utility/BytesUnitAndSize.php new file mode 100644 index 0000000..570c82f --- /dev/null +++ b/core/lib/Drupal/Component/Utility/BytesUnitAndSize.php @@ -0,0 +1,68 @@ +size = $size; + $this->unit = $unit; + } + + /** + * Gets the bytes quantity. + * + * @return int + * The bytes quantity. + */ + public function getSize() { + return $this->size; + } + + /** + * Gets the unit of measurement of this bytes quantity. + * + * @return string + * The unit of measurement of this bytes quantity. + */ + public function getUnit() { + return $this->unit; + } + +} diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php index 99817d1..b2813b9 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @@ -192,12 +192,12 @@ protected function load() { // invalid, to avoid useless attempts to manipulate it. $this->preLoadInfo = NULL; $this->logger->error("Not enough memory (required: %req_size %req_unit, free: %free_size %free_unit, total: %tot_size %tot_unit) to load image '%file'", [ - '%req_size' => $memory['required']['size'], - '%req_unit' => $memory['required']['unit'], - '%free_size' => $memory['free']['size'], - '%free_unit' => $memory['free']['unit'], - '%tot_size' => $memory['total']['size'], - '%tot_unit' => $memory['total']['unit'], + '%req_size' => $memory['required']->getSize(), + '%req_unit' => $memory['required']->getUnit(), + '%free_size' => $memory['free']->getSize(), + '%free_unit' => $memory['free']->getUnit(), + '%tot_size' => $memory['total']->getSize(), + '%tot_unit' => $memory['total']->getUnit(), '%file' => $this->getSource() ]); return FALSE; @@ -481,9 +481,9 @@ protected static function supportedTypes() { * http://php.net/manual/en/function.imagecreatetruecolor.php#99623. * Defaults to 1.7. * - * @return TRUE|array[] + * @return TRUE|\Drupal\Component\Utility\BytesUnitAndSize[] * TRUE if the memory is available. If not enough memory is available, an - * associative array with the following keys: + * associative array of BytesUnitAndSize objects 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. diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php index d394c2b..24d31a4 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php @@ -87,7 +87,7 @@ protected function execute(array $arguments) { // Check if enough memory is available. $memory = $this->getToolkit()->isMemoryAvailable($arguments['width'], $arguments['height']); if ($memory !== TRUE) { - throw new \RuntimeException("Not enough memory (required: {$memory['required']['size']} {$memory['required']['unit']}, free: {$memory['free']['size']} {$memory['free']['unit']}, total: {$memory['total']['size']} {$memory['total']['unit']}) to perform the image '{$this->getPluginId()}' operation"); + throw new \RuntimeException("Not enough memory (required: {$memory['required']->getSize()} {$memory['required']->getUnit()}, free: {$memory['free']->getSize()} {$memory['free']->getUnit()}, total: {$memory['total']->getSize()} {$memory['total']->getUnit()}) to perform the image '{$this->getPluginId()}' operation"); } // Get the image type. diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php index e27c705..6e84c82 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php @@ -104,7 +104,7 @@ protected function execute(array $arguments) { $rect = $rect->rotate($arguments['degrees']); $memory = $this->getToolkit()->isMemoryAvailable($rect->getBoundingWidth(), $rect->getBoundingHeight()); if ($memory !== TRUE) { - throw new \RuntimeException("Not enough memory (required: {$memory['required']['size']} {$memory['required']['unit']}, free: {$memory['free']['size']} {$memory['free']['unit']}, total: {$memory['total']['size']} {$memory['total']['unit']}) to perform the image '{$this->getPluginId()}' operation"); + throw new \RuntimeException("Not enough memory (required: {$memory['required']->getSize()} {$memory['required']->getUnit()}, free: {$memory['free']->getSize()} {$memory['free']->getUnit()}, total: {$memory['total']->getSize()} {$memory['total']->getUnit()}) to perform the image '{$this->getPluginId()}' operation"); } // Stores the original GD resource. diff --git a/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php b/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php index d97657b..d117f0f 100644 --- a/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php +++ b/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\system\Unit\Plugin\ImageToolkit; +use Drupal\Component\Utility\BytesUnitAndSize; use Drupal\Core\Image\Image; use Drupal\Core\ImageToolkit\ImageToolkitInterface; use Drupal\Tests\UnitTestCase; @@ -53,9 +54,9 @@ class GdToolkitTest extends UnitTestCase { * @var array */ protected $availableMemoryFalse = [ - 'free' => ['unit' => 'MB', 'size' => 10], - 'required' => ['unit' => 'MB', 'size' => 20], - 'total' => ['unit' => 'MB', 'size' => 20], + 'free' => new BytesUnitAndSize(10, 'MB'), + 'required' => new BytesUnitAndSize(20, 'MB'), + 'total' => new BytesUnitAndSize(20, 'MB'), ]; /** diff --git a/core/tests/Drupal/Tests/Component/Utility/BytesTest.php b/core/tests/Drupal/Tests/Component/Utility/BytesTest.php index 347bff2..19a37f7 100644 --- a/core/tests/Drupal/Tests/Component/Utility/BytesTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/BytesTest.php @@ -8,6 +8,7 @@ namespace Drupal\Tests\Component\Utility; use Drupal\Component\Utility\Bytes; +use Drupal\Component\Utility\BytesUnitAndSize; use Drupal\Tests\UnitTestCase; /** @@ -89,22 +90,22 @@ public function testToUnitAndSize($size, array $expected_unit_and_size) { */ public function providerTestToUnitAndSize() { return [ - [0, ['unit' => 'B', 'size' => 0]], - [1, ['unit' => 'B', 'size' => 1]], - [-1, []], - [768, ['unit' => 'B', 'size' => 768]], - [Bytes::KILOBYTE, ['unit' => 'KB', 'size' => 1]], - [pow(Bytes::KILOBYTE, 2), ['unit' => 'MB', 'size' => 1]], - [pow(Bytes::KILOBYTE, 3), ['unit' => 'GB', 'size' => 1]], - [pow(Bytes::KILOBYTE, 4), ['unit' => 'TB', 'size' => 1]], - [pow(Bytes::KILOBYTE, 5), ['unit' => 'PB', 'size' => 1]], - [pow(Bytes::KILOBYTE, 6), ['unit' => 'EB', 'size' => 1]], - [pow(Bytes::KILOBYTE, 7), ['unit' => 'ZB', 'size' => 1]], - [pow(Bytes::KILOBYTE, 8), ['unit' => 'YB', 'size' => 1]], - [23476892, ['unit' => 'MB', 'size' => 22.39]], - [79691776, ['unit' => 'MB', 'size' => 76]], - [81862076662, ['unit' => 'GB', 'size' => 76.24]], - [4053371676, ['unit' => 'GB', 'size' => 3.77]], + [0, new BytesUnitAndSize(0, 'B')], + [1, new BytesUnitAndSize(1, 'B')], + [-1, NULL], + [768, new BytesUnitAndSize(768, 'B')], + [Bytes::KILOBYTE, new BytesUnitAndSize(1, 'KB')], + [pow(Bytes::KILOBYTE, 2), new BytesUnitAndSize(1, 'MB')], + [pow(Bytes::KILOBYTE, 3), new BytesUnitAndSize(1, 'GB')], + [pow(Bytes::KILOBYTE, 4), new BytesUnitAndSize(1, 'TB')], + [pow(Bytes::KILOBYTE, 5), new BytesUnitAndSize(1, 'PB')], + [pow(Bytes::KILOBYTE, 6), new BytesUnitAndSize(1, 'EB')], + [pow(Bytes::KILOBYTE, 7), new BytesUnitAndSize(1, 'ZB')], + [pow(Bytes::KILOBYTE, 8), new BytesUnitAndSize(1, 'KB')], + [23476892, new BytesUnitAndSize(22.39, 'MB')], + [79691776, new BytesUnitAndSize(76, 'MB')], + [81862076662, new BytesUnitAndSize(76.24, 'GB')], + [4053371676, new BytesUnitAndSize(3.77, 'GB')], ]; } }