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