class Math
{
+ public static function toBytes($val) {
+ $val = trim($val);
+ $last = strtolower($val[strlen($val)-1]);
+ $val = substr($val, 0, -1);
+ switch($last) {
+ // The 'G' modifier is available since PHP 5.1.0
+ case 'g':
+ $val *= 1024;
+ case 'm':
+ $val *= 1024;
+ case 'k':
+ $val *= 1024;
+ }
+ return $val;
+ }
+
public static function interval($val, $min, $max)
{
if ($min > $max) {
ini_set("memory_limit", $amount);
}
+ public static function isCloseToMemoryLimit($throw = false)
+ {
+ $res = (memory_get_usage() / Math::toBytes(ini_get('memory_limit'))) > 0.9;
+ if ($throw && $res) {
+ throw new \Exception('Memory is close to the limit');
+ }
+ return $res;
+ }
+
/**
* @param $file
* @return mixed