Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | /** | |
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). | |
11 | * | |
12 | * To ensure that new errors arising during execution of the current | |
13 | * function are immediately fatal, use: | |
14 | * | |
15 | * To ensure that they throw exceptions, use: | |
16 | * | |
17 | * @code | |
18 | * $errorScope = CRM_Core_TemporaryErrorScope::useException(); | |
19 | * @endcode | |
20 | * | |
21 | * Note that relying on this is a code-smell: it can be | |
22 | * safe to temporarily switch to exception | |
23 | */ | |
24 | class CRM_Core_TemporaryErrorScope { | |
25 | static $oldFrames; | |
26 | ||
6a4257d4 TO |
27 | /** |
28 | * @return CRM_Core_TemporaryErrorScope | |
29 | */ | |
6a488035 | 30 | public static function useException() { |
6a4257d4 TO |
31 | return self::create(array('CRM_Core_Error', 'exceptionHandler'), 1); |
32 | } | |
33 | ||
34 | /** | |
35 | * @return CRM_Core_TemporaryErrorScope | |
36 | */ | |
37 | public static function ignoreException() { | |
38 | return self::create(array('CRM_Core_Error', 'nullHandler')); | |
39 | } | |
40 | ||
41 | /** | |
42 | * @param mixed $callback | |
43 | * @return CRM_Core_TemporaryErrorScope | |
44 | */ | |
45 | public static function create($callback, $modeException = NULL) { | |
6a488035 TO |
46 | $newFrame = array( |
47 | '_PEAR_default_error_mode' => PEAR_ERROR_CALLBACK, | |
6a4257d4 TO |
48 | '_PEAR_default_error_options' => $callback, |
49 | 'modeException' => $modeException, | |
6a488035 TO |
50 | ); |
51 | return new CRM_Core_TemporaryErrorScope($newFrame); | |
52 | } | |
53 | ||
54 | public function __construct($newFrame) { | |
55 | self::$oldFrames[] = self::getActive(); | |
56 | self::setActive($newFrame); | |
57 | } | |
58 | ||
59 | public function __destruct() { | |
60 | $oldFrame = array_pop(self::$oldFrames); | |
61 | self::setActive($oldFrame); | |
62 | } | |
63 | ||
64 | /** | |
65 | * Read the active error-handler settings | |
66 | */ | |
67 | public static function getActive() { | |
68 | return array( | |
69 | '_PEAR_default_error_mode' => $GLOBALS['_PEAR_default_error_mode'], | |
70 | '_PEAR_default_error_options' =>$GLOBALS['_PEAR_default_error_options'], | |
71 | 'modeException' => CRM_Core_Error::$modeException, | |
72 | ); | |
73 | } | |
74 | ||
75 | /** | |
76 | * Set the active error-handler settings | |
77 | */ | |
78 | public static function setActive($frame) { | |
79 | $GLOBALS['_PEAR_default_error_mode'] = $frame['_PEAR_default_error_mode']; | |
80 | $GLOBALS['_PEAR_default_error_options'] = $frame['_PEAR_default_error_options']; | |
81 | CRM_Core_Error::$modeException = $frame['modeException']; | |
82 | } | |
83 | } |