ErrorHandler.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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 Psr\Log\LoggerInterface;
  12. use Psr\Log\LogLevel;
  13. use Symfony\Component\Debug\Exception\ContextErrorException;
  14. use Symfony\Component\Debug\Exception\FatalErrorException;
  15. use Symfony\Component\Debug\Exception\FatalThrowableError;
  16. use Symfony\Component\Debug\Exception\OutOfMemoryException;
  17. use Symfony\Component\Debug\Exception\SilencedErrorContext;
  18. use Symfony\Component\Debug\FatalErrorHandler\ClassNotFoundFatalErrorHandler;
  19. use Symfony\Component\Debug\FatalErrorHandler\FatalErrorHandlerInterface;
  20. use Symfony\Component\Debug\FatalErrorHandler\UndefinedFunctionFatalErrorHandler;
  21. use Symfony\Component\Debug\FatalErrorHandler\UndefinedMethodFatalErrorHandler;
  22. /**
  23. * A generic ErrorHandler for the PHP engine.
  24. *
  25. * Provides five bit fields that control how errors are handled:
  26. * - thrownErrors: errors thrown as \ErrorException
  27. * - loggedErrors: logged errors, when not @-silenced
  28. * - scopedErrors: errors thrown or logged with their local context
  29. * - tracedErrors: errors logged with their stack trace
  30. * - screamedErrors: never @-silenced errors
  31. *
  32. * Each error level can be logged by a dedicated PSR-3 logger object.
  33. * Screaming only applies to logging.
  34. * Throwing takes precedence over logging.
  35. * Uncaught exceptions are logged as E_ERROR.
  36. * E_DEPRECATED and E_USER_DEPRECATED levels never throw.
  37. * E_RECOVERABLE_ERROR and E_USER_ERROR levels always throw.
  38. * Non catchable errors that can be detected at shutdown time are logged when the scream bit field allows so.
  39. * As errors have a performance cost, repeated errors are all logged, so that the developer
  40. * can see them and weight them as more important to fix than others of the same level.
  41. *
  42. * @author Nicolas Grekas <p@tchwork.com>
  43. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  44. */
  45. class ErrorHandler
  46. {
  47. private $levels = array(
  48. E_DEPRECATED => 'Deprecated',
  49. E_USER_DEPRECATED => 'User Deprecated',
  50. E_NOTICE => 'Notice',
  51. E_USER_NOTICE => 'User Notice',
  52. E_STRICT => 'Runtime Notice',
  53. E_WARNING => 'Warning',
  54. E_USER_WARNING => 'User Warning',
  55. E_COMPILE_WARNING => 'Compile Warning',
  56. E_CORE_WARNING => 'Core Warning',
  57. E_USER_ERROR => 'User Error',
  58. E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
  59. E_COMPILE_ERROR => 'Compile Error',
  60. E_PARSE => 'Parse Error',
  61. E_ERROR => 'Error',
  62. E_CORE_ERROR => 'Core Error',
  63. );
  64. private $loggers = array(
  65. E_DEPRECATED => array(null, LogLevel::INFO),
  66. E_USER_DEPRECATED => array(null, LogLevel::INFO),
  67. E_NOTICE => array(null, LogLevel::WARNING),
  68. E_USER_NOTICE => array(null, LogLevel::WARNING),
  69. E_STRICT => array(null, LogLevel::WARNING),
  70. E_WARNING => array(null, LogLevel::WARNING),
  71. E_USER_WARNING => array(null, LogLevel::WARNING),
  72. E_COMPILE_WARNING => array(null, LogLevel::WARNING),
  73. E_CORE_WARNING => array(null, LogLevel::WARNING),
  74. E_USER_ERROR => array(null, LogLevel::CRITICAL),
  75. E_RECOVERABLE_ERROR => array(null, LogLevel::CRITICAL),
  76. E_COMPILE_ERROR => array(null, LogLevel::CRITICAL),
  77. E_PARSE => array(null, LogLevel::CRITICAL),
  78. E_ERROR => array(null, LogLevel::CRITICAL),
  79. E_CORE_ERROR => array(null, LogLevel::CRITICAL),
  80. );
  81. private $thrownErrors = 0x1FFF; // E_ALL - E_DEPRECATED - E_USER_DEPRECATED
  82. private $scopedErrors = 0x1FFF; // E_ALL - E_DEPRECATED - E_USER_DEPRECATED
  83. private $tracedErrors = 0x77FB; // E_ALL - E_STRICT - E_PARSE
  84. private $screamedErrors = 0x55; // E_ERROR + E_CORE_ERROR + E_COMPILE_ERROR + E_PARSE
  85. private $loggedErrors = 0;
  86. private $traceReflector;
  87. private $isRecursive = 0;
  88. private $isRoot = false;
  89. private $exceptionHandler;
  90. private $bootstrappingLogger;
  91. private static $reservedMemory;
  92. private static $stackedErrors = array();
  93. private static $stackedErrorLevels = array();
  94. private static $toStringException = null;
  95. private static $silencedErrorCache = array();
  96. private static $silencedErrorCount = 0;
  97. private static $exitCode = 0;
  98. /**
  99. * Registers the error handler.
  100. *
  101. * @param self|null $handler The handler to register
  102. * @param bool $replace Whether to replace or not any existing handler
  103. *
  104. * @return self The registered error handler
  105. */
  106. public static function register(self $handler = null, $replace = true)
  107. {
  108. if (null === self::$reservedMemory) {
  109. self::$reservedMemory = str_repeat('x', 10240);
  110. register_shutdown_function(__CLASS__.'::handleFatalError');
  111. }
  112. if ($handlerIsNew = null === $handler) {
  113. $handler = new static();
  114. }
  115. if (null === $prev = set_error_handler(array($handler, 'handleError'))) {
  116. restore_error_handler();
  117. // Specifying the error types earlier would expose us to https://bugs.php.net/63206
  118. set_error_handler(array($handler, 'handleError'), $handler->thrownErrors | $handler->loggedErrors);
  119. $handler->isRoot = true;
  120. }
  121. if ($handlerIsNew && \is_array($prev) && $prev[0] instanceof self) {
  122. $handler = $prev[0];
  123. $replace = false;
  124. }
  125. if (!$replace && $prev) {
  126. restore_error_handler();
  127. $handlerIsRegistered = \is_array($prev) && $handler === $prev[0];
  128. } else {
  129. $handlerIsRegistered = true;
  130. }
  131. if (\is_array($prev = set_exception_handler(array($handler, 'handleException'))) && $prev[0] instanceof self) {
  132. restore_exception_handler();
  133. if (!$handlerIsRegistered) {
  134. $handler = $prev[0];
  135. } elseif ($handler !== $prev[0] && $replace) {
  136. set_exception_handler(array($handler, 'handleException'));
  137. $p = $prev[0]->setExceptionHandler(null);
  138. $handler->setExceptionHandler($p);
  139. $prev[0]->setExceptionHandler($p);
  140. }
  141. } else {
  142. $handler->setExceptionHandler($prev);
  143. }
  144. $handler->throwAt(E_ALL & $handler->thrownErrors, true);
  145. return $handler;
  146. }
  147. public function __construct(BufferingLogger $bootstrappingLogger = null)
  148. {
  149. if ($bootstrappingLogger) {
  150. $this->bootstrappingLogger = $bootstrappingLogger;
  151. $this->setDefaultLogger($bootstrappingLogger);
  152. }
  153. $this->traceReflector = new \ReflectionProperty('Exception', 'trace');
  154. $this->traceReflector->setAccessible(true);
  155. }
  156. /**
  157. * Sets a logger to non assigned errors levels.
  158. *
  159. * @param LoggerInterface $logger A PSR-3 logger to put as default for the given levels
  160. * @param array|int $levels An array map of E_* to LogLevel::* or an integer bit field of E_* constants
  161. * @param bool $replace Whether to replace or not any existing logger
  162. */
  163. public function setDefaultLogger(LoggerInterface $logger, $levels = E_ALL, $replace = false)
  164. {
  165. $loggers = array();
  166. if (\is_array($levels)) {
  167. foreach ($levels as $type => $logLevel) {
  168. if (empty($this->loggers[$type][0]) || $replace || $this->loggers[$type][0] === $this->bootstrappingLogger) {
  169. $loggers[$type] = array($logger, $logLevel);
  170. }
  171. }
  172. } else {
  173. if (null === $levels) {
  174. $levels = E_ALL;
  175. }
  176. foreach ($this->loggers as $type => $log) {
  177. if (($type & $levels) && (empty($log[0]) || $replace || $log[0] === $this->bootstrappingLogger)) {
  178. $log[0] = $logger;
  179. $loggers[$type] = $log;
  180. }
  181. }
  182. }
  183. $this->setLoggers($loggers);
  184. }
  185. /**
  186. * Sets a logger for each error level.
  187. *
  188. * @param array $loggers Error levels to [LoggerInterface|null, LogLevel::*] map
  189. *
  190. * @return array The previous map
  191. *
  192. * @throws \InvalidArgumentException
  193. */
  194. public function setLoggers(array $loggers)
  195. {
  196. $prevLogged = $this->loggedErrors;
  197. $prev = $this->loggers;
  198. $flush = array();
  199. foreach ($loggers as $type => $log) {
  200. if (!isset($prev[$type])) {
  201. throw new \InvalidArgumentException('Unknown error type: '.$type);
  202. }
  203. if (!\is_array($log)) {
  204. $log = array($log);
  205. } elseif (!array_key_exists(0, $log)) {
  206. throw new \InvalidArgumentException('No logger provided');
  207. }
  208. if (null === $log[0]) {
  209. $this->loggedErrors &= ~$type;
  210. } elseif ($log[0] instanceof LoggerInterface) {
  211. $this->loggedErrors |= $type;
  212. } else {
  213. throw new \InvalidArgumentException('Invalid logger provided');
  214. }
  215. $this->loggers[$type] = $log + $prev[$type];
  216. if ($this->bootstrappingLogger && $prev[$type][0] === $this->bootstrappingLogger) {
  217. $flush[$type] = $type;
  218. }
  219. }
  220. $this->reRegister($prevLogged | $this->thrownErrors);
  221. if ($flush) {
  222. foreach ($this->bootstrappingLogger->cleanLogs() as $log) {
  223. $type = $log[2]['exception'] instanceof \ErrorException ? $log[2]['exception']->getSeverity() : E_ERROR;
  224. if (!isset($flush[$type])) {
  225. $this->bootstrappingLogger->log($log[0], $log[1], $log[2]);
  226. } elseif ($this->loggers[$type][0]) {
  227. $this->loggers[$type][0]->log($this->loggers[$type][1], $log[1], $log[2]);
  228. }
  229. }
  230. }
  231. return $prev;
  232. }
  233. /**
  234. * Sets a user exception handler.
  235. *
  236. * @param callable $handler A handler that will be called on Exception
  237. *
  238. * @return callable|null The previous exception handler
  239. */
  240. public function setExceptionHandler(callable $handler = null)
  241. {
  242. $prev = $this->exceptionHandler;
  243. $this->exceptionHandler = $handler;
  244. return $prev;
  245. }
  246. /**
  247. * Sets the PHP error levels that throw an exception when a PHP error occurs.
  248. *
  249. * @param int $levels A bit field of E_* constants for thrown errors
  250. * @param bool $replace Replace or amend the previous value
  251. *
  252. * @return int The previous value
  253. */
  254. public function throwAt($levels, $replace = false)
  255. {
  256. $prev = $this->thrownErrors;
  257. $this->thrownErrors = ($levels | E_RECOVERABLE_ERROR | E_USER_ERROR) & ~E_USER_DEPRECATED & ~E_DEPRECATED;
  258. if (!$replace) {
  259. $this->thrownErrors |= $prev;
  260. }
  261. $this->reRegister($prev | $this->loggedErrors);
  262. return $prev;
  263. }
  264. /**
  265. * Sets the PHP error levels for which local variables are preserved.
  266. *
  267. * @param int $levels A bit field of E_* constants for scoped errors
  268. * @param bool $replace Replace or amend the previous value
  269. *
  270. * @return int The previous value
  271. */
  272. public function scopeAt($levels, $replace = false)
  273. {
  274. $prev = $this->scopedErrors;
  275. $this->scopedErrors = (int) $levels;
  276. if (!$replace) {
  277. $this->scopedErrors |= $prev;
  278. }
  279. return $prev;
  280. }
  281. /**
  282. * Sets the PHP error levels for which the stack trace is preserved.
  283. *
  284. * @param int $levels A bit field of E_* constants for traced errors
  285. * @param bool $replace Replace or amend the previous value
  286. *
  287. * @return int The previous value
  288. */
  289. public function traceAt($levels, $replace = false)
  290. {
  291. $prev = $this->tracedErrors;
  292. $this->tracedErrors = (int) $levels;
  293. if (!$replace) {
  294. $this->tracedErrors |= $prev;
  295. }
  296. return $prev;
  297. }
  298. /**
  299. * Sets the error levels where the @-operator is ignored.
  300. *
  301. * @param int $levels A bit field of E_* constants for screamed errors
  302. * @param bool $replace Replace or amend the previous value
  303. *
  304. * @return int The previous value
  305. */
  306. public function screamAt($levels, $replace = false)
  307. {
  308. $prev = $this->screamedErrors;
  309. $this->screamedErrors = (int) $levels;
  310. if (!$replace) {
  311. $this->screamedErrors |= $prev;
  312. }
  313. return $prev;
  314. }
  315. /**
  316. * Re-registers as a PHP error handler if levels changed.
  317. */
  318. private function reRegister($prev)
  319. {
  320. if ($prev !== $this->thrownErrors | $this->loggedErrors) {
  321. $handler = set_error_handler('var_dump');
  322. $handler = \is_array($handler) ? $handler[0] : null;
  323. restore_error_handler();
  324. if ($handler === $this) {
  325. restore_error_handler();
  326. if ($this->isRoot) {
  327. set_error_handler(array($this, 'handleError'), $this->thrownErrors | $this->loggedErrors);
  328. } else {
  329. set_error_handler(array($this, 'handleError'));
  330. }
  331. }
  332. }
  333. }
  334. /**
  335. * Handles errors by filtering then logging them according to the configured bit fields.
  336. *
  337. * @param int $type One of the E_* constants
  338. * @param string $message
  339. * @param string $file
  340. * @param int $line
  341. *
  342. * @return bool Returns false when no handling happens so that the PHP engine can handle the error itself
  343. *
  344. * @throws \ErrorException When $this->thrownErrors requests so
  345. *
  346. * @internal
  347. */
  348. public function handleError($type, $message, $file, $line)
  349. {
  350. // Level is the current error reporting level to manage silent error.
  351. $level = error_reporting();
  352. $silenced = 0 === ($level & $type);
  353. // Strong errors are not authorized to be silenced.
  354. $level |= E_RECOVERABLE_ERROR | E_USER_ERROR | E_DEPRECATED | E_USER_DEPRECATED;
  355. $log = $this->loggedErrors & $type;
  356. $throw = $this->thrownErrors & $type & $level;
  357. $type &= $level | $this->screamedErrors;
  358. if (!$type || (!$log && !$throw)) {
  359. return !$silenced && $type && $log;
  360. }
  361. $scope = $this->scopedErrors & $type;
  362. if (4 < $numArgs = \func_num_args()) {
  363. $context = $scope ? (func_get_arg(4) ?: array()) : array();
  364. $backtrace = 5 < $numArgs ? func_get_arg(5) : null; // defined on HHVM
  365. } else {
  366. $context = array();
  367. $backtrace = null;
  368. }
  369. if (isset($context['GLOBALS']) && $scope) {
  370. $e = $context; // Whatever the signature of the method,
  371. unset($e['GLOBALS'], $context); // $context is always a reference in 5.3
  372. $context = $e;
  373. }
  374. if (null !== $backtrace && $type & E_ERROR) {
  375. // E_ERROR fatal errors are triggered on HHVM when
  376. // hhvm.error_handling.call_user_handler_on_fatals=1
  377. // which is the way to get their backtrace.
  378. $this->handleFatalError(compact('type', 'message', 'file', 'line', 'backtrace'));
  379. return true;
  380. }
  381. $logMessage = $this->levels[$type].': '.$message;
  382. if (null !== self::$toStringException) {
  383. $errorAsException = self::$toStringException;
  384. self::$toStringException = null;
  385. } elseif (!$throw && !($type & $level)) {
  386. if (!isset(self::$silencedErrorCache[$id = $file.':'.$line])) {
  387. $lightTrace = $this->tracedErrors & $type ? $this->cleanTrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3), $type, $file, $line, false) : array();
  388. $errorAsException = new SilencedErrorContext($type, $file, $line, $lightTrace);
  389. } elseif (isset(self::$silencedErrorCache[$id][$message])) {
  390. $lightTrace = null;
  391. $errorAsException = self::$silencedErrorCache[$id][$message];
  392. ++$errorAsException->count;
  393. } else {
  394. $lightTrace = array();
  395. $errorAsException = null;
  396. }
  397. if (100 < ++self::$silencedErrorCount) {
  398. self::$silencedErrorCache = $lightTrace = array();
  399. self::$silencedErrorCount = 1;
  400. }
  401. if ($errorAsException) {
  402. self::$silencedErrorCache[$id][$message] = $errorAsException;
  403. }
  404. if (null === $lightTrace) {
  405. return;
  406. }
  407. } else {
  408. if ($scope) {
  409. $errorAsException = new ContextErrorException($logMessage, 0, $type, $file, $line, $context);
  410. } else {
  411. $errorAsException = new \ErrorException($logMessage, 0, $type, $file, $line);
  412. }
  413. // Clean the trace by removing function arguments and the first frames added by the error handler itself.
  414. if ($throw || $this->tracedErrors & $type) {
  415. $backtrace = $backtrace ?: $errorAsException->getTrace();
  416. $lightTrace = $this->cleanTrace($backtrace, $type, $file, $line, $throw);
  417. $this->traceReflector->setValue($errorAsException, $lightTrace);
  418. } else {
  419. $this->traceReflector->setValue($errorAsException, array());
  420. }
  421. }
  422. if ($throw) {
  423. if (E_USER_ERROR & $type) {
  424. for ($i = 1; isset($backtrace[$i]); ++$i) {
  425. if (isset($backtrace[$i]['function'], $backtrace[$i]['type'], $backtrace[$i - 1]['function'])
  426. && '__toString' === $backtrace[$i]['function']
  427. && '->' === $backtrace[$i]['type']
  428. && !isset($backtrace[$i - 1]['class'])
  429. && ('trigger_error' === $backtrace[$i - 1]['function'] || 'user_error' === $backtrace[$i - 1]['function'])
  430. ) {
  431. // Here, we know trigger_error() has been called from __toString().
  432. // HHVM is fine with throwing from __toString() but PHP triggers a fatal error instead.
  433. // A small convention allows working around the limitation:
  434. // given a caught $e exception in __toString(), quitting the method with
  435. // `return trigger_error($e, E_USER_ERROR);` allows this error handler
  436. // to make $e get through the __toString() barrier.
  437. foreach ($context as $e) {
  438. if (($e instanceof \Exception || $e instanceof \Throwable) && $e->__toString() === $message) {
  439. if (1 === $i) {
  440. // On HHVM
  441. $errorAsException = $e;
  442. break;
  443. }
  444. self::$toStringException = $e;
  445. return true;
  446. }
  447. }
  448. if (1 < $i) {
  449. // On PHP (not on HHVM), display the original error message instead of the default one.
  450. $this->handleException($errorAsException);
  451. // Stop the process by giving back the error to the native handler.
  452. return false;
  453. }
  454. }
  455. }
  456. }
  457. throw $errorAsException;
  458. }
  459. if ($this->isRecursive) {
  460. $log = 0;
  461. } elseif (self::$stackedErrorLevels) {
  462. self::$stackedErrors[] = array(
  463. $this->loggers[$type][0],
  464. ($type & $level) ? $this->loggers[$type][1] : LogLevel::DEBUG,
  465. $logMessage,
  466. $errorAsException ? array('exception' => $errorAsException) : array(),
  467. );
  468. } else {
  469. try {
  470. $this->isRecursive = true;
  471. $level = ($type & $level) ? $this->loggers[$type][1] : LogLevel::DEBUG;
  472. $this->loggers[$type][0]->log($level, $logMessage, $errorAsException ? array('exception' => $errorAsException) : array());
  473. } finally {
  474. $this->isRecursive = false;
  475. }
  476. }
  477. return !$silenced && $type && $log;
  478. }
  479. /**
  480. * Handles an exception by logging then forwarding it to another handler.
  481. *
  482. * @param \Exception|\Throwable $exception An exception to handle
  483. * @param array $error An array as returned by error_get_last()
  484. *
  485. * @internal
  486. */
  487. public function handleException($exception, array $error = null)
  488. {
  489. if (null === $error) {
  490. self::$exitCode = 255;
  491. }
  492. if (!$exception instanceof \Exception) {
  493. $exception = new FatalThrowableError($exception);
  494. }
  495. $type = $exception instanceof FatalErrorException ? $exception->getSeverity() : E_ERROR;
  496. $handlerException = null;
  497. if (($this->loggedErrors & $type) || $exception instanceof FatalThrowableError) {
  498. if ($exception instanceof FatalErrorException) {
  499. if ($exception instanceof FatalThrowableError) {
  500. $error = array(
  501. 'type' => $type,
  502. 'message' => $message = $exception->getMessage(),
  503. 'file' => $exception->getFile(),
  504. 'line' => $exception->getLine(),
  505. );
  506. } else {
  507. $message = 'Fatal '.$exception->getMessage();
  508. }
  509. } elseif ($exception instanceof \ErrorException) {
  510. $message = 'Uncaught '.$exception->getMessage();
  511. } else {
  512. $message = 'Uncaught Exception: '.$exception->getMessage();
  513. }
  514. }
  515. if ($this->loggedErrors & $type) {
  516. try {
  517. $this->loggers[$type][0]->log($this->loggers[$type][1], $message, array('exception' => $exception));
  518. } catch (\Exception $handlerException) {
  519. } catch (\Throwable $handlerException) {
  520. }
  521. }
  522. if ($exception instanceof FatalErrorException && !$exception instanceof OutOfMemoryException && $error) {
  523. foreach ($this->getFatalErrorHandlers() as $handler) {
  524. if ($e = $handler->handleError($error, $exception)) {
  525. $exception = $e;
  526. break;
  527. }
  528. }
  529. }
  530. $exceptionHandler = $this->exceptionHandler;
  531. $this->exceptionHandler = null;
  532. try {
  533. if (null !== $exceptionHandler) {
  534. return \call_user_func($exceptionHandler, $exception);
  535. }
  536. $handlerException = $handlerException ?: $exception;
  537. } catch (\Exception $handlerException) {
  538. } catch (\Throwable $handlerException) {
  539. }
  540. if ($exception === $handlerException) {
  541. self::$reservedMemory = null; // Disable the fatal error handler
  542. throw $exception; // Give back $exception to the native handler
  543. }
  544. $this->handleException($handlerException);
  545. }
  546. /**
  547. * Shutdown registered function for handling PHP fatal errors.
  548. *
  549. * @param array $error An array as returned by error_get_last()
  550. *
  551. * @internal
  552. */
  553. public static function handleFatalError(array $error = null)
  554. {
  555. if (null === self::$reservedMemory) {
  556. return;
  557. }
  558. $handler = self::$reservedMemory = null;
  559. $handlers = array();
  560. $previousHandler = null;
  561. $sameHandlerLimit = 10;
  562. while (!\is_array($handler) || !$handler[0] instanceof self) {
  563. $handler = set_exception_handler('var_dump');
  564. restore_exception_handler();
  565. if (!$handler) {
  566. break;
  567. }
  568. restore_exception_handler();
  569. if ($handler !== $previousHandler) {
  570. array_unshift($handlers, $handler);
  571. $previousHandler = $handler;
  572. } elseif (0 === --$sameHandlerLimit) {
  573. $handler = null;
  574. break;
  575. }
  576. }
  577. foreach ($handlers as $h) {
  578. set_exception_handler($h);
  579. }
  580. if (!$handler) {
  581. return;
  582. }
  583. if ($handler !== $h) {
  584. $handler[0]->setExceptionHandler($h);
  585. }
  586. $handler = $handler[0];
  587. $handlers = array();
  588. if ($exit = null === $error) {
  589. $error = error_get_last();
  590. }
  591. try {
  592. while (self::$stackedErrorLevels) {
  593. static::unstackErrors();
  594. }
  595. } catch (\Exception $exception) {
  596. // Handled below
  597. } catch (\Throwable $exception) {
  598. // Handled below
  599. }
  600. if ($error && $error['type'] &= E_PARSE | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR) {
  601. // Let's not throw anymore but keep logging
  602. $handler->throwAt(0, true);
  603. $trace = isset($error['backtrace']) ? $error['backtrace'] : null;
  604. if (0 === strpos($error['message'], 'Allowed memory') || 0 === strpos($error['message'], 'Out of memory')) {
  605. $exception = new OutOfMemoryException($handler->levels[$error['type']].': '.$error['message'], 0, $error['type'], $error['file'], $error['line'], 2, false, $trace);
  606. } else {
  607. $exception = new FatalErrorException($handler->levels[$error['type']].': '.$error['message'], 0, $error['type'], $error['file'], $error['line'], 2, true, $trace);
  608. }
  609. }
  610. try {
  611. if (isset($exception)) {
  612. self::$exitCode = 255;
  613. $handler->handleException($exception, $error);
  614. }
  615. } catch (FatalErrorException $e) {
  616. // Ignore this re-throw
  617. }
  618. if ($exit && self::$exitCode) {
  619. $exitCode = self::$exitCode;
  620. register_shutdown_function('register_shutdown_function', function () use ($exitCode) { exit($exitCode); });
  621. }
  622. }
  623. /**
  624. * Configures the error handler for delayed handling.
  625. * Ensures also that non-catchable fatal errors are never silenced.
  626. *
  627. * As shown by http://bugs.php.net/42098 and http://bugs.php.net/60724
  628. * PHP has a compile stage where it behaves unusually. To workaround it,
  629. * we plug an error handler that only stacks errors for later.
  630. *
  631. * The most important feature of this is to prevent
  632. * autoloading until unstackErrors() is called.
  633. *
  634. * @deprecated since version 3.4, to be removed in 4.0.
  635. */
  636. public static function stackErrors()
  637. {
  638. @trigger_error('Support for stacking errors is deprecated since Symfony 3.4 and will be removed in 4.0.', E_USER_DEPRECATED);
  639. self::$stackedErrorLevels[] = error_reporting(error_reporting() | E_PARSE | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR);
  640. }
  641. /**
  642. * Unstacks stacked errors and forwards to the logger.
  643. *
  644. * @deprecated since version 3.4, to be removed in 4.0.
  645. */
  646. public static function unstackErrors()
  647. {
  648. @trigger_error('Support for unstacking errors is deprecated since Symfony 3.4 and will be removed in 4.0.', E_USER_DEPRECATED);
  649. $level = array_pop(self::$stackedErrorLevels);
  650. if (null !== $level) {
  651. $errorReportingLevel = error_reporting($level);
  652. if ($errorReportingLevel !== ($level | E_PARSE | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR)) {
  653. // If the user changed the error level, do not overwrite it
  654. error_reporting($errorReportingLevel);
  655. }
  656. }
  657. if (empty(self::$stackedErrorLevels)) {
  658. $errors = self::$stackedErrors;
  659. self::$stackedErrors = array();
  660. foreach ($errors as $error) {
  661. $error[0]->log($error[1], $error[2], $error[3]);
  662. }
  663. }
  664. }
  665. /**
  666. * Gets the fatal error handlers.
  667. *
  668. * Override this method if you want to define more fatal error handlers.
  669. *
  670. * @return FatalErrorHandlerInterface[] An array of FatalErrorHandlerInterface
  671. */
  672. protected function getFatalErrorHandlers()
  673. {
  674. return array(
  675. new UndefinedFunctionFatalErrorHandler(),
  676. new UndefinedMethodFatalErrorHandler(),
  677. new ClassNotFoundFatalErrorHandler(),
  678. );
  679. }
  680. private function cleanTrace($backtrace, $type, $file, $line, $throw)
  681. {
  682. $lightTrace = $backtrace;
  683. for ($i = 0; isset($backtrace[$i]); ++$i) {
  684. if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) {
  685. $lightTrace = \array_slice($lightTrace, 1 + $i);
  686. break;
  687. }
  688. }
  689. if (!($throw || $this->scopedErrors & $type)) {
  690. for ($i = 0; isset($lightTrace[$i]); ++$i) {
  691. unset($lightTrace[$i]['args'], $lightTrace[$i]['object']);
  692. }
  693. }
  694. return $lightTrace;
  695. }
  696. }