2010-02-17 13:54:36 +00:00
< ? php
/*
2011-01-15 13:29:43 +00:00
* This file is part of the Symfony package .
2010-02-17 13:54:36 +00:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-02-17 13:54:36 +00:00
*
2011-01-15 13:29:43 +00:00
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
2010-02-17 13:54:36 +00:00
*/
2011-01-15 13:29:43 +00:00
namespace Symfony\Component\HttpKernel\Debug ;
2010-02-17 13:54:36 +00:00
/**
2010-04-07 01:51:29 +01:00
* ErrorHandler .
2010-02-17 13:54:36 +00:00
*
2011-03-06 11:40:06 +00:00
* @ author Fabien Potencier < fabien @ symfony . com >
2010-02-17 13:54:36 +00:00
*/
class ErrorHandler
{
2011-03-23 18:47:16 +00:00
private $levels = array (
2010-05-06 12:25:53 +01:00
E_WARNING => 'Warning' ,
E_NOTICE => 'Notice' ,
E_USER_ERROR => 'User Error' ,
E_USER_WARNING => 'User Warning' ,
E_USER_NOTICE => 'User Notice' ,
E_STRICT => 'Runtime Notice' ,
E_RECOVERABLE_ERROR => 'Catchable Fatal Error' ,
2012-04-24 11:21:59 +01:00
E_DEPRECATED => 'Deprecated' ,
E_USER_DEPRECATED => 'User Deprecated' ,
2010-05-06 12:25:53 +01:00
);
2010-02-17 13:54:36 +00:00
2011-03-23 18:47:16 +00:00
private $level ;
2010-02-17 13:54:36 +00:00
2010-05-06 12:25:53 +01:00
/**
2011-05-05 07:46:20 +01:00
* Register the error handler .
2010-05-06 12:25:53 +01:00
*
* @ param integer $level The level at which the conversion to Exception is done ( null to use the error_reporting () value and 0 to disable )
2011-05-05 07:46:20 +01:00
*
* @ return The registered error handler
2010-05-06 12:25:53 +01:00
*/
2012-07-09 13:50:58 +01:00
public static function register ( $level = null )
2010-02-17 13:54:36 +00:00
{
2011-05-05 07:46:20 +01:00
$handler = new static ();
$handler -> setLevel ( $level );
set_error_handler ( array ( $handler , 'handle' ));
return $handler ;
2010-02-17 13:54:36 +00:00
}
2011-05-05 07:46:20 +01:00
public function setLevel ( $level )
2010-02-17 13:54:36 +00:00
{
2011-05-05 07:46:20 +01:00
$this -> level = null === $level ? error_reporting () : $level ;
2010-02-17 13:54:36 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* @ throws \ErrorException When error_reporting returns error
*/
public function handle ( $level , $message , $file , $line , $context )
{
2010-05-07 15:09:11 +01:00
if ( 0 === $this -> level ) {
2010-05-06 12:25:53 +01:00
return false ;
}
2010-05-07 15:09:11 +01:00
if ( error_reporting () & $level && $this -> level & $level ) {
2012-06-25 21:04:54 +01:00
throw new \ErrorException ( sprintf ( '%s: %s in %s line %d' , isset ( $this -> levels [ $level ]) ? $this -> levels [ $level ] : $level , $message , $file , $line ), 0 , $level , $file , $line );
2010-05-06 12:25:53 +01:00
}
return false ;
}
2010-02-17 13:54:36 +00:00
}