4 * This is an evil, evil work-around for CRM-11043. It is used to
5 * temporarily change the error-handling behavior and then automatically
6 * restore it -- that protocol is an improvement over the current protocol
7 * (in which random bits of code will change the global error handler
8 * setting and then forget to change it back). This class and all
9 * references to it should be removed in 4.3/4.4 (when we adopt
10 * exception-based error handling).
12 * To ensure that new errors arising during execution of the current
13 * function are immediately fatal, use:
15 * To ensure that they throw exceptions, use:
18 * $errorScope = CRM_Core_TemporaryErrorScope::useException();
21 * Note that relying on this is a code-smell: it can be
22 * safe to temporarily switch to exception
24 class CRM_Core_TemporaryErrorScope
{
27 public static function useException() {
29 '_PEAR_default_error_mode' => PEAR_ERROR_CALLBACK
,
30 '_PEAR_default_error_options' => array('CRM_Core_Error', 'exceptionHandler'),
33 return new CRM_Core_TemporaryErrorScope($newFrame);
36 public function __construct($newFrame) {
37 self
::$oldFrames[] = self
::getActive();
38 self
::setActive($newFrame);
41 public function __destruct() {
42 $oldFrame = array_pop(self
::$oldFrames);
43 self
::setActive($oldFrame);
47 * Read the active error-handler settings
49 public static function getActive() {
51 '_PEAR_default_error_mode' => $GLOBALS['_PEAR_default_error_mode'],
52 '_PEAR_default_error_options' =>$GLOBALS['_PEAR_default_error_options'],
53 'modeException' => CRM_Core_Error
::$modeException,
58 * Set the active error-handler settings
60 public static function setActive($frame) {
61 $GLOBALS['_PEAR_default_error_mode'] = $frame['_PEAR_default_error_mode'];
62 $GLOBALS['_PEAR_default_error_options'] = $frame['_PEAR_default_error_options'];
63 CRM_Core_Error
::$modeException = $frame['modeException'];