1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
<?php
class Page
{
private static $pInstance = NULL;
private $pExceptionHandler;
private $pErrorHandler;
private $pTraceErrorHandler;
private $pRecordedErrors;
private function __construct()
{
/* */
date_default_timezone_set(date_default_timezone_get());
/* */
$this->pExceptionHandler = set_exception_handler(array($this , "OnException"));
$this->pErrorHandler = set_error_handler(array($this , "OnError"));
$this->pTraceErrorHandler = array(&$this , "TraceErrors");
$this->pRecordedErrors = new ErrorList();
ob_start();
}
public function OnException( $Ex )
{
$this->pRecordedErrors->add( $Ex );
}
public function OnError( $errno, $errstr, $errfile, $errline )
{
$Ex = new PageError( $errstr, $errno, $errfile, $errline );
$this->pRecordedErrors->add( $Ex );
}
public function __get( $prop )
{
switch( $prop)
{
case "Exception_handler":
return $this->pExceptionHandler;
break;
case "Error_handler":
return $this->pErrorHandler;
break;
case "Trace_error_handler":
return $this->pTraceErrorHandler;
break;
}
}
public function __set( $prop , $value )
{
switch( $prop)
{
case "Exception_handler":
$this->pExceptionHandler = set_exception_handler($value);
break;
case "Error_handler":
$this->pErrorHandler = set_error_handler($value);
break;
case "Trace_error_handler":
$this->pTraceErrorHandler = $value;
break;
}
}
private function TraceErrors()
{
echo "Erreur d'execution :";
echo "<br/>";
echo "<br/>";
for($i=0,$ii=count( $this->pRecordedErrors );$i<$ii;$i++)
{
if( is_object ($this->pRecordedErrors[$i]) )
{
echo "Message : ".$this->pRecordedErrors[$i]->getMessage()."<br>"; // message de l'exception
echo "Code : ".$this->pRecordedErrors[$i]->getCode()."<br>"; // code de l'exception
echo "File : ".$this->pRecordedErrors[$i]->getFile()."<br>"; // nom du fichier source
echo "Line : ".$this->pRecordedErrors[$i]->getLine()."<br>"; // ligne du fichier source
echo "Trace As String : ".$this->pRecordedErrors[$i]->getTraceAsString()."<br>"; // chaîne formattée de trace
foreach( $this->pRecordedErrors[$i]->getTrace() as $Key => $Value)
{
if( is_array($Value) )
{
foreach($Value as $_Key => $_Value)
{
echo $_Key. " : ".$_Value."<br>";
}
}
else
{
echo $Key. " : ".$Value."<br>";
}
}
echo "<br/>";
echo "<br/>";
}
}
}
public static function getInstance()
{
if( self::$pInstance == NULL ) self::$pInstance = new Page();
return self::$pInstance;
}
public function __destruct()
{
if( count( $this->pRecordedErrors ) > 0)
{
while( ob_get_level () > 0 )
{
ob_end_clean();
}
call_user_func($this->pTraceErrorHandler , $this->pRecordedErrors);
}
}
}
class PageError extends Exception
{
public function __construct($message , $code , $file , $line )
{
parent::__construct($message, $code);
$this->file = $file;
$this->line = $line;
}
}
class ErrorList extends ArrayObject
{
public function __construct()
{
parent::__construct();
}
public function add( Exception $Ex )
{
parent::append ( $Ex );
}
}
$P = Page::getInstance();
echo "Je ne serais pas affiché !";
$a = 2/0;
echo "Je ne serais pas affiché !";
throw new Exception("yopyop");
/*
Affiche
Erreur d'execution :
Message : Division by zero
Code : 2
File : D:\www\blog\class\libs\class.page.php
Line : 144
Trace As String : #0 D:\www\blog\class\libs\class.page.php(144): Page->OnError(2, 'Division by zer...', 'D:\www\blog\cla...', 144, Array) #1 {main}
file : D:\www\blog\class\libs\class.page.php
line : 144
function : OnError
class : Page
type : ->
args : Array
Message : yopyop
Code : 0
File : D:\www\blog\class\libs\class.page.php
Line : 146
Trace As String : #0 {main}
*/
?> |
Partager