/vendor/symfony/debug/Symfony/Component/Debug/ErrorHandler.php

https://bitbucket.org/larryg/powerhut · PHP · 165 lines · 108 code · 28 blank · 29 comment · 15 complexity · fd16a01cbab969aafd27e7b94fbad52f MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Debug;
  11. use Symfony\Component\Debug\Exception\FatalErrorException;
  12. use Symfony\Component\Debug\Exception\ContextErrorException;
  13. use Psr\Log\LoggerInterface;
  14. /**
  15. * ErrorHandler.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. * @author Konstantin Myakshin <koc-dp@yandex.ru>
  19. */
  20. class ErrorHandler
  21. {
  22. const TYPE_DEPRECATION = -100;
  23. private $levels = array(
  24. E_WARNING => 'Warning',
  25. E_NOTICE => 'Notice',
  26. E_USER_ERROR => 'User Error',
  27. E_USER_WARNING => 'User Warning',
  28. E_USER_NOTICE => 'User Notice',
  29. E_STRICT => 'Runtime Notice',
  30. E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
  31. E_DEPRECATED => 'Deprecated',
  32. E_USER_DEPRECATED => 'User Deprecated',
  33. E_ERROR => 'Error',
  34. E_CORE_ERROR => 'Core Error',
  35. E_COMPILE_ERROR => 'Compile Error',
  36. E_PARSE => 'Parse',
  37. );
  38. private $level;
  39. private $reservedMemory;
  40. private $displayErrors;
  41. /**
  42. * @var LoggerInterface[] Loggers for channels
  43. */
  44. private static $loggers = array();
  45. /**
  46. * Registers the error handler.
  47. *
  48. * @param integer $level The level at which the conversion to Exception is done (null to use the error_reporting() value and 0 to disable)
  49. * @param Boolean $displayErrors Display errors (for dev environment) or just log they (production usage)
  50. *
  51. * @return The registered error handler
  52. */
  53. public static function register($level = null, $displayErrors = true)
  54. {
  55. $handler = new static();
  56. $handler->setLevel($level);
  57. $handler->setDisplayErrors($displayErrors);
  58. ini_set('display_errors', 0);
  59. set_error_handler(array($handler, 'handle'));
  60. register_shutdown_function(array($handler, 'handleFatal'));
  61. $handler->reservedMemory = str_repeat('x', 10240);
  62. return $handler;
  63. }
  64. public function setLevel($level)
  65. {
  66. $this->level = null === $level ? error_reporting() : $level;
  67. }
  68. public function setDisplayErrors($displayErrors)
  69. {
  70. $this->displayErrors = $displayErrors;
  71. }
  72. public static function setLogger(LoggerInterface $logger, $channel = 'deprecation')
  73. {
  74. self::$loggers[$channel] = $logger;
  75. }
  76. /**
  77. * @throws ContextErrorException When error_reporting returns error
  78. */
  79. public function handle($level, $message, $file = 'unknown', $line = 0, $context = array())
  80. {
  81. if (0 === $this->level) {
  82. return false;
  83. }
  84. if ($level & (E_USER_DEPRECATED | E_DEPRECATED)) {
  85. if (isset(self::$loggers['deprecation'])) {
  86. if (version_compare(PHP_VERSION, '5.4', '<')) {
  87. $stack = array_map(
  88. function ($row) {
  89. unset($row['args']);
  90. return $row;
  91. },
  92. array_slice(debug_backtrace(false), 0, 10)
  93. );
  94. } else {
  95. $stack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 10);
  96. }
  97. self::$loggers['deprecation']->warning($message, array('type' => self::TYPE_DEPRECATION, 'stack' => $stack));
  98. }
  99. return true;
  100. }
  101. if ($this->displayErrors && error_reporting() & $level && $this->level & $level) {
  102. throw new ContextErrorException(sprintf('%s: %s in %s line %d', isset($this->levels[$level]) ? $this->levels[$level] : $level, $message, $file, $line), 0, $level, $file, $line, $context);
  103. }
  104. return false;
  105. }
  106. public function handleFatal()
  107. {
  108. if (null === $error = error_get_last()) {
  109. return;
  110. }
  111. unset($this->reservedMemory);
  112. $type = $error['type'];
  113. if (0 === $this->level || !in_array($type, array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE))) {
  114. return;
  115. }
  116. if (isset(self::$loggers['emergency'])) {
  117. $fatal = array(
  118. 'type' => $type,
  119. 'file' => $error['file'],
  120. 'line' => $error['line'],
  121. );
  122. self::$loggers['emergency']->emerg($error['message'], $fatal);
  123. }
  124. if (!$this->displayErrors) {
  125. return;
  126. }
  127. // get current exception handler
  128. $exceptionHandler = set_exception_handler(function() {});
  129. restore_exception_handler();
  130. if (is_array($exceptionHandler) && $exceptionHandler[0] instanceof ExceptionHandler) {
  131. $level = isset($this->levels[$type]) ? $this->levels[$type] : $type;
  132. $message = sprintf('%s: %s in %s line %d', $level, $error['message'], $error['file'], $error['line']);
  133. $exception = new FatalErrorException($message, 0, $type, $error['file'], $error['line']);
  134. $exceptionHandler[0]->handle($exception);
  135. }
  136. }
  137. }