2013-07-18 07:09:21 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ErrorControlChain
|
|
|
|
*
|
2013-07-23 01:51:38 +02:00
|
|
|
* Runs a set of steps, optionally suppressing uncaught errors or exceptions which would otherwise be fatal that
|
|
|
|
* occur in each step. If an error does occur, subsequent steps are normally skipped, but can optionally be run anyway.
|
2013-07-18 07:09:21 +02:00
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
*
|
|
|
|
* $chain = new ErrorControlChain();
|
2013-07-23 01:51:38 +02:00
|
|
|
* $chain->then($callback1)->then($callback2)->thenIfErrored($callback3)->execute();
|
2013-07-18 07:09:21 +02:00
|
|
|
*
|
|
|
|
* WARNING: This class is experimental and designed specifically for use pre-startup in main.php
|
|
|
|
* It will likely be heavily refactored before the release of 3.2
|
2015-08-24 06:15:38 +02:00
|
|
|
*
|
|
|
|
* @package framework
|
|
|
|
* @subpackage misc
|
2013-07-18 07:09:21 +02:00
|
|
|
*/
|
|
|
|
class ErrorControlChain {
|
2013-07-22 04:48:16 +02:00
|
|
|
public static $fatal_errors = null; // Initialised after class definition
|
|
|
|
|
2014-09-26 06:54:34 +02:00
|
|
|
/**
|
|
|
|
* Is there an error?
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2013-07-18 07:09:21 +02:00
|
|
|
protected $error = false;
|
2014-09-26 06:54:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List of steps
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-07-18 07:09:21 +02:00
|
|
|
protected $steps = array();
|
|
|
|
|
2014-09-26 06:54:34 +02:00
|
|
|
/**
|
|
|
|
* True if errors should be hidden
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2013-07-18 07:09:21 +02:00
|
|
|
protected $suppression = true;
|
|
|
|
|
|
|
|
/** We can't unregister_shutdown_function, so this acts as a flag to enable handling */
|
|
|
|
protected $handleFatalErrors = false;
|
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
/** We overload display_errors to hide errors during execution, so we need to remember the original to restore to */
|
|
|
|
protected $originalDisplayErrors = null;
|
|
|
|
|
2014-09-26 06:54:34 +02:00
|
|
|
/**
|
|
|
|
* Any exceptions passed through the chain
|
|
|
|
*
|
|
|
|
* @var Exception
|
|
|
|
*/
|
|
|
|
protected $lastException = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if an error has been found
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-07-18 07:09:21 +02:00
|
|
|
public function hasErrored() {
|
|
|
|
return $this->error;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setErrored($error) {
|
|
|
|
$this->error = (bool)$error;
|
|
|
|
}
|
|
|
|
|
2016-04-01 00:03:21 +02:00
|
|
|
/**
|
|
|
|
* Sets whether errors are suppressed or not
|
|
|
|
* Notes:
|
|
|
|
* - Errors cannot be suppressed if not handling errors.
|
|
|
|
* - Errors cannot be un-suppressed if original mode dis-allowed visible errors
|
|
|
|
*
|
|
|
|
* @param bool $suppression
|
|
|
|
*/
|
2013-07-18 07:09:21 +02:00
|
|
|
public function setSuppression($suppression) {
|
|
|
|
$this->suppression = (bool)$suppression;
|
2016-05-18 03:36:54 +02:00
|
|
|
// If handling fatal errors, conditionally disable, or restore error display
|
|
|
|
// Note: original value of display_errors could also evaluate to "off"
|
|
|
|
if ($this->handleFatalErrors) {
|
|
|
|
if($suppression) {
|
|
|
|
$this->setDisplayErrors(0);
|
|
|
|
} else {
|
|
|
|
$this->setDisplayErrors($this->originalDisplayErrors);
|
|
|
|
}
|
2016-04-01 00:03:21 +02:00
|
|
|
}
|
2013-07-18 07:09:21 +02:00
|
|
|
}
|
|
|
|
|
2016-05-18 03:36:54 +02:00
|
|
|
/**
|
|
|
|
* Set display_errors
|
|
|
|
*
|
|
|
|
* @param mixed $errors
|
|
|
|
*/
|
|
|
|
protected function setDisplayErrors($errors) {
|
|
|
|
ini_set('display_errors', $errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get value of display_errors ini value
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function getDisplayErrors() {
|
|
|
|
return ini_get('display_errors');
|
|
|
|
}
|
|
|
|
|
2013-07-18 07:09:21 +02:00
|
|
|
/**
|
|
|
|
* Add this callback to the chain of callbacks to call along with the state
|
|
|
|
* that $error must be in this point in the chain for the callback to be called
|
|
|
|
*
|
|
|
|
* @param $callback - The callback to call
|
|
|
|
* @param $onErrorState - false if only call if no errors yet, true if only call if already errors, null for either
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function then($callback, $onErrorState = false) {
|
|
|
|
$this->steps[] = array(
|
|
|
|
'callback' => $callback,
|
|
|
|
'onErrorState' => $onErrorState
|
|
|
|
);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-09-26 06:54:34 +02:00
|
|
|
/**
|
|
|
|
* Request that the callback is invoked if not errored
|
|
|
|
*
|
|
|
|
* @param callable $callback
|
|
|
|
* @return $this
|
|
|
|
*/
|
2013-07-18 07:09:21 +02:00
|
|
|
public function thenWhileGood($callback) {
|
|
|
|
return $this->then($callback, false);
|
|
|
|
}
|
|
|
|
|
2014-09-26 06:54:34 +02:00
|
|
|
/**
|
|
|
|
* Request that the callback is invoked on error
|
|
|
|
*
|
|
|
|
* @param callable $callback
|
|
|
|
* @return $this
|
|
|
|
*/
|
2013-07-18 07:09:21 +02:00
|
|
|
public function thenIfErrored($callback) {
|
|
|
|
return $this->then($callback, true);
|
|
|
|
}
|
|
|
|
|
2014-09-26 06:54:34 +02:00
|
|
|
/**
|
|
|
|
* Request that the callback is invoked always
|
|
|
|
*
|
|
|
|
* @param callable $callback
|
|
|
|
* @return $this
|
|
|
|
*/
|
2013-07-18 07:09:21 +02:00
|
|
|
public function thenAlways($callback) {
|
|
|
|
return $this->then($callback, null);
|
|
|
|
}
|
|
|
|
|
2014-09-26 06:54:34 +02:00
|
|
|
/**
|
|
|
|
* Return true if the last error was fatal
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-07-18 07:09:21 +02:00
|
|
|
protected function lastErrorWasFatal() {
|
2014-09-26 06:54:34 +02:00
|
|
|
if($this->lastException) return true;
|
2013-07-18 07:09:21 +02:00
|
|
|
$error = error_get_last();
|
2013-07-23 01:51:38 +02:00
|
|
|
return $error && ($error['type'] & self::$fatal_errors) != 0;
|
2013-07-18 07:09:21 +02:00
|
|
|
}
|
|
|
|
|
2013-07-31 23:44:36 +02:00
|
|
|
protected function lastErrorWasMemoryExhaustion() {
|
|
|
|
$error = error_get_last();
|
|
|
|
$message = $error ? $error['message'] : '';
|
|
|
|
return stripos($message, 'memory') !== false && stripos($message, 'exhausted') !== false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static $transtable = array(
|
|
|
|
'k' => 1024,
|
|
|
|
'm' => 1048576,
|
|
|
|
'g' => 1073741824
|
|
|
|
);
|
|
|
|
|
|
|
|
protected function translateMemstring($memString) {
|
|
|
|
$char = strtolower(substr($memString, -1));
|
|
|
|
$fact = isset(self::$transtable[$char]) ? self::$transtable[$char] : 1;
|
|
|
|
return ((int)$memString) * $fact;
|
|
|
|
}
|
|
|
|
|
2013-07-18 07:09:21 +02:00
|
|
|
public function handleFatalError() {
|
2013-07-22 03:52:00 +02:00
|
|
|
if ($this->handleFatalErrors && $this->suppression) {
|
2013-07-18 07:09:21 +02:00
|
|
|
if ($this->lastErrorWasFatal()) {
|
2013-07-31 23:44:36 +02:00
|
|
|
if ($this->lastErrorWasMemoryExhaustion()) {
|
|
|
|
// Bump up memory limit by an arbitrary 10% / 10MB (whichever is bigger) since we've run out
|
|
|
|
$cur = $this->translateMemstring(ini_get('memory_limit'));
|
|
|
|
if ($cur != -1) ini_set('memory_limit', $cur + max(round($cur*0.1), 10000000));
|
|
|
|
}
|
|
|
|
|
2013-07-18 07:09:21 +02:00
|
|
|
$this->error = true;
|
|
|
|
$this->step();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
register_shutdown_function(array($this, 'handleFatalError'));
|
|
|
|
$this->handleFatalErrors = true;
|
|
|
|
|
2016-05-18 03:36:54 +02:00
|
|
|
$this->originalDisplayErrors = $this->getDisplayErrors();
|
2016-04-01 00:03:21 +02:00
|
|
|
$this->setSuppression($this->suppression);
|
2013-07-23 01:51:38 +02:00
|
|
|
|
2013-07-18 07:09:21 +02:00
|
|
|
$this->step();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function step() {
|
|
|
|
if ($this->steps) {
|
|
|
|
$step = array_shift($this->steps);
|
|
|
|
|
|
|
|
if ($step['onErrorState'] === null || $step['onErrorState'] === $this->error) {
|
2014-09-26 06:54:34 +02:00
|
|
|
try {
|
|
|
|
call_user_func($step['callback'], $this);
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
$this->lastException = $ex;
|
|
|
|
throw $ex;
|
|
|
|
}
|
2013-07-18 07:09:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->step();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Now clean up
|
|
|
|
$this->handleFatalErrors = false;
|
2016-05-18 03:36:54 +02:00
|
|
|
$this->setDisplayErrors($this->originalDisplayErrors);
|
2013-07-18 07:09:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-22 04:48:16 +02:00
|
|
|
|
|
|
|
ErrorControlChain::$fatal_errors = E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR;
|
|
|
|
if (defined('E_RECOVERABLE_ERROR')) ErrorControlChain::$fatal_errors |= E_RECOVERABLE_ERROR;
|