1: <?php
2:
3: namespace PHPixie\Debug;
4:
5: class Handlers
6: {
7: protected $builder;
8:
9: public function __construct($builder)
10: {
11: $this->builder = $builder;
12: }
13:
14: public function register($shutdownLog = false, $exception = true, $error = true)
15: {
16: if($error) {
17: $this->registerErrorHandler();
18: }
19:
20: if($exception) {
21: $this->registerExceptionHandler();
22: }
23:
24: if($shutdownLog) {
25: $this->registerShutdownLogHandler();
26: }
27: }
28:
29: public function registerErrorHandler()
30: {
31: $self = $this;
32: $this->setErrorHandler(function($level, $message, $file, $line) use($self) {
33: $self->handleError($level, $message, $file, $line);
34: });
35: }
36:
37: public function registerExceptionHandler()
38: {
39: $self = $this;
40: $this->setExceptionHandler(function($exception) use($self) {
41: $self->handleException($exception);
42: });
43: }
44:
45: public function registerShutdownLogHandler()
46: {
47: $self = $this;
48: $this->setShutdownHandler(function() use($self) {
49: $self->handleShutdownLog();
50: });
51: }
52:
53: public function handleError($level, $message, $file, $line)
54: {
55: throw new \ErrorException($message, 0, $level, $file, $line);
56: }
57:
58: public function handleException($exception)
59: {
60: $messages = $this->builder->messages();
61: echo "\n\n".$messages->exception($exception);
62: echo "\n\n".$messages->log();
63: }
64:
65: public function handleShutdownLog()
66: {
67: $messages = $this->builder->messages();
68: echo "\n\n".$messages->log();
69: }
70:
71: protected function setErrorHandler($callback)
72: {
73: set_error_handler($callback);
74: }
75:
76: protected function setExceptionHandler($callback)
77: {
78: set_exception_handler($callback);
79: }
80:
81: protected function setShutdownHandler($callback)
82: {
83: register_shutdown_function($callback);
84: }
85: }
86: