2015-08-17 17:00:26 -07:00
< ? php
/**
* Zend Framework ( http :// framework . zend . com / )
*
* @ link http :// github . com / zendframework / zf2 for the canonical source repository
* @ copyright Copyright ( c ) 2005 - 2015 Zend Technologies USA Inc . ( http :// www . zend . com )
* @ license http :// framework . zend . com / license / new - bsd New BSD License
*/
namespace Zend\Stdlib ;
use ErrorException ;
/**
* ErrorHandler that can be used to catch internal PHP errors
* and convert to an ErrorException instance .
*/
abstract class ErrorHandler
{
/**
* Active stack
*
* @ var array
*/
2015-10-08 11:40:12 -07:00
protected static $stack = [];
2015-08-17 17:00:26 -07:00
/**
* Check if this error handler is active
*
* @ return bool
*/
public static function started ()
{
return ( bool ) static :: getNestedLevel ();
}
/**
* Get the current nested level
*
* @ return int
*/
public static function getNestedLevel ()
{
return count ( static :: $stack );
}
/**
* Starting the error handler
*
* @ param int $errorLevel
*/
public static function start ( $errorLevel = \E_WARNING )
{
2017-04-13 15:53:35 +01:00
if ( ! static :: $stack ) {
2015-10-08 11:40:12 -07:00
set_error_handler ([ get_called_class (), 'addError' ], $errorLevel );
2015-08-17 17:00:26 -07:00
}
static :: $stack [] = null ;
}
/**
* Stopping the error handler
*
* @ param bool $throw Throw the ErrorException if any
* @ return null | ErrorException
2018-11-23 12:29:20 +00:00
* @ throws ErrorException If an error has been caught and $throw is true
2015-08-17 17:00:26 -07:00
*/
public static function stop ( $throw = false )
{
$errorException = null ;
if ( static :: $stack ) {
$errorException = array_pop ( static :: $stack );
2017-04-13 15:53:35 +01:00
if ( ! static :: $stack ) {
2015-08-17 17:00:26 -07:00
restore_error_handler ();
}
if ( $errorException && $throw ) {
throw $errorException ;
}
}
return $errorException ;
}
/**
* Stop all active handler
*
* @ return void
*/
public static function clean ()
{
if ( static :: $stack ) {
restore_error_handler ();
}
2015-10-08 11:40:12 -07:00
static :: $stack = [];
2015-08-17 17:00:26 -07:00
}
/**
* Add an error to the stack
*
* @ param int $errno
* @ param string $errstr
* @ param string $errfile
* @ param int $errline
* @ return void
*/
public static function addError ( $errno , $errstr = '' , $errfile = '' , $errline = 0 )
{
$stack = & static :: $stack [ count ( static :: $stack ) - 1 ];
$stack = new ErrorException ( $errstr , 0 , $errno , $errfile , $errline , $stack );
}
}