1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Buttress\Concrete\Exception; |
4
|
|
|
|
5
|
|
|
use League\CLImate\CLImate; |
6
|
|
|
|
7
|
|
|
class ErrorHandler |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** @var \League\CLImate\CLImate */ |
11
|
|
|
private $cli; |
12
|
|
|
|
13
|
|
|
protected $verbose = false; |
14
|
|
|
|
15
|
|
|
public function __construct(CLImate $cli) |
16
|
|
|
{ |
17
|
|
|
$this->cli = $cli; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Register this error handler |
22
|
|
|
*/ |
23
|
|
|
public function register() |
24
|
|
|
{ |
25
|
|
|
set_error_handler([$this, 'handleError']); |
26
|
|
|
set_exception_handler([$this, 'handleException']); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function setVerbose($verbose) |
30
|
|
|
{ |
31
|
|
|
$this->verbose = $verbose; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Handle an exception |
36
|
|
|
* @param \Exception $e |
37
|
|
|
*/ |
38
|
|
|
public function handleException(\Exception $e) |
39
|
|
|
{ |
40
|
|
|
$cli = $this->cli; |
41
|
|
|
|
42
|
|
|
$cli->errorInline(sprintf('<bold>%s</bold> ', '<underline>Uncaught Exception</underline>')) |
|
|
|
|
43
|
|
|
->errorInline(sprintf('on line <bold><yellow>%s</yellow></bold> ', $e->getLine())) |
44
|
|
|
->error(sprintf('in <bold><yellow>%s</yellow></bold>', $e->getFile())); |
45
|
|
|
|
46
|
|
|
$cli->out("<dim><error>>></error></dim> " . $e->getMessage()); |
47
|
|
|
|
48
|
|
|
$this->outputStack(); |
49
|
|
|
exit(1); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Handle an error |
54
|
|
|
* @param int $code |
55
|
|
|
* @param string $message |
56
|
|
|
* @param string $file |
57
|
|
|
* @param int $line |
58
|
|
|
* @param array $context |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function handleError($code, $message, $file, $line, $context) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
if (!(error_reporting() & $code)) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$cli = $this->cli; |
68
|
|
|
|
69
|
|
|
switch ($code) { |
70
|
|
|
case E_ERROR: |
71
|
|
|
case E_USER_ERROR: |
72
|
|
|
$type = '<error>Fatal Error</error>'; |
73
|
|
|
break; |
74
|
|
|
case E_RECOVERABLE_ERROR: |
75
|
|
|
case E_WARNING: |
76
|
|
|
case E_USER_WARNING: |
77
|
|
|
$type = '<yellow>Warning</yellow>'; |
78
|
|
|
break; |
79
|
|
|
case E_NOTICE: |
80
|
|
|
case E_USER_NOTICE: |
81
|
|
|
$type = '<green>Notice</green>'; |
82
|
|
|
break; |
83
|
|
|
case E_DEPRECATED: |
84
|
|
|
case E_USER_DEPRECATED: |
85
|
|
|
$type = '<blue>Deprecated Alert</blue>'; |
86
|
|
|
break; |
87
|
|
|
default: |
88
|
|
|
$type = 'Unknown Error'; |
89
|
|
|
break; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$cli->errorInline(sprintf('<bold>%s</bold> ', $type)) |
|
|
|
|
93
|
|
|
->errorInline(sprintf('on line <bold><yellow>%s</yellow></bold> ', $line)) |
94
|
|
|
->error(sprintf('in <bold><yellow>%s</yellow></bold>', $file)); |
95
|
|
|
|
96
|
|
|
$cli->out("<dim><error>>></error></dim> " . $message); |
97
|
|
|
|
98
|
|
|
$this->outputStack(); |
99
|
|
|
exit(1); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function outputStack() |
103
|
|
|
{ |
104
|
|
|
if (!$this->verbose) { |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
109
|
|
|
$count = count($backtrace); |
110
|
|
|
|
111
|
|
|
$this->cli->br(); |
112
|
|
|
foreach ($backtrace as $key => $item) { |
113
|
|
|
$this->cli->dim()->inline(str_pad($count-- . '. ', 4, ' ', STR_PAD_RIGHT)); |
114
|
|
|
|
115
|
|
|
$string = '%s()'; |
116
|
|
|
$data = [ |
117
|
|
|
$item['function'] |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
if (isset($item['class'])) { |
121
|
|
|
list($class, $namespace) = explode('\\', strrev($item['class']), 2); |
122
|
|
|
$string = '%s\\<green>%s</green><dim>%s</dim>%s()'; |
123
|
|
|
$data = [ |
124
|
|
|
strrev($namespace), |
125
|
|
|
strrev($class), |
126
|
|
|
$item['type'], |
127
|
|
|
$item['function'] |
128
|
|
|
]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
array_unshift($data, $string); |
132
|
|
|
$this->cli->inline(call_user_func_array('sprintf', $data)); |
133
|
|
|
|
134
|
|
|
if (isset($item['file'])) { |
135
|
|
|
$this->cli->out( |
136
|
|
|
sprintf( |
137
|
|
|
'<dim> >> On line <yellow>%s</yellow> in <bold>%s</bold></dim>', |
138
|
|
|
$item['line'], |
139
|
|
|
$item['file'] |
140
|
|
|
)); |
141
|
|
|
} else { |
142
|
|
|
$this->cli->br(); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.