2013-03-20 17:09:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Debug;
|
|
|
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
2013-05-28 18:12:41 +01:00
|
|
|
use Symfony\Component\Debug\Exception\ContextErrorException;
|
|
|
|
use Symfony\Component\Debug\Exception\FatalErrorException;
|
2013-11-28 16:41:31 +00:00
|
|
|
use Symfony\Component\Debug\Exception\DummyException;
|
2013-07-24 05:51:29 +01:00
|
|
|
use Symfony\Component\Debug\FatalErrorHandler\UndefinedFunctionFatalErrorHandler;
|
2013-12-15 00:42:57 +00:00
|
|
|
use Symfony\Component\Debug\FatalErrorHandler\UndefinedMethodFatalErrorHandler;
|
2013-07-24 05:51:29 +01:00
|
|
|
use Symfony\Component\Debug\FatalErrorHandler\ClassNotFoundFatalErrorHandler;
|
2013-07-24 06:23:37 +01:00
|
|
|
use Symfony\Component\Debug\FatalErrorHandler\FatalErrorHandlerInterface;
|
2013-03-20 17:09:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ErrorHandler.
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2013-04-10 06:14:53 +01:00
|
|
|
* @author Konstantin Myakshin <koc-dp@yandex.ru>
|
2014-02-04 10:33:08 +00:00
|
|
|
* @author Nicolas Grekas <p@tchwork.com>
|
2013-03-20 17:09:46 +00:00
|
|
|
*/
|
|
|
|
class ErrorHandler
|
|
|
|
{
|
|
|
|
const TYPE_DEPRECATION = -100;
|
|
|
|
|
|
|
|
private $levels = array(
|
|
|
|
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',
|
|
|
|
E_DEPRECATED => 'Deprecated',
|
|
|
|
E_USER_DEPRECATED => 'User Deprecated',
|
|
|
|
E_ERROR => 'Error',
|
|
|
|
E_CORE_ERROR => 'Core Error',
|
|
|
|
E_COMPILE_ERROR => 'Compile Error',
|
|
|
|
E_PARSE => 'Parse',
|
|
|
|
);
|
|
|
|
|
|
|
|
private $level;
|
|
|
|
|
|
|
|
private $reservedMemory;
|
|
|
|
|
2013-04-10 06:14:53 +01:00
|
|
|
private $displayErrors;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var LoggerInterface[] Loggers for channels
|
|
|
|
*/
|
|
|
|
private static $loggers = array();
|
2013-03-20 17:09:46 +00:00
|
|
|
|
2014-02-04 10:33:08 +00:00
|
|
|
private static $stackedErrors = array();
|
|
|
|
|
|
|
|
private static $stackedErrorLevels = array();
|
|
|
|
|
2013-03-20 17:09:46 +00:00
|
|
|
/**
|
2013-03-21 07:33:19 +00:00
|
|
|
* Registers the error handler.
|
2013-03-20 17:09:46 +00:00
|
|
|
*
|
2013-07-24 06:23:37 +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)
|
2013-11-16 18:00:51 +00:00
|
|
|
* @param Boolean $displayErrors Display errors (for dev environment) or just log them (production usage)
|
2013-03-20 17:09:46 +00:00
|
|
|
*
|
2013-09-19 10:47:13 +01:00
|
|
|
* @return ErrorHandler The registered error handler
|
2013-03-20 17:09:46 +00:00
|
|
|
*/
|
2013-04-10 06:14:53 +01:00
|
|
|
public static function register($level = null, $displayErrors = true)
|
2013-03-20 17:09:46 +00:00
|
|
|
{
|
|
|
|
$handler = new static();
|
|
|
|
$handler->setLevel($level);
|
2013-04-10 06:14:53 +01:00
|
|
|
$handler->setDisplayErrors($displayErrors);
|
2013-03-20 17:09:46 +00:00
|
|
|
|
|
|
|
ini_set('display_errors', 0);
|
|
|
|
set_error_handler(array($handler, 'handle'));
|
|
|
|
register_shutdown_function(array($handler, 'handleFatal'));
|
|
|
|
$handler->reservedMemory = str_repeat('x', 10240);
|
|
|
|
|
|
|
|
return $handler;
|
|
|
|
}
|
|
|
|
|
2013-07-24 06:23:37 +01:00
|
|
|
/**
|
|
|
|
* Sets the level at which the conversion to Exception is done.
|
|
|
|
*
|
|
|
|
* @param integer|null $level The level (null to use the error_reporting() value and 0 to disable)
|
|
|
|
*/
|
2013-03-20 17:09:46 +00:00
|
|
|
public function setLevel($level)
|
|
|
|
{
|
|
|
|
$this->level = null === $level ? error_reporting() : $level;
|
|
|
|
}
|
|
|
|
|
2013-07-24 06:23:37 +01:00
|
|
|
/**
|
|
|
|
* Sets the display_errors flag value.
|
|
|
|
*
|
|
|
|
* @param integer $displayErrors The display_errors flag value
|
|
|
|
*/
|
2013-04-10 06:14:53 +01:00
|
|
|
public function setDisplayErrors($displayErrors)
|
|
|
|
{
|
|
|
|
$this->displayErrors = $displayErrors;
|
|
|
|
}
|
|
|
|
|
2013-07-24 06:23:37 +01:00
|
|
|
/**
|
|
|
|
* Sets a logger for the given channel.
|
|
|
|
*
|
|
|
|
* @param LoggerInterface $logger A logger interface
|
|
|
|
* @param string $channel The channel associated with the logger (deprecation or emergency)
|
|
|
|
*/
|
2013-04-10 06:14:53 +01:00
|
|
|
public static function setLogger(LoggerInterface $logger, $channel = 'deprecation')
|
2013-03-20 17:09:46 +00:00
|
|
|
{
|
2013-04-10 06:14:53 +01:00
|
|
|
self::$loggers[$channel] = $logger;
|
2013-03-20 17:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-31 13:57:00 +01:00
|
|
|
* @throws ContextErrorException When error_reporting returns error
|
2013-03-20 17:09:46 +00:00
|
|
|
*/
|
2013-05-31 13:57:00 +01:00
|
|
|
public function handle($level, $message, $file = 'unknown', $line = 0, $context = array())
|
2013-03-20 17:09:46 +00:00
|
|
|
{
|
|
|
|
if (0 === $this->level) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($level & (E_USER_DEPRECATED | E_DEPRECATED)) {
|
2013-04-10 06:14:53 +01:00
|
|
|
if (isset(self::$loggers['deprecation'])) {
|
2014-02-04 10:33:08 +00:00
|
|
|
if (self::$stackedErrorLevels) {
|
|
|
|
self::$stackedErrors[] = func_get_args();
|
2013-04-07 21:25:23 +01:00
|
|
|
} else {
|
2014-02-04 10:33:08 +00:00
|
|
|
if (version_compare(PHP_VERSION, '5.4', '<')) {
|
|
|
|
$stack = array_map(
|
|
|
|
function ($row) {
|
|
|
|
unset($row['args']);
|
|
|
|
|
|
|
|
return $row;
|
|
|
|
},
|
|
|
|
array_slice(debug_backtrace(false), 0, 10)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$stack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 10);
|
|
|
|
}
|
2013-03-20 17:09:46 +00:00
|
|
|
|
2014-02-04 10:33:08 +00:00
|
|
|
self::$loggers['deprecation']->warning($message, array('type' => self::TYPE_DEPRECATION, 'stack' => $stack));
|
|
|
|
}
|
2013-03-20 17:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-10 06:14:53 +01:00
|
|
|
if ($this->displayErrors && error_reporting() & $level && $this->level & $level) {
|
2013-11-05 20:12:21 +00:00
|
|
|
// Exceptions thrown from error handlers are sometimes not caught by the exception
|
|
|
|
// handler, so we invoke it directly (https://bugs.php.net/bug.php?id=54275)
|
2014-02-04 10:33:08 +00:00
|
|
|
$exceptionHandler = set_exception_handler('var_dump');
|
2013-11-05 20:12:21 +00:00
|
|
|
restore_exception_handler();
|
|
|
|
|
|
|
|
if (is_array($exceptionHandler) && $exceptionHandler[0] instanceof ExceptionHandler) {
|
2014-02-04 10:33:08 +00:00
|
|
|
if (self::$stackedErrorLevels) {
|
|
|
|
self::$stackedErrors[] = func_get_args();
|
2013-11-28 10:16:43 +00:00
|
|
|
|
2014-02-04 10:33:08 +00:00
|
|
|
return true;
|
2013-11-28 16:41:31 +00:00
|
|
|
}
|
|
|
|
|
2014-02-04 10:33:08 +00:00
|
|
|
$exception = sprintf('%s: %s in %s line %d', isset($this->levels[$level]) ? $this->levels[$level] : $level, $message, $file, $line);
|
|
|
|
$exception = new ContextErrorException($exception, 0, $level, $file, $line, $context);
|
|
|
|
$exceptionHandler[0]->handle($exception);
|
|
|
|
|
2013-11-28 16:41:31 +00:00
|
|
|
// we must stop the PHP script execution, as the exception has
|
|
|
|
// already been dealt with, so, let's throw an exception that
|
2013-12-28 10:24:52 +00:00
|
|
|
// will be caught by a dummy exception handler
|
2013-11-28 16:41:31 +00:00
|
|
|
set_exception_handler(function (\Exception $e) use ($exceptionHandler) {
|
|
|
|
if (!$e instanceof DummyException) {
|
2013-12-28 10:24:52 +00:00
|
|
|
// happens if our dummy exception is caught by a
|
2013-11-28 16:41:31 +00:00
|
|
|
// catch-all from user code, in which case, let's the
|
|
|
|
// current handler handle this "new" exception
|
|
|
|
call_user_func($exceptionHandler, $e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
throw new DummyException();
|
2013-11-05 20:12:21 +00:00
|
|
|
}
|
2013-03-20 17:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-04 10:33:08 +00:00
|
|
|
/**
|
|
|
|
* Configure the error handler for delayed handling.
|
|
|
|
* Ensures also that non-catchable fatal errors are never silenced.
|
|
|
|
*
|
|
|
|
* As shown by http://bugs.php.net/42098 and http://bugs.php.net/60724
|
|
|
|
* PHP has a compile stage where it behaves unusually. To workaround it,
|
|
|
|
* we plug an error handler that only stacks errors for later.
|
|
|
|
*
|
|
|
|
* The most important feature of this is to prevent
|
|
|
|
* autoloading until unstackErrors() is called.
|
|
|
|
*/
|
|
|
|
public static function stackErrors()
|
|
|
|
{
|
|
|
|
self::$stackedErrorLevels[] = error_reporting(error_reporting() | E_PARSE | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unstacks stacked errors and forwards to the regular handler
|
|
|
|
*/
|
|
|
|
public static function unstackErrors()
|
|
|
|
{
|
|
|
|
$level = array_pop(self::$stackedErrorLevels);
|
|
|
|
|
|
|
|
if (null !== $level) {
|
|
|
|
error_reporting($level);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty(self::$stackedErrorLevels)) {
|
|
|
|
$errors = self::$stackedErrors;
|
|
|
|
self::$stackedErrors = array();
|
|
|
|
|
|
|
|
$errorHandler = set_error_handler('var_dump');
|
|
|
|
restore_error_handler();
|
|
|
|
|
|
|
|
if ($errorHandler) {
|
|
|
|
foreach ($errors as $e) {
|
|
|
|
call_user_func_array($errorHandler, $e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-20 17:09:46 +00:00
|
|
|
public function handleFatal()
|
|
|
|
{
|
2014-02-04 10:33:08 +00:00
|
|
|
$this->reservedMemory = '';
|
|
|
|
$error = error_get_last();
|
|
|
|
|
|
|
|
while (self::$stackedErrorLevels) {
|
|
|
|
static::unstackErrors();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null === $error) {
|
2013-03-20 17:09:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = $error['type'];
|
|
|
|
if (0 === $this->level || !in_array($type, array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-10 06:14:53 +01:00
|
|
|
if (isset(self::$loggers['emergency'])) {
|
|
|
|
$fatal = array(
|
|
|
|
'type' => $type,
|
|
|
|
'file' => $error['file'],
|
|
|
|
'line' => $error['line'],
|
|
|
|
);
|
|
|
|
|
|
|
|
self::$loggers['emergency']->emerg($error['message'], $fatal);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->displayErrors) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-24 05:59:37 +01:00
|
|
|
// get current exception handler
|
2014-02-04 10:33:08 +00:00
|
|
|
$exceptionHandler = set_exception_handler('var_dump');
|
2013-07-24 05:59:37 +01:00
|
|
|
restore_exception_handler();
|
|
|
|
|
|
|
|
if (is_array($exceptionHandler) && $exceptionHandler[0] instanceof ExceptionHandler) {
|
|
|
|
$this->handleFatalError($exceptionHandler[0], $error);
|
|
|
|
}
|
2013-05-28 18:12:41 +01:00
|
|
|
}
|
|
|
|
|
2013-07-24 06:23:37 +01:00
|
|
|
/**
|
|
|
|
* Gets the fatal error handlers.
|
|
|
|
*
|
|
|
|
* Override this method if you want to define more fatal error handlers.
|
|
|
|
*
|
|
|
|
* @return FatalErrorHandlerInterface[] An array of FatalErrorHandlerInterface
|
|
|
|
*/
|
|
|
|
protected function getFatalErrorHandlers()
|
2013-07-24 05:51:29 +01:00
|
|
|
{
|
|
|
|
return array(
|
|
|
|
new UndefinedFunctionFatalErrorHandler(),
|
2013-12-15 00:42:57 +00:00
|
|
|
new UndefinedMethodFatalErrorHandler(),
|
2013-07-24 05:51:29 +01:00
|
|
|
new ClassNotFoundFatalErrorHandler(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-07-24 05:59:37 +01:00
|
|
|
private function handleFatalError(ExceptionHandler $exceptionHandler, array $error)
|
2013-05-28 18:12:41 +01:00
|
|
|
{
|
2013-07-24 05:59:37 +01:00
|
|
|
$level = isset($this->levels[$error['type']]) ? $this->levels[$error['type']] : $error['type'];
|
|
|
|
$message = sprintf('%s: %s in %s line %d', $level, $error['message'], $error['file'], $error['line']);
|
|
|
|
$exception = new FatalErrorException($message, 0, $error['type'], $error['file'], $error['line']);
|
2013-05-28 18:12:41 +01:00
|
|
|
|
2013-07-24 05:59:37 +01:00
|
|
|
foreach ($this->getFatalErrorHandlers() as $handler) {
|
|
|
|
if ($ex = $handler->handleError($error, $exception)) {
|
|
|
|
return $exceptionHandler->handle($ex);
|
2013-07-24 04:56:44 +01:00
|
|
|
}
|
|
|
|
}
|
2013-07-24 05:59:37 +01:00
|
|
|
|
|
|
|
$exceptionHandler->handle($exception);
|
2013-07-24 04:31:30 +01:00
|
|
|
}
|
2013-03-20 17:09:46 +00:00
|
|
|
}
|