diff --git a/core/includes/common.inc b/core/includes/common.inc index 3215de9..825ffba 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -1151,17 +1151,12 @@ function format_plural($count, $singular, $plural, array $args = array(), array * * @return * An integer representation of the size in bytes. + * + * @deprecated as of Drupal 8.0. Use + * \Drupal\Core\Bootstrap\Bootstrap::parseSize() directly instead. */ function parse_size($size) { - $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size. - $size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size. - if ($unit) { - // Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by. - return round($size * pow(DRUPAL_KILOBYTE, stripos('bkmgtpezy', $unit[0]))); - } - else { - return round($size); - } + return Bootstrap::parseSize($size); } /** diff --git a/core/lib/Drupal/Core/Bootstrap/Bootstrap.php b/core/lib/Drupal/Core/Bootstrap/Bootstrap.php index 3225eed..f29ed75 100644 --- a/core/lib/Drupal/Core/Bootstrap/Bootstrap.php +++ b/core/lib/Drupal/Core/Bootstrap/Bootstrap.php @@ -10,6 +10,14 @@ * Provides helper methods for bootstrapping Drupal. */ class Bootstrap { + + /** + * The number of bytes in a kilobyte. + * + * For more information, visit http://en.wikipedia.org/wiki/Kilobyte. + */ + const DRUPAL_KILOBYTE = 1024; + /** * Compares the memory required for an operation to the available memory. * @@ -37,7 +45,7 @@ function checkMemoryLimit($required, $memory_limit = NULL) { // - 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) || (parse_size($memory_limit) >= parse_size($required))); + return ((!$memory_limit) || ($memory_limit == -1) || (self::parseSize($memory_limit) >= self::parseSize($required))); } /** @@ -102,4 +110,27 @@ function overrideServerVariables($variables = array()) { // Replace elements of the $_SERVER array, as appropriate. $_SERVER = $variables + $_SERVER + $defaults; } + + /** + * Parses a given byte count. + * + * @param $size + * A size expressed as a number of bytes with optional SI or IEC binary unit + * prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8 bytes, 9mbytes). + * + * @return + * An integer representation of the size in bytes. + */ + function parseSize($size) { + $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size. + $size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size. + if ($unit) { + // Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by. + return round($size * pow(self::DRUPAL_KILOBYTE, stripos('bkmgtpezy', $unit[0]))); + } + else { + return round($size); + } + } + } diff --git a/core/tests/Drupal/Tests/Core/Bootstrap/MiscUnitTest.php b/core/tests/Drupal/Tests/Core/Bootstrap/MiscUnitTest.php index 7c91054..3518cd2 100644 --- a/core/tests/Drupal/Tests/Core/Bootstrap/MiscUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Bootstrap/MiscUnitTest.php @@ -23,12 +23,6 @@ public static function getInfo() { ); } - function setUp() { - // @todo think about removing this and converting to components. - // Some procedural functions called from within require common.inc. - require_once __DIR__ . '/../../../../../includes/common.inc'; - } - /** * Tests that the Bootstrap::checkMemoryLimit() method works as expected. */ @@ -41,13 +35,13 @@ function testCheckMemoryLimit() { // high. $twice_avail_memory = ($memory_limit * 2) . 'MB'; // The function should always return true if the memory limit is set to -1. - $this->assertTrue(drupal_check_memory_limit($twice_avail_memory, -1), 'drupal_check_memory_limit() returns TRUE when a limit of -1 (none) is supplied'); + $this->assertTrue(Bootstrap::checkMemoryLimit($twice_avail_memory, -1), 'Bootstrap::checkMemoryLimit() returns TRUE when a limit of -1 (none) is supplied'); // Test that even though we have 30MB of memory available - the function // returns FALSE when given an upper limit for how much memory can be used. - $this->assertFalse(drupal_check_memory_limit('30MB', '16MB'), 'drupal_check_memory_limit() returns FALSE with a 16MB upper limit on a 30MB requirement.'); + $this->assertFalse(Bootstrap::checkMemoryLimit('30MB', '16MB'), 'Bootstrap::checkMemoryLimit() returns FALSE with a 16MB upper limit on a 30MB requirement.'); // Test that an equal amount of memory to the amount requested returns TRUE. - $this->assertTrue(drupal_check_memory_limit('30MB', '30MB'), 'drupal_check_memory_limit() returns TRUE when requesting 30MB on a 30MB requirement.'); + $this->assertTrue(Bootstrap::checkMemoryLimit('30MB', '30MB'), 'Bootstrap::checkMemoryLimit() returns TRUE when requesting 30MB on a 30MB requirement.'); } }