This repository was archived by the owner on May 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathErrorHandler.php
72 lines (56 loc) · 1.56 KB
/
ErrorHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace Cyantree\Grout;
use Cyantree\Grout\Types\ErrorHandlerError;
class ErrorHandler
{
private $_callback;
public $data;
private $_registered;
private static $_pool = array();
private static $_poolLength = 0;
/** @return ErrorHandler */
public static function getHandler($callback, $data = null, $register = true)
{
if (self::$_poolLength) {
$h = array_pop(self::$_pool);
self::$_poolLength--;
} else {
$h = new ErrorHandler();
}
$h->_callback = $callback;
$h->data = $data;
if ($register) $h->register();
return $h;
}
public function register($newData = null)
{
if ($this->_registered) return;
if ($newData !== null) $this->data = $newData;
set_error_handler(array($this, 'onError'), E_ALL);
$this->_registered = true;
}
public function unRegister()
{
if (!$this->_registered) return;
restore_error_handler();
$this->_registered = false;
}
public function onError($code, $message, $file, $line, $context)
{
$e = new ErrorHandlerError();
$e->code = $code;
$e->message = $message;
$e->file = $file;
$e->line = $line;
$e->context = $context;
$e->data = $this->data;
call_user_func($this->_callback, $e);
}
public function destroy()
{
$this->unregister();
$this->_callback = $this->data = null;
self::$_pool[] = $this;
self::$_poolLength++;
}
}