This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Debug/ErrorHandler.php

346 lines
11 KiB
PHP
Raw Normal View History

<?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\LogLevel;
use Psr\Log\LoggerInterface;
use Symfony\Component\Debug\Exception\FatalErrorException;
use Symfony\Component\Debug\Exception\DummyException;
2014-04-17 08:44:42 +01:00
use Symfony\Component\Debug\Exception\HandledErrorException;
use Symfony\Component\Debug\FatalErrorHandler\UndefinedFunctionFatalErrorHandler;
use Symfony\Component\Debug\FatalErrorHandler\UndefinedMethodFatalErrorHandler;
use Symfony\Component\Debug\FatalErrorHandler\ClassNotFoundFatalErrorHandler;
2013-07-24 06:23:37 +01:00
use Symfony\Component\Debug\FatalErrorHandler\FatalErrorHandlerInterface;
/**
* ErrorHandler.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Konstantin Myakshin <koc-dp@yandex.ru>
* @author Nicolas Grekas <p@tchwork.com>
*/
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 Error',
);
private $level;
private $reservedMemory;
private $displayErrors;
/**
* @var LoggerInterface[] Loggers for channels
*/
private static $loggers = array();
private static $stackedErrors = array();
private static $stackedErrorLevels = array();
2014-04-17 08:44:42 +01:00
private static $fatalHandler = false;
/**
* Registers the error handler.
*
Merge branch '2.3' into 2.4 * 2.3: made {@inheritdoc} annotations consistent across the board fixed types in phpdocs made phpdoc types consistent with those defined in Hack Add support Thai translations made types consistent with those defined in Hack removed extra/unsupported arguments [HttpKernel] fixed an error message [TwigBundle] removed undefined argument [Translation] Make IcuDatFileLoader/IcuResFileLoader::load invalid resource compatible with HHVM. Conflicts: src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/Fixtures/php/lazy_service.php src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php src/Symfony/Bundle/FrameworkBundle/Templating/Loader/FilesystemLoader.php src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php src/Symfony/Component/Config/Definition/ReferenceDumper.php src/Symfony/Component/Console/Helper/DescriptorHelper.php src/Symfony/Component/Debug/ErrorHandler.php src/Symfony/Component/Finder/Tests/Iterator/RecursiveDirectoryIteratorTest.php src/Symfony/Component/Form/Extension/Core/DataTransformer/IntegerToLocalizedStringTransformer.php src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php src/Symfony/Component/HttpFoundation/Response.php src/Symfony/Component/HttpFoundation/StreamedResponse.php src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php src/Symfony/Component/HttpKernel/EventListener/ProfilerListener.php src/Symfony/Component/HttpKernel/Fragment/FragmentHandler.php src/Symfony/Component/HttpKernel/Fragment/RoutableFragmentRenderer.php src/Symfony/Component/HttpKernel/Kernel.php src/Symfony/Component/HttpKernel/Tests/Fixtures/KernelForTest.php src/Symfony/Component/Intl/NumberFormatter/NumberFormatter.php src/Symfony/Component/Security/Core/Authorization/AccessDecisionManager.php src/Symfony/Component/Stopwatch/StopwatchPeriod.php src/Symfony/Component/Translation/TranslatorInterface.php src/Symfony/Component/Validator/ConstraintValidatorFactory.php
2014-04-16 09:02:57 +01:00
* @param int $level The level at which the conversion to Exception is done (null to use the error_reporting() value and 0 to disable)
* @param bool $displayErrors Display errors (for dev environment) or just log them (production usage)
*
2013-09-19 10:47:13 +01:00
* @return ErrorHandler The registered error handler
*/
public static function register($level = null, $displayErrors = true)
{
$handler = new static();
$handler->setLevel($level);
$handler->setDisplayErrors($displayErrors);
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 int|null $level The level (null to use the error_reporting() value and 0 to disable)
2013-07-24 06:23:37 +01: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 int $displayErrors The display_errors flag value
2013-07-24 06:23:37 +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, emergency or scream)
2013-07-24 06:23:37 +01:00
*/
public static function setLogger(LoggerInterface $logger, $channel = 'deprecation')
{
self::$loggers[$channel] = $logger;
}
/**
2014-04-17 08:44:42 +01:00
* Sets a fatal error exception handler.
*
* @param callable $handler An handler that will be called on FatalErrorException
*/
public static function setFatalErrorExceptionHandler($handler)
{
self::$fatalHandler = $handler;
}
/**
* @throws HandledErrorException When error_reporting returns error
*/
2013-05-31 13:57:00 +01:00
public function handle($level, $message, $file = 'unknown', $line = 0, $context = array())
{
if ($level & (E_USER_DEPRECATED | E_DEPRECATED)) {
if (isset(self::$loggers['deprecation'])) {
if (self::$stackedErrorLevels) {
self::$stackedErrors[] = func_get_args();
} else {
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);
}
self::$loggers['deprecation']->warning($message, array('type' => self::TYPE_DEPRECATION, 'stack' => $stack));
}
}
return true;
}
if ($this->displayErrors && error_reporting() & $level && $this->level & $level) {
// 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)
$exceptionHandler = set_exception_handler('var_dump');
restore_exception_handler();
2014-04-17 08:44:42 +01:00
if ($exceptionHandler) {
if (self::$stackedErrorLevels) {
self::$stackedErrors[] = func_get_args();
2013-11-28 10:16:43 +00:00
return true;
}
$exception = sprintf('%s: %s in %s line %d', isset($this->levels[$level]) ? $this->levels[$level] : $level, $message, $file, $line);
2014-04-17 08:44:42 +01:00
$exception = new HandledErrorException($exception, 0, $level, $file, $line, $context);
$exception->handleWith($exceptionHandler);
// 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
set_exception_handler(function (\Exception $e) use ($exceptionHandler) {
2014-04-17 08:44:42 +01:00
if (!$e instanceof HandledErrorException && !$e instanceof DummyException) {
// happens if our handled exception is caught by a
// catch-all from user code, in which case, let the
// current handler handle this "new" exception
call_user_func($exceptionHandler, $e);
}
});
2014-04-17 08:44:42 +01:00
throw $exception;
}
}
if (isset(self::$loggers['scream']) && !(error_reporting() & $level)) {
if (self::$stackedErrorLevels) {
self::$stackedErrors[] = func_get_args();
} else {
switch ($level) {
case E_USER_ERROR:
case E_RECOVERABLE_ERROR:
$logLevel = LogLevel::ERROR;
break;
case E_WARNING:
case E_USER_WARNING:
$logLevel = LogLevel::WARNING;
break;
default:
$logLevel = LogLevel::NOTICE;
break;
}
self::$loggers['scream']->log($logLevel, $message, array(
'type' => $level,
'file' => $file,
'line' => $line,
'scream' => error_reporting(),
));
}
}
return false;
}
/**
* 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);
}
}
}
}
public function handleFatal()
{
$this->reservedMemory = '';
2014-04-17 08:44:42 +01:00
gc_collect_cycles();
$error = error_get_last();
while (self::$stackedErrorLevels) {
static::unstackErrors();
}
if (null === $error) {
return;
}
$type = $error['type'];
if (0 === $this->level || !in_array($type, array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE))) {
return;
}
if (isset(self::$loggers['emergency'])) {
$fatal = array(
'type' => $type,
'file' => $error['file'],
'line' => $error['line'],
);
self::$loggers['emergency']->emergency($error['message'], $fatal);
}
2014-04-17 08:44:42 +01:00
if ($this->displayErrors) {
// get current exception handler
$exceptionHandler = set_exception_handler('var_dump');
restore_exception_handler();
2013-07-24 05:59:37 +01:00
2014-04-17 08:44:42 +01:00
if ($exceptionHandler || self::$fatalHandler) {
$this->handleFatalError($exceptionHandler, $error);
}
2013-07-24 05:59:37 +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()
{
return array(
new UndefinedFunctionFatalErrorHandler(),
new UndefinedMethodFatalErrorHandler(),
new ClassNotFoundFatalErrorHandler(),
);
}
2014-04-17 08:44:42 +01:00
private function handleFatalError($exceptionHandler, array $error)
{
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']);
2014-04-17 08:44:42 +01:00
$exception = new FatalErrorException($message, 0, $error['type'], $error['file'], $error['line'], 3);
2013-07-24 05:59:37 +01:00
foreach ($this->getFatalErrorHandlers() as $handler) {
if ($ex = $handler->handleError($error, $exception)) {
2014-04-17 08:44:42 +01:00
$exception = $ex;
break;
}
}
2013-07-24 05:59:37 +01:00
2014-04-17 08:44:42 +01:00
if ($exceptionHandler) {
$exception->handleWith($exceptionHandler);
}
if (self::$fatalHandler) {
call_user_func(self::$fatalHandler, $exception);
}
}
}