diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 1c7da30..63c3aa0 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -184,13 +184,6 @@ const DRUPAL_AUTHENTICATED_RID = 'authenticated'; /** - * The number of bytes in a kilobyte. - * - * For more information, visit http://en.wikipedia.org/wiki/Kilobyte. - */ -const DRUPAL_KILOBYTE = 1024; - -/** * The maximum number of characters in a module or theme name. */ const DRUPAL_EXTENSION_NAME_MAX_LENGTH = 50; diff --git a/core/includes/common.inc b/core/includes/common.inc index 38033ac..6af931a 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -1,5 +1,6 @@ $langcode)); } else { - $size = $size / DRUPAL_KILOBYTE; // Convert bytes to kilobytes. + $size = $size / Bytes::KILOBYTE; // Convert bytes to kilobytes. $units = array( t('@size KB', array(), array('langcode' => $langcode)), t('@size MB', array(), array('langcode' => $langcode)), @@ -1101,8 +1102,8 @@ function format_size($size, $langcode = NULL) { t('@size YB', array(), array('langcode' => $langcode)), ); foreach ($units as $unit) { - if (round($size, 2) >= DRUPAL_KILOBYTE) { - $size = $size / DRUPAL_KILOBYTE; + if (round($size, 2) >= Bytes::KILOBYTE) { + $size = $size / Bytes::KILOBYTE; } else { break; diff --git a/core/lib/Drupal/Component/Utility/Bytes.php b/core/lib/Drupal/Component/Utility/Bytes.php new file mode 100644 index 0000000..7988542 --- /dev/null +++ b/core/lib/Drupal/Component/Utility/Bytes.php @@ -0,0 +1,43 @@ += self::parseSize($required))); + return ((!$memory_limit) || ($memory_limit == -1) || (Bytes::parseSize($memory_limit) >= Bytes::parseSize($required))); } /** @@ -111,26 +106,4 @@ function overrideServerVariables($variables = array()) { $_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/modules/system/lib/Drupal/system/Tests/Common/SizeUnitTest.php b/core/modules/system/lib/Drupal/system/Tests/Common/SizeUnitTest.php index a8a2662..1c1f121 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Common/SizeUnitTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Common/SizeUnitTest.php @@ -7,6 +7,7 @@ namespace Drupal\system\Tests\Common; +use Drupal\Component\Utility\Bytes; use Drupal\simpletest\UnitTestBase; /** @@ -25,7 +26,7 @@ public static function getInfo() { } function setUp() { - $kb = DRUPAL_KILOBYTE; + $kb = Bytes::KILOBYTE; $this->exact_test_cases = array( '1 byte' => 1, '1 KB' => $kb,