|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BFW\Core; |
|
4
|
|
|
|
|
5
|
|
|
use \BFW\Application; |
|
6
|
|
|
|
|
7
|
|
|
class Errors |
|
8
|
|
|
{ |
|
9
|
|
|
protected static $app = null; |
|
10
|
|
|
|
|
11
|
|
|
public function __construct(\BFW\Application $app) |
|
12
|
|
|
{ |
|
13
|
|
|
self::$app = $app; |
|
14
|
|
|
$calledClass = get_called_class(); |
|
15
|
|
|
|
|
16
|
|
|
$errorRender = $calledClass::getErrorRender(); |
|
17
|
|
|
if (!empty($errorRender)) { |
|
18
|
|
|
set_error_handler(['\BFW\Core\Errors', 'errorHandler']); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
$exceptionRender = $calledClass::getExceptionRender(); |
|
22
|
|
|
if (!empty($exceptionRender)) { |
|
23
|
|
|
set_exception_handler(['\BFW\Core\Errors', 'exceptionHandler']); |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
protected static function getApp() |
|
28
|
|
|
{ |
|
29
|
|
|
if(is_null(self::$app)) { |
|
30
|
|
|
self::$app = Application::getInstance(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return self::$app; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
View Code Duplication |
public static function getErrorRender() |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
$calledClass = get_called_class(); |
|
39
|
|
|
$app = $calledClass::getApp(); |
|
40
|
|
|
$renderFcts = $app->getConfig('errorRenderFct'); |
|
41
|
|
|
|
|
42
|
|
|
return self::defineRenderToUse($renderFcts); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
View Code Duplication |
public static function getExceptionRender() |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
$calledClass = get_called_class(); |
|
48
|
|
|
$app = $calledClass::getApp(); |
|
49
|
|
|
$renderFcts = $app->getConfig('exceptionRenderFct'); |
|
50
|
|
|
|
|
51
|
|
|
return self::defineRenderToUse($renderFcts); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected static function defineRenderToUse($renderConfig) |
|
55
|
|
|
{ |
|
56
|
|
|
if (PHP_SAPI === 'cli' && isset($renderConfig['cli'])) { |
|
57
|
|
|
return $renderConfig['cli']; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if(isset($renderConfig['default'])) { |
|
61
|
|
|
return $renderConfig['default']; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public static function exceptionHandler($exception) |
|
68
|
|
|
{ |
|
69
|
|
|
$calledClass = get_called_class(); |
|
70
|
|
|
$errorRender = $calledClass::getExceptionRender(); |
|
71
|
|
|
|
|
72
|
|
|
$errorRender( |
|
73
|
|
|
'Fatal', |
|
74
|
|
|
$exception->getMessage(), |
|
75
|
|
|
$exception->getFile(), |
|
76
|
|
|
$exception->getLine(), |
|
77
|
|
|
$exception->getTrace() |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public static function errorHandler( |
|
82
|
|
|
$errSeverity, |
|
83
|
|
|
$errMsg, |
|
84
|
|
|
$errFile, |
|
85
|
|
|
$errLine |
|
86
|
|
|
) { |
|
87
|
|
|
$calledClass = get_called_class(); |
|
88
|
|
|
$erreurType = $calledClass::getErrorType($errSeverity); |
|
89
|
|
|
$errorRender = $calledClass::getErrorRender(); |
|
90
|
|
|
|
|
91
|
|
|
$errorRender( |
|
92
|
|
|
$erreurType, |
|
93
|
|
|
$errMsg, |
|
94
|
|
|
$errFile, |
|
95
|
|
|
$errLine, |
|
96
|
|
|
debug_backtrace() |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected static function getErrorType($errSeverity) |
|
101
|
|
|
{ |
|
102
|
|
|
//List : http://fr2.php.net/manual/fr/function.set-error-handler.php#113567 |
|
103
|
|
|
$map = [ |
|
104
|
|
|
E_ERROR => 'Fatal', |
|
105
|
|
|
E_CORE_ERROR => 'Fatal', |
|
106
|
|
|
E_USER_ERROR => 'Fatal', |
|
107
|
|
|
E_COMPILE_ERROR => 'Fatal', |
|
108
|
|
|
E_RECOVERABLE_ERROR => 'Fatal', |
|
109
|
|
|
E_WARNING => 'Fatal', |
|
110
|
|
|
E_CORE_WARNING => 'Fatal', |
|
111
|
|
|
E_USER_WARNING => 'Fatal', |
|
112
|
|
|
E_COMPILE_WARNING => 'Fatal', |
|
113
|
|
|
E_PARSE => 'Parse', |
|
114
|
|
|
E_NOTICE => 'Notice', |
|
115
|
|
|
E_USER_NOTICE => 'Notice', |
|
116
|
|
|
E_STRICT => 'Strict', |
|
117
|
|
|
E_RECOVERABLE_ERROR => '/', |
|
118
|
|
|
E_DEPRECATED => 'Deprecated', |
|
119
|
|
|
E_USER_DEPRECATED => 'Deprecated' |
|
120
|
|
|
]; |
|
121
|
|
|
|
|
122
|
|
|
$erreurType = 'Unknown'; |
|
123
|
|
|
if (isset($map[$errSeverity])) { |
|
124
|
|
|
$erreurType = $map[$errSeverity]; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $erreurType; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public static function defaultCliErrorRender( |
|
131
|
|
|
$erreurType, |
|
132
|
|
|
$errMsg, |
|
133
|
|
|
$errFile, |
|
134
|
|
|
$errLine, |
|
135
|
|
|
$backtrace |
|
|
|
|
|
|
136
|
|
|
) { |
|
137
|
|
|
$msgError = $erreurType.' Error : '.$errMsg. |
|
138
|
|
|
' in '.$errFile.' at line '.$errLine; |
|
139
|
|
|
|
|
140
|
|
|
\BFW\Cli\displayMsg( |
|
141
|
|
|
$msgError, |
|
142
|
|
|
'white', |
|
143
|
|
|
'red' |
|
144
|
|
|
); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public static function defaultErrorRender( |
|
148
|
|
|
$erreurType, |
|
149
|
|
|
$errMsg, |
|
150
|
|
|
$errFile, |
|
151
|
|
|
$errLine, |
|
152
|
|
|
$backtrace |
|
153
|
|
|
) { |
|
154
|
|
|
ob_clean(); |
|
155
|
|
|
|
|
156
|
|
|
echo ' |
|
157
|
|
|
<!doctype html> |
|
158
|
|
|
<html lang="fr"> |
|
159
|
|
|
<head> |
|
160
|
|
|
<title>Une erreur est parmi nous !</title> |
|
161
|
|
|
<style> |
|
162
|
|
|
html {padding:0; margin:0; background-color:#e3e3e3; font-family:sans-serif; font-size: 1em; word-wrap:break-word;} |
|
163
|
|
|
div {position:relative; margin:auto; width:950px; border: 1px solid #a6c9e2; top: 30px; margin-bottom:10px;} |
|
164
|
|
|
p {padding:0; margin:0;} |
|
165
|
|
|
p.title {font-size:1.2em; background-color:#D0DCE9; padding:10px;} |
|
166
|
|
|
p.info {padding:5px; margin-top:10px; margin-bottom:10px;} |
|
167
|
|
|
fieldset {border:none; background-color: white;} |
|
168
|
|
|
pre {width:910px; line-height:1.5;} |
|
169
|
|
|
</style> |
|
170
|
|
|
</head> |
|
171
|
|
|
<body> |
|
172
|
|
|
<div> |
|
173
|
|
|
<p class="title">Niarf, a error is detected !</p> |
|
174
|
|
|
<p class="info">'.$erreurType.' Error : <strong>'.$errMsg.'</strong> in '.$errFile.' at line '.$errLine.'</p> |
|
175
|
|
|
<fieldset><pre>'; |
|
176
|
|
|
foreach ($backtrace as $i => $info) { |
|
177
|
|
|
echo '#'.$i.' '.$info['function']; |
|
178
|
|
|
|
|
179
|
|
|
if (isset($info['args']) && count($info['args']) > 0) { |
|
180
|
|
|
echo '('; |
|
181
|
|
|
|
|
182
|
|
|
foreach ($info['args'] as $iArgs => $args) { |
|
183
|
|
|
if ($iArgs > 0) { |
|
184
|
|
|
echo ', '; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
if (is_array($args) || is_object($args)) { |
|
188
|
|
|
echo gettype($args); |
|
189
|
|
|
} elseif (is_null($args)) { |
|
190
|
|
|
echo 'null'; |
|
191
|
|
|
} else { |
|
192
|
|
|
echo htmlentities($args); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
echo ')'; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
if (isset($info['file'], $info['line'])) { |
|
200
|
|
|
echo ' called at ['.$info['file'].' line '.$info['line'].']'; |
|
201
|
|
|
} |
|
202
|
|
|
echo "\n\n"; |
|
203
|
|
|
} |
|
204
|
|
|
echo '</pre></fieldset> |
|
205
|
|
|
</div> |
|
206
|
|
|
<body> |
|
207
|
|
|
</html> |
|
208
|
|
|
'; |
|
209
|
|
|
|
|
210
|
|
|
ob_flush(); |
|
211
|
|
|
exit; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.