diff --git a/src/Error/BaseErrorHandler.php b/src/Error/BaseErrorHandler.php index 6a9ca73da88..cc4dac79168 100644 --- a/src/Error/BaseErrorHandler.php +++ b/src/Error/BaseErrorHandler.php @@ -72,6 +72,13 @@ public function register() if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')) { return; } + $megabytes = Configure::read('Error.extraFatalErrorMemory'); + if ($megabytes === null) { + $megabytes = 4; + } + if ($megabytes !== false && $megabytes > 0) { + static::increaseMemoryLimit($megabytes * 1024); + } $error = error_get_last(); if (!is_array($error)) { return; @@ -212,6 +219,36 @@ public function handleFatalError($code, $description, $file, $line) return true; } + /** + * Increases the PHP "memory_limit" ini setting by the specified amount + * in kilobytes + * + * @param string $additionalKb Number in kilobytes + * @return void + */ + public static function increaseMemoryLimit($additionalKb) + { + $limit = ini_get("memory_limit"); + if (!is_string($limit) || !strlen($limit)) { + return; + } + $limit = trim($limit); + $units = strtoupper(substr($limit, -1)); + $current = substr($limit, 0, strlen($limit) - 1); + if ($units === "M") { + $current = $current * 1024; + $units = "K"; + } + if ($units === "G") { + $current = $current * 1024 * 1024; + $units = "K"; + } + + if ($units === "K") { + ini_set("memory_limit", ceil($current + $additionalKb) . "K"); + } + } + /** * Log an error. *