From a11f5298a0fb29dd8bea491209b68dc528ccb936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?U=C4=9Fur=20Bi=C3=A7er?= Date: Sat, 15 May 2021 18:15:10 +0300 Subject: [PATCH] Fixed memory issue --- src/StringObjects.php | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/StringObjects.php b/src/StringObjects.php index e52bb25..f70ee8f 100644 --- a/src/StringObjects.php +++ b/src/StringObjects.php @@ -8,7 +8,7 @@ * @package strobj * @license GPLv2 * @author Uğur Biçer - * @version 0.5.0 + * @version 0.5.1 */ namespace StrObj; @@ -37,7 +37,7 @@ public function __construct($obj, int $memory) { $this->obj = $obj; - self::_setmemory($memory); + self::_setMemoryLimit($memory); } @@ -59,21 +59,40 @@ public static function instance($obj, int $memory = 50) - private static function _setmemory(int $mem) + public static function convertToByte($amount) + { + $value = intval($amount); + $unit = strtolower(substr($amount, strlen($value))); + + if ($unit == "g" || $unit == "gb") { + $value *= 1024 * 1024 * 1024; + } else if ($unit == "m" || $unit == "mb") { + $value *= 1024 * 1024; + } else if ($unit == "k" || $unit == "kb") { + $value *= 1024; + } + + return $value; + } + + + + private static function _setMemoryLimit(int $mem) { // Its check only once for performance. if (self::$memory_limit > 0) { return; } - $toMb = 1024 * 1024; - $default = 50 * $toMb; - $mem *= $toMb; - $ini_get_mem = ini_get('memory_limit') ? ini_get('memory_limit') : 0; - $ini_get_mem = intval($ini_get_mem) * $toMb; + $mbToByte = 1024 * 1024; + $default = 50 * $mbToByte; + + $mem *= $mbToByte; + + $ini_get_mem = ini_get('memory_limit') ? self::convertToByte(ini_get('memory_limit')) : 0; if (empty($ini_get_mem)) { - $mem = $default * $toMb; + $mem = $default; } else if ($mem > $ini_get_mem) { $mem = $ini_get_mem; }