diff --git a/core/lib/Drupal/Component/Utility/Environment.php b/core/lib/Drupal/Component/Utility/Environment.php index 087bcad..b10b97f 100644 --- a/core/lib/Drupal/Component/Utility/Environment.php +++ b/core/lib/Drupal/Component/Utility/Environment.php @@ -9,6 +9,9 @@ /** * Provides PHP environment helper methods. + * + * @deprecated in Drupal 8.1.x, will be removed before Drupal 9.0.0. + * Use \Drupal\Component\Utility\EnvironmentMemory. */ class Environment { @@ -28,18 +31,21 @@ class Environment { * @return bool * TRUE if there is sufficient memory to allow the operation, or FALSE * otherwise. + * + * @deprecated in Drupal 8.1.x, will be removed before Drupal 9.0.0. + * Use \Drupal\Component\Utility\EnvironmentMemory: + * @code + * $memory = new EnvironmentMemory(); + * $memory->setTotal($memory_limit); // optional + * $enough_memory = $memory->checkLimit($required); + * @endcode */ public static function checkMemoryLimit($required, $memory_limit = NULL) { - if (!isset($memory_limit)) { - $memory_limit = ini_get('memory_limit'); + $memory = new EnvironmentMemory(); + if ($memory_limit) { + $memory->setTotal($memory_limit); } - - // There is sufficient memory if: - // - No memory limit is set. - // - The memory limit is set to unlimited (-1). - // - The memory limit is greater than or equal to the memory required for - // the operation. - return ((!$memory_limit) || ($memory_limit == -1) || (Bytes::toInt($memory_limit) >= Bytes::toInt($required))); + return $memory->checkLimit($required); } } diff --git a/core/lib/Drupal/Component/Utility/EnvironmentMemory.php b/core/lib/Drupal/Component/Utility/EnvironmentMemory.php index 3cb1ec8..5ea8979 100644 --- a/core/lib/Drupal/Component/Utility/EnvironmentMemory.php +++ b/core/lib/Drupal/Component/Utility/EnvironmentMemory.php @@ -50,7 +50,7 @@ public function setTotal($size = NULL) { if (!$size) { $size = ini_get('memory_limit'); } - $this->total = (!$size || $size == -1) ? -1 : Bytes::toInt($size); + $this->total = (!$size || (int) $size === -1) ? -1 : Bytes::toInt($size); } /** @@ -71,7 +71,7 @@ public function getUsed() { * 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() === - 1 ? -1 : $this->getTotal() - $this->getUsed(); } /** @@ -92,7 +92,7 @@ public function checkLimit($required) { // - No memory limit is set, or set to unlimited (-1). // - 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 == -1 || $memory_limit >= Bytes::toInt($required); } /** @@ -109,7 +109,7 @@ public function checkLimit($required) { */ public function checkAvailable($required) { $memory_limit = $this->getTotal(); - return ($memory_limit == -1 || $this->getFree() >= Bytes::toInt($required)); + return $memory_limit == -1 || $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 9ca7893..b908a09 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @@ -67,6 +67,13 @@ class GDToolkit extends ImageToolkitBase { protected $streamWrapperManager; /** + * An array storing last memory check results. + * + * @var array[] + */ + protected $memoryInfo; + + /** * Constructs a GDToolkit object. * * @param array $configuration @@ -186,10 +193,11 @@ protected function load() { } // Check if enough memory is available. - if (is_array($memory = $this->checkAvailableMemory($this->getWidth(), $this->getHeight()))) { + if (!$this->checkAvailableMemory($this->getWidth(), $this->getHeight())) { // If not, it is not possible to load the GD resource. Mark the image as // invalid, to avoid useless attempts to manipulate it. $this->preLoadInfo = NULL; + $memory = $this->getMemoryInfo(); $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'], @@ -478,9 +486,9 @@ protected static function supportedTypes() { * http://php.net/manual/en/function.imagecreatetruecolor.php#99623. * Defaults to 1.7. * - * @return null|array - * NULL if there is sufficient memory to allow the operation. Otherwise, an - * associative array with information about required, free and total memory. + * @return bool + * TRUE if there is sufficient memory to allow the operation, FALSE + * otherwise. * * @see http://php.net/manual/en/function.imagecreatetruecolor.php#99623 */ @@ -488,15 +496,27 @@ public function checkAvailableMemory($width, $height, $bits_per_pixel = 32, $twe $required = (int) ($width * $height * ($bits_per_pixel / 8) * $tweak_factor); $memory = new EnvironmentMemory(); if ($memory->checkAvailable($required)) { - return NULL; + $this->memoryInfo = []; + return TRUE; } else { - return [ + $this->memoryInfo = [ 'required' => Bytes::toUnitAndSize($required), 'free' => Bytes::toUnitAndSize($memory->getFree()), 'total' => Bytes::toUnitAndSize($memory->getTotal()), ]; + return FALSE; } } + /** + * Get last memory check information. + * + * @return array + * An associative array with information about required, free and total + * memory. + */ + public function getMemoryInfo() { + return $this->memoryInfo; + } } 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 8cd5503..f2f07e1 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php @@ -86,7 +86,8 @@ protected function validateArguments(array $arguments) { */ protected function execute(array $arguments) { // Check if enough memory is available. - if (is_array($memory = $this->getToolkit()->checkAvailableMemory($arguments['width'], $arguments['height']))) { + if (!$this->getToolkit()->checkAvailableMemory($arguments['width'], $arguments['height'])) { + $memory = $this->getToolkit()->getMemoryInfo(); 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"); } 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 0edcc1b..601f020 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php @@ -105,7 +105,8 @@ protected function execute(array $arguments) { // can never exceed the length of the diagonal of the rectangle represented // by the image. $diagonal = (int) sqrt(pow($this->getToolkit()->getWidth(), 2) + pow($this->getToolkit()->getHeight(), 2)) + 1; - if (is_array($memory = $this->getToolkit()->checkAvailableMemory($diagonal, $diagonal))) { + if (!$this->getToolkit()->checkAvailableMemory($diagonal, $diagonal)) { + $memory = $this->getToolkit()->getMemoryInfo(); 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"); } 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 b6b96f6..23edaee5 100644 --- a/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php +++ b/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php @@ -48,17 +48,6 @@ class GdToolkitTest extends UnitTestCase { protected $toolkitOperation; /** - * An insufficient memory array for testing. - * - * @var array - */ - protected $insufficientMemory = [ - 'required' => ['size' => 300, 'unit' => 'B'], - 'free' => ['size' => 100, 'unit' => 'B'], - 'total' => ['size' => 200, 'unit' => 'B'], - ]; - - /** * @inheritdoc */ protected function setUp() { @@ -123,7 +112,7 @@ public function testLoad() { $this->setToolkitMock(['checkAvailableMemory']); $this->toolkit->expects($this->any()) ->method('checkAvailableMemory') - ->will($this->returnValue(NULL)); + ->will($this->returnValue(TRUE)); $this->image = new Image($this->toolkit, $this->source); $this->assertTrue(is_resource($this->image->getToolkit()->getResource())); @@ -141,7 +130,7 @@ public function testNotEnoughMemoryOnLoad() { $this->setToolkitMock(['checkAvailableMemory']); $this->toolkit->expects($this->any()) ->method('checkAvailableMemory') - ->will($this->returnValue($this->insufficientMemory)); + ->will($this->returnValue(FALSE)); // Getting image from file validates the Image object, since GD toolkit is // lazy loading the resource. @@ -167,7 +156,7 @@ public function testNotEnoughMemoryOnCreateNew() { $this->setToolkitOperationMock('CreateNew', $this->toolkit); $this->toolkit->expects($this->any()) ->method('checkAvailableMemory') - ->will($this->onConsecutiveCalls(NULL, $this->insufficientMemory)); + ->will($this->onConsecutiveCalls(TRUE, FALSE)); $this->toolkit->expects($this->any()) ->method('getToolkitOperation') ->will($this->returnValue($this->toolkitOperation)); @@ -196,7 +185,7 @@ public function testNotEnoughMemoryOnRotate() { $this->setToolkitOperationMock('Rotate', $this->toolkit); $this->toolkit->expects($this->any()) ->method('checkAvailableMemory') - ->will($this->onConsecutiveCalls(NULL, $this->insufficientMemory)); + ->will($this->onConsecutiveCalls(TRUE, FALSE)); $this->toolkit->expects($this->any()) ->method('getToolkitOperation') ->will($this->returnValue($this->toolkitOperation)); diff --git a/core/tests/Drupal/Tests/Component/Utility/EnvironmentMemoryTest.php b/core/tests/Drupal/Tests/Component/Utility/EnvironmentMemoryTest.php index 60a3c55..3579133 100644 --- a/core/tests/Drupal/Tests/Component/Utility/EnvironmentMemoryTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/EnvironmentMemoryTest.php @@ -100,8 +100,8 @@ public function testCheckAvailableCurrent() { $limit = ini_get('memory_limit'); $limit = (!$limit || $limit == -1) ? -1 : Bytes::toInt($limit); if ($limit > 0) { - $free_memory = $limit - memory_get_usage(TRUE); $memory = new EnvironmentMemory(); + $free_memory = $limit - memory_get_usage(TRUE); $this->assertTrue($memory->checkAvailable($free_memory - Bytes::toInt('4k'))); $this->assertFalse($memory->checkAvailable($free_memory + Bytes::toInt('4k'))); } diff --git a/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php b/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php index 72f04f7..32d756f 100644 --- a/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php @@ -16,6 +16,9 @@ * @group Utility * * @coversDefaultClass \Drupal\Component\Utility\Environment + * + * @deprecated in Drupal 8.1.x, will be removed before Drupal 9.0.0. + * Use \Drupal\Component\Utility\EnvironmentMemory. */ class EnvironmentTest extends UnitTestCase {