1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ErrorHandler |
4
|
|
|
* |
5
|
|
|
* @category Erdiko |
6
|
|
|
* @package Core |
7
|
|
|
* @copyright Copyright (c) 2016, Arroyo Labs, http://www.arroyolabs.com |
8
|
|
|
* @author Leo Daidone, [email protected] |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace erdiko\core; |
12
|
|
|
|
13
|
|
|
use ToroHook; |
|
|
|
|
14
|
|
|
|
15
|
|
|
class ErrorHandler |
16
|
|
|
{ |
17
|
|
|
public static function init() |
18
|
|
|
{ |
19
|
|
|
ini_set('html_errors',0); // @todo review this line |
20
|
|
|
// error_reporting((E_ALL | E_STRICT)); // @note we shouldn't override by default, but we could inject |
|
|
|
|
21
|
|
|
set_error_handler("\\erdiko\\core\\ErrorHandler::errorHandler"); |
22
|
|
|
register_shutdown_function("\\erdiko\\core\\ErrorHandler::fatalErrorShutdownHandler"); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public static function errorHandler($errno, $errstr, $errfile, $errline) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$debug = \erdiko\core\ErrorHandler::isDebug(); |
28
|
|
|
if ( ! ( error_reporting() & $errno ) || empty( $errstr ) ) { |
29
|
|
|
return null; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$errorHook = "500"; |
33
|
|
|
|
34
|
|
|
switch ( $errno ) { |
35
|
|
|
case E_USER_ERROR: |
36
|
|
|
$vars['msg_type'] = "USER ERROR"; |
|
|
|
|
37
|
|
|
$vars['msg_description'] = " Fatal error in line $errline of $errfile file"; |
38
|
|
|
$vars['msg_description'] .= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")"; |
39
|
|
|
break; |
40
|
|
|
|
41
|
|
|
case E_USER_WARNING: |
42
|
|
|
$vars['msg_type'] = "USER WARNING"; |
|
|
|
|
43
|
|
|
break; |
44
|
|
|
|
45
|
|
|
case E_USER_NOTICE: |
46
|
|
|
$vars['msg_type'] = "USER NOTICE"; |
|
|
|
|
47
|
|
|
break; |
48
|
|
|
|
49
|
|
|
case E_ERROR: |
50
|
|
|
$vars['msg_type'] = "ERROR"; |
|
|
|
|
51
|
|
|
$vars['msg_description'] = print_r(debug_backtrace(),1); |
52
|
|
|
break; |
53
|
|
|
|
54
|
|
|
default: |
55
|
|
|
$vars['msg_type'] = "Type of error: [$errno] $errstr"; |
|
|
|
|
56
|
|
|
$vars['msg_description'] = print_r(debug_backtrace(),1); |
57
|
|
|
break; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$vars['code'] = $errno; |
61
|
|
|
$vars['error'] = trim($errstr); |
62
|
|
|
$vars['path_info'] = $errfile . " on line " . $errline; |
63
|
|
|
$vars['debug'] = $debug; |
64
|
|
|
|
65
|
|
|
ToroHook::fire($errorHook, $vars ); |
66
|
|
|
|
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function fatalErrorShutdownHandler() |
71
|
|
|
{ |
72
|
|
|
$last_error = error_get_last(); |
73
|
|
|
static::errorHandler(E_ERROR, |
74
|
|
|
$last_error['message'], $last_error['file'], $last_error['line']); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* isDebug |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public static function isDebug() |
83
|
|
|
{ |
84
|
|
|
return (getenv("ERDIKO_DEBUG")=='1'); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: