|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BFW\Core; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class used to have a personnal message/page for errors and exceptions |
|
7
|
|
|
*/ |
|
8
|
|
|
class Errors |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Constructeur |
|
12
|
|
|
*/ |
|
13
|
|
|
public function __construct() |
|
14
|
|
|
{ |
|
15
|
|
|
//Find and create the handler for errors |
|
16
|
|
|
$this->defineErrorHandler(); |
|
17
|
|
|
|
|
18
|
|
|
//Find and create the handler for exceptions |
|
19
|
|
|
$this->defineExceptionHandler(); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Find and create the handler for errors |
|
24
|
|
|
* |
|
25
|
|
|
* @return void |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function defineErrorHandler() |
|
28
|
|
|
{ |
|
29
|
|
|
$errorRender = $this->obtainErrorRender(); |
|
30
|
|
|
|
|
31
|
|
|
//If not render to use |
|
32
|
|
|
if ($errorRender === false) { |
|
33
|
|
|
return; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
//add the handler for errors |
|
37
|
|
|
set_error_handler([$this, 'errorHandler']); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Find and create the handler for exceptions |
|
42
|
|
|
* |
|
43
|
|
|
* @return type |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function defineExceptionHandler() |
|
46
|
|
|
{ |
|
47
|
|
|
$exceptionRender = $this->obtainExceptionRender(); |
|
48
|
|
|
|
|
49
|
|
|
//If not render to use |
|
50
|
|
|
if ($exceptionRender === false) { |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
//add the handler for exceptions |
|
55
|
|
|
set_exception_handler([$this, 'exceptionHandler']); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get the error render from config for cli or default |
|
60
|
|
|
* |
|
61
|
|
|
* @return boolean|array Render infos |
|
62
|
|
|
* Boolean : false if no render to use |
|
63
|
|
|
* Array : Infos from config |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function obtainErrorRender() |
|
66
|
|
|
{ |
|
67
|
|
|
$app = \BFW\Application::getInstance(); |
|
68
|
|
|
$renderFcts = $app->getConfig()->getValue('errorRenderFct'); |
|
69
|
|
|
|
|
70
|
|
|
return $this->defineRenderToUse($renderFcts); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the exception render from config for cli or default |
|
75
|
|
|
* |
|
76
|
|
|
* @return boolean|array Render infos |
|
77
|
|
|
* Boolean : false if no render to use |
|
78
|
|
|
* Array : Infos from config |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function obtainExceptionRender() |
|
81
|
|
|
{ |
|
82
|
|
|
$app = \BFW\Application::getInstance(); |
|
83
|
|
|
$renderFcts = $app->getConfig()->getValue('exceptionRenderFct'); |
|
84
|
|
|
|
|
85
|
|
|
return $this->defineRenderToUse($renderFcts); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Find the render to use with the config |
|
90
|
|
|
* If cli render is not define, it's use the default render. |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $renderConfig : Render infos from config |
|
93
|
|
|
* |
|
94
|
|
|
* @return boolean|array : Render to use |
|
95
|
|
|
* Boolean : false if is no enabled or if no render is defined |
|
96
|
|
|
* Array : The render to use |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function defineRenderToUse($renderConfig) |
|
99
|
|
|
{ |
|
100
|
|
|
//Check enabled |
|
101
|
|
|
if ($renderConfig['enabled'] === false) { |
|
102
|
|
|
return false; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
//The cli render if cli mode |
|
106
|
|
|
if (PHP_SAPI === 'cli' && isset($renderConfig['cli'])) { |
|
107
|
|
|
return $renderConfig['cli']; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
//The default render or cli if cli mode and no cli render configured |
|
111
|
|
|
if (isset($renderConfig['default'])) { |
|
112
|
|
|
return $renderConfig['default']; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* The default exception handler included in BFW |
|
120
|
|
|
* |
|
121
|
|
|
* @param \Exception $exception : Exception informations |
|
122
|
|
|
* |
|
123
|
|
|
* @return void |
|
124
|
|
|
*/ |
|
125
|
|
|
public function exceptionHandler($exception) |
|
126
|
|
|
{ |
|
127
|
|
|
$errorRender = $this->obtainExceptionRender(); |
|
128
|
|
|
|
|
129
|
|
|
$this->callRender( |
|
130
|
|
|
$errorRender, |
|
|
|
|
|
|
131
|
|
|
'Exception Uncaught', |
|
132
|
|
|
$exception->getMessage(), |
|
133
|
|
|
$exception->getFile(), |
|
134
|
|
|
$exception->getLine(), |
|
135
|
|
|
$exception->getTrace() |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* The default error handler included in BFW |
|
141
|
|
|
* |
|
142
|
|
|
* @param integer $errSeverity : Error severity |
|
143
|
|
|
* @param string $errMsg : Error message |
|
144
|
|
|
* @param string $errFile : File where the error is triggered |
|
145
|
|
|
* @param integer $errLine : Line where the error is triggered |
|
146
|
|
|
* |
|
147
|
|
|
* @return void |
|
148
|
|
|
*/ |
|
149
|
|
|
public function errorHandler( |
|
150
|
|
|
$errSeverity, |
|
151
|
|
|
$errMsg, |
|
152
|
|
|
$errFile, |
|
153
|
|
|
$errLine |
|
154
|
|
|
) { |
|
155
|
|
|
$errType = $this->obtainErrorType($errSeverity); |
|
156
|
|
|
$errorRender = $this->obtainErrorRender(); |
|
157
|
|
|
|
|
158
|
|
|
//Call the "callRender" method for this class (or child class) |
|
159
|
|
|
$this->callRender( |
|
160
|
|
|
$errorRender, |
|
|
|
|
|
|
161
|
|
|
$errType, |
|
162
|
|
|
$errMsg, |
|
163
|
|
|
$errFile, |
|
164
|
|
|
$errLine, |
|
165
|
|
|
debug_backtrace() |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Call the personnal class-method or function declared on config when |
|
171
|
|
|
* an exception or an error is triggered. |
|
172
|
|
|
* |
|
173
|
|
|
* @param array $renderInfos : Infos from config |
|
174
|
|
|
* @param string $errType : Human readable error severity |
|
175
|
|
|
* @param string $errMsg : Error/exception message |
|
176
|
|
|
* @param string $errFile : File where the error/exception is triggered |
|
177
|
|
|
* @param integer $errLine : Line where the error/exception is triggered |
|
178
|
|
|
* @param array $backtrace : Error/exception backtrace |
|
179
|
|
|
* |
|
180
|
|
|
* @return void |
|
181
|
|
|
*/ |
|
182
|
|
|
protected function callRender( |
|
183
|
|
|
$renderInfos, |
|
184
|
|
|
$errType, |
|
185
|
|
|
$errMsg, |
|
186
|
|
|
$errFile, |
|
187
|
|
|
$errLine, |
|
188
|
|
|
$backtrace |
|
189
|
|
|
) { |
|
190
|
|
|
$this->saveIntoPhpLog($errType, $errMsg, $errFile, $errLine); |
|
191
|
|
|
|
|
192
|
|
|
$class = $renderInfos['class']; |
|
193
|
|
|
$method = $renderInfos['method']; |
|
194
|
|
|
|
|
195
|
|
|
//If is a class, call "$class::$method" (compatibility 5.x) |
|
196
|
|
|
if (!empty($class)) { |
|
197
|
|
|
$class::$method( |
|
198
|
|
|
$errType, |
|
199
|
|
|
$errMsg, |
|
200
|
|
|
$errFile, |
|
201
|
|
|
$errLine, |
|
202
|
|
|
$backtrace |
|
203
|
|
|
); |
|
204
|
|
|
|
|
205
|
|
|
return; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
//If is not a class, it's a function. |
|
209
|
|
|
$method( |
|
210
|
|
|
$errType, |
|
211
|
|
|
$errMsg, |
|
212
|
|
|
$errFile, |
|
213
|
|
|
$errLine, |
|
214
|
|
|
$backtrace |
|
215
|
|
|
); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Save the error into the PHP log |
|
220
|
|
|
* |
|
221
|
|
|
* @param string $errType : Human readable error severity |
|
222
|
|
|
* @param string $errMsg : Error/exception message |
|
223
|
|
|
* @param string $errFile : File where the error/exception is triggered |
|
224
|
|
|
* @param integer $errLine : Line where the error/exception is triggered |
|
225
|
|
|
* |
|
226
|
|
|
* @return void |
|
227
|
|
|
*/ |
|
228
|
|
|
protected function saveIntoPhpLog( |
|
229
|
|
|
$errType, |
|
230
|
|
|
$errMsg, |
|
231
|
|
|
$errFile, |
|
232
|
|
|
$errLine |
|
233
|
|
|
) { |
|
234
|
|
|
error_log( |
|
235
|
|
|
'Error detected : '.$errType.' '.$errMsg |
|
236
|
|
|
.' at '.$errFile.':'.$errLine |
|
237
|
|
|
); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Map array to have a human readable severity. |
|
242
|
|
|
* |
|
243
|
|
|
* @see http://fr2.php.net/manual/fr/function.set-error-handler.php#113567 |
|
244
|
|
|
* |
|
245
|
|
|
* @param int $errSeverity : The error severity with PHP constant |
|
246
|
|
|
* |
|
247
|
|
|
* @return string |
|
248
|
|
|
*/ |
|
249
|
|
|
protected function obtainErrorType($errSeverity) |
|
250
|
|
|
{ |
|
251
|
|
|
$errorMap = [ |
|
252
|
|
|
E_ERROR => 'Fatal', |
|
253
|
|
|
E_CORE_ERROR => 'Fatal', |
|
254
|
|
|
E_USER_ERROR => 'Fatal', |
|
255
|
|
|
E_COMPILE_ERROR => 'Fatal', |
|
256
|
|
|
E_RECOVERABLE_ERROR => 'Fatal', |
|
257
|
|
|
E_WARNING => 'Warning', |
|
258
|
|
|
E_CORE_WARNING => 'Warning', |
|
259
|
|
|
E_USER_WARNING => 'Warning', |
|
260
|
|
|
E_COMPILE_WARNING => 'Warning', |
|
261
|
|
|
E_PARSE => 'Parse', |
|
262
|
|
|
E_NOTICE => 'Notice', |
|
263
|
|
|
E_USER_NOTICE => 'Notice', |
|
264
|
|
|
E_STRICT => 'Strict', |
|
265
|
|
|
E_DEPRECATED => 'Deprecated', |
|
266
|
|
|
E_USER_DEPRECATED => 'Deprecated' |
|
267
|
|
|
]; |
|
268
|
|
|
|
|
269
|
|
|
//Default value if the error is not found in the map array |
|
270
|
|
|
$errType = 'Unknown'; |
|
271
|
|
|
|
|
272
|
|
|
//Search in map array |
|
273
|
|
|
if (isset($errorMap[$errSeverity])) { |
|
274
|
|
|
$errType = $errorMap[$errSeverity]; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
return $errType; |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.