|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByJG\RestServer; |
|
4
|
|
|
|
|
5
|
|
|
use ByJG\DesignPattern\Singleton; |
|
6
|
|
|
use ByJG\RestServer\Whoops\PlainResponseErrorHandler; |
|
7
|
|
|
use Whoops\Handler\Handler; |
|
8
|
|
|
use Whoops\Run; |
|
9
|
|
|
|
|
10
|
|
|
class ErrorHandler |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
use Singleton; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* |
|
17
|
|
|
* @var Run |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $whoops = null; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* |
|
23
|
|
|
* @var Handler |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $handler = null; |
|
26
|
|
|
|
|
27
|
1 |
|
protected function __construct() |
|
28
|
|
|
{ |
|
29
|
1 |
|
$this->whoops = new Run(); |
|
30
|
1 |
|
$this->setHandler(new PlainResponseErrorHandler()); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Set the proper Error Handler based on the Output of the page |
|
35
|
|
|
* |
|
36
|
|
|
* @param Handler $handler |
|
37
|
|
|
*/ |
|
38
|
6 |
|
public function setHandler(Handler $handler) |
|
39
|
|
|
{ |
|
40
|
6 |
|
$this->whoops->popHandler(); |
|
41
|
6 |
|
$this->handler = $handler; |
|
42
|
6 |
|
$this->whoops->pushHandler($this->handler); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Set Whoops as the default error and exception handler used by PHP: |
|
47
|
|
|
*/ |
|
48
|
7 |
|
public function register() |
|
49
|
|
|
{ |
|
50
|
7 |
|
$this->whoops->register(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Disable Whoops as the default error and exception handler used by PHP: |
|
55
|
|
|
*/ |
|
56
|
|
|
public function unregister() |
|
57
|
|
|
{ |
|
58
|
|
|
$this->whoops->unregister(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Added extra information for debug purposes on the error handler screen |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $name |
|
65
|
|
|
* @param string $value |
|
66
|
|
|
*/ |
|
67
|
|
|
public function addExtraInfo($name, $value) |
|
68
|
|
|
{ |
|
69
|
|
|
if (method_exists($this->handler, 'addDataTable')) { |
|
70
|
|
|
$data = $this->handler->getDataTable(); |
|
|
|
|
|
|
71
|
|
|
$this->handler->addDataTable('Info #' . (count($data) + 1), array($name => $value)); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|