Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
39 | PointedEar | 1 | <?php |
2 | /** |
||
3 | * Zend Framework (http://framework.zend.com/) |
||
4 | * |
||
5 | * @link http://github.com/zendframework/zf2 for the canonical source repository |
||
6 | * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) |
||
7 | * @license http://framework.zend.com/license/new-bsd New BSD License |
||
8 | */ |
||
9 | |||
10 | namespace Zend\Stdlib; |
||
11 | |||
12 | use ErrorException; |
||
13 | |||
14 | /** |
||
15 | * ErrorHandler that can be used to catch internal PHP errors |
||
16 | * and convert to a ErrorException instance. |
||
17 | */ |
||
18 | abstract class ErrorHandler |
||
19 | { |
||
20 | /** |
||
21 | * Active stack |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected static $stack = array(); |
||
26 | |||
27 | /** |
||
28 | * Check if this error handler is active |
||
29 | * |
||
30 | * @return boolean |
||
31 | */ |
||
32 | public static function started() |
||
33 | { |
||
34 | return (bool) static::getNestedLevel(); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Get the current nested level |
||
39 | * |
||
40 | * @return int |
||
41 | */ |
||
42 | public static function getNestedLevel() |
||
43 | { |
||
44 | return count(static::$stack); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Starting the error handler |
||
49 | * |
||
50 | * @param int $errorLevel |
||
51 | */ |
||
52 | public static function start($errorLevel = \E_WARNING) |
||
53 | { |
||
54 | if (!static::$stack) { |
||
55 | set_error_handler(array(get_called_class(), 'addError'), $errorLevel); |
||
56 | } |
||
57 | |||
58 | static::$stack[] = null; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Stopping the error handler |
||
63 | * |
||
64 | * @param bool $throw Throw the ErrorException if any |
||
65 | * @return null|ErrorException |
||
66 | * @throws ErrorException If an error has been catched and $throw is true |
||
67 | */ |
||
68 | public static function stop($throw = false) |
||
69 | { |
||
70 | $errorException = null; |
||
71 | |||
72 | if (static::$stack) { |
||
73 | $errorException = array_pop(static::$stack); |
||
74 | |||
75 | if (!static::$stack) { |
||
76 | restore_error_handler(); |
||
77 | } |
||
78 | |||
79 | if ($errorException && $throw) { |
||
80 | throw $errorException; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | return $errorException; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Stop all active handler |
||
89 | * |
||
90 | * @return void |
||
91 | */ |
||
92 | public static function clean() |
||
93 | { |
||
94 | if (static::$stack) { |
||
95 | restore_error_handler(); |
||
96 | } |
||
97 | |||
98 | static::$stack = array(); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Add an error to the stack |
||
103 | * |
||
104 | * @param int $errno |
||
105 | * @param string $errstr |
||
106 | * @param string $errfile |
||
107 | * @param int $errline |
||
108 | * @return void |
||
109 | */ |
||
110 | public static function addError($errno, $errstr = '', $errfile = '', $errline = 0) |
||
111 | { |
||
112 | $stack = & static::$stack[ count(static::$stack) - 1 ]; |
||
113 | $stack = new ErrorException($errstr, 0, $errno, $errfile, $errline, $stack); |
||
114 | } |
||
115 | } |