ErrorHandler.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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\LogLevel;
  12. use Psr\Log\LoggerInterface;
  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\UndefinedFunctionFatalErrorHandler;
  19. use Symfony\Component\Debug\FatalErrorHandler\UndefinedMethodFatalErrorHandler;
  20. use Symfony\Component\Debug\FatalErrorHandler\ClassNotFoundFatalErrorHandler;
  21. use Symfony\Component\Debug\FatalErrorHandler\FatalErrorHandlerInterface;
  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. /**
  96. * Registers the error handler.
  97. *
  98. * @param self|null $handler The handler to register
  99. * @param bool $replace Whether to replace or not any existing handler
  100. *
  101. * @return self The registered error handler
  102. */
  103. public static function register(self $handler = null, $replace = true)
  104. {
  105. if (null === self::$reservedMemory) {
  106. self::$reservedMemory = str_repeat('x', 10240);
  107. register_shutdown_function(__CLASS__.'::handleFatalError');
  108. }
  109. if ($handlerIsNew = null === $handler) {
  110. $handler = new static();
  111. }
  112. if (null === $prev = set_error_handler(array($handler, 'handleError'))) {
  113. restore_error_handler();
  114. // Specifying the error types earlier would expose us to https://bugs.php.net/63206
  115. set_error_handler(array($handler, 'handleError'), $handler->thrownErrors | $handler->loggedErrors);
  116. $handler->isRoot = true;
  117. }
  118. if ($handlerIsNew && is_array($prev) && $prev[0] instanceof self) {
  119. $handler = $prev[0];
  120. $replace = false;
  121. }
  122. if ($replace || !$prev) {
  123. $handler->setExceptionHandler(set_exception_handler(array($handler, 'handleException')));
  124. } else {
  125. restore_error_handler();
  126. }
  127. $handler->throwAt(E_ALL & $handler->thrownErrors, true);
  128. return $handler;
  129. }
  130. public function __construct(BufferingLogger $bootstrappingLogger = null)
  131. {
  132. if ($bootstrappingLogger) {
  133. $this->bootstrappingLogger = $bootstrappingLogger;
  134. $this->setDefaultLogger($bootstrappingLogger);
  135. }
  136. $this->traceReflector = new \ReflectionProperty('Exception', 'trace');
  137. $this->traceReflector->setAccessible(true);
  138. }
  139. /**
  140. * Sets a logger to non assigned errors levels.
  141. *
  142. * @param LoggerInterface $logger A PSR-3 logger to put as default for the given levels
  143. * @param array|int $levels An array map of E_* to LogLevel::* or an integer bit field of E_* constants
  144. * @param bool $replace Whether to replace or not any existing logger
  145. */
  146. public function setDefaultLogger(LoggerInterface $logger, $levels = E_ALL, $replace = false)
  147. {
  148. $loggers = array();
  149. if (is_array($levels)) {
  150. foreach ($levels as $type => $logLevel) {
  151. if (empty($this->loggers[$type][0]) || $replace || $this->loggers[$type][0] === $this->bootstrappingLogger) {
  152. $loggers[$type] = array($logger, $logLevel);
  153. }
  154. }
  155. } else {
  156. if (null === $levels) {
  157. $levels = E_ALL;
  158. }
  159. foreach ($this->loggers as $type => $log) {
  160. if (($type & $levels) && (empty($log[0]) || $replace || $log[0] === $this->bootstrappingLogger)) {
  161. $log[0] = $logger;
  162. $loggers[$type] = $log;
  163. }
  164. }
  165. }
  166. $this->setLoggers($loggers);
  167. }
  168. /**
  169. * Sets a logger for each error level.
  170. *
  171. * @param array $loggers Error levels to [LoggerInterface|null, LogLevel::*] map
  172. *
  173. * @return array The previous map
  174. *
  175. * @throws \InvalidArgumentException
  176. */
  177. public function setLoggers(array $loggers)
  178. {
  179. $prevLogged = $this->loggedErrors;
  180. $prev = $this->loggers;
  181. $flush = array();
  182. foreach ($loggers as $type => $log) {
  183. if (!isset($prev[$type])) {
  184. throw new \InvalidArgumentException('Unknown error type: '.$type);
  185. }
  186. if (!is_array($log)) {
  187. $log = array($log);
  188. } elseif (!array_key_exists(0, $log)) {
  189. throw new \InvalidArgumentException('No logger provided');
  190. }
  191. if (null === $log[0]) {
  192. $this->loggedErrors &= ~$type;
  193. } elseif ($log[0] instanceof LoggerInterface) {
  194. $this->loggedErrors |= $type;
  195. } else {
  196. throw new \InvalidArgumentException('Invalid logger provided');
  197. }
  198. $this->loggers[$type] = $log + $prev[$type];
  199. if ($this->bootstrappingLogger && $prev[$type][0] === $this->bootstrappingLogger) {
  200. $flush[$type] = $type;
  201. }
  202. }
  203. $this->reRegister($prevLogged | $this->thrownErrors);
  204. if ($flush) {
  205. foreach ($this->bootstrappingLogger->cleanLogs() as $log) {
  206. $type = $log[2]['exception']->getSeverity();
  207. if (!isset($flush[$type])) {
  208. $this->bootstrappingLogger->log($log[0], $log[1], $log[2]);
  209. } elseif ($this->loggers[$type][0]) {
  210. $this->loggers[$type][0]->log($this->loggers[$type][1], $log[1], $log[2]);
  211. }
  212. }
  213. }
  214. return $prev;
  215. }
  216. /**
  217. * Sets a user exception handler.
  218. *
  219. * @param callable $handler A handler that will be called on Exception
  220. *
  221. * @return callable|null The previous exception handler
  222. */
  223. public function setExceptionHandler(callable $handler = null)
  224. {
  225. $prev = $this->exceptionHandler;
  226. $this->exceptionHandler = $handler;
  227. return $prev;
  228. }
  229. /**
  230. * Sets the PHP error levels that throw an exception when a PHP error occurs.
  231. *
  232. * @param int $levels A bit field of E_* constants for thrown errors
  233. * @param bool $replace Replace or amend the previous value
  234. *
  235. * @return int The previous value
  236. */
  237. public function throwAt($levels, $replace = false)
  238. {
  239. $prev = $this->thrownErrors;
  240. $this->thrownErrors = ($levels | E_RECOVERABLE_ERROR | E_USER_ERROR) & ~E_USER_DEPRECATED & ~E_DEPRECATED;
  241. if (!$replace) {
  242. $this->thrownErrors |= $prev;
  243. }
  244. $this->reRegister($prev | $this->loggedErrors);
  245. return $prev;
  246. }
  247. /**
  248. * Sets the PHP error levels for which local variables are preserved.
  249. *
  250. * @param int $levels A bit field of E_* constants for scoped errors
  251. * @param bool $replace Replace or amend the previous value
  252. *
  253. * @return int The previous value
  254. */
  255. public function scopeAt($levels, $replace = false)
  256. {
  257. $prev = $this->scopedErrors;
  258. $this->scopedErrors = (int) $levels;
  259. if (!$replace) {
  260. $this->scopedErrors |= $prev;
  261. }
  262. return $prev;
  263. }
  264. /**
  265. * Sets the PHP error levels for which the stack trace is preserved.
  266. *
  267. * @param int $levels A bit field of E_* constants for traced errors
  268. * @param bool $replace Replace or amend the previous value
  269. *
  270. * @return int The previous value
  271. */
  272. public function traceAt($levels, $replace = false)
  273. {
  274. $prev = $this->tracedErrors;
  275. $this->tracedErrors = (int) $levels;
  276. if (!$replace) {
  277. $this->tracedErrors |= $prev;
  278. }
  279. return $prev;
  280. }
  281. /**
  282. * Sets the error levels where the @-operator is ignored.
  283. *
  284. * @param int $levels A bit field of E_* constants for screamed errors
  285. * @param bool $replace Replace or amend the previous value
  286. *
  287. * @return int The previous value
  288. */
  289. public function screamAt($levels, $replace = false)
  290. {
  291. $prev = $this->screamedErrors;
  292. $this->screamedErrors = (int) $levels;
  293. if (!$replace) {
  294. $this->screamedErrors |= $prev;
  295. }
  296. return $prev;
  297. }
  298. /**
  299. * Re-registers as a PHP error handler if levels changed.
  300. */
  301. private function reRegister($prev)
  302. {
  303. if ($prev !== $this->thrownErrors | $this->loggedErrors) {
  304. $handler = set_error_handler('var_dump');
  305. $handler = is_array($handler) ? $handler[0] : null;
  306. restore_error_handler();
  307. if ($handler === $this) {
  308. restore_error_handler();
  309. if ($this->isRoot) {
  310. set_error_handler(array($this, 'handleError'), $this->thrownErrors | $this->loggedErrors);
  311. } else {
  312. set_error_handler(array($this, 'handleError'));
  313. }
  314. }
  315. }
  316. }
  317. /**
  318. * Handles errors by filtering then logging them according to the configured bit fields.
  319. *
  320. * @param int $type One of the E_* constants
  321. * @param string $message
  322. * @param string $file
  323. * @param int $line
  324. * @param array $context
  325. * @param array $backtrace
  326. *
  327. * @return bool Returns false when no handling happens so that the PHP engine can handle the error itself
  328. *
  329. * @throws \ErrorException When $this->thrownErrors requests so
  330. *
  331. * @internal
  332. */
  333. public function handleError($type, $message, $file, $line, array $context, array $backtrace = null)
  334. {
  335. // Level is the current error reporting level to manage silent error.
  336. // Strong errors are not authorized to be silenced.
  337. $level = error_reporting() | E_RECOVERABLE_ERROR | E_USER_ERROR | E_DEPRECATED | E_USER_DEPRECATED;
  338. $log = $this->loggedErrors & $type;
  339. $throw = $this->thrownErrors & $type & $level;
  340. $type &= $level | $this->screamedErrors;
  341. if (!$type || (!$log && !$throw)) {
  342. return $type && $log;
  343. }
  344. if (isset($context['GLOBALS']) && ($this->scopedErrors & $type)) {
  345. unset($context['GLOBALS']);
  346. }
  347. if (null !== $backtrace && $type & E_ERROR) {
  348. // E_ERROR fatal errors are triggered on HHVM when
  349. // hhvm.error_handling.call_user_handler_on_fatals=1
  350. // which is the way to get their backtrace.
  351. $this->handleFatalError(compact('type', 'message', 'file', 'line', 'backtrace'));
  352. return true;
  353. }
  354. $logMessage = $this->levels[$type].': '.$message;
  355. if (null !== self::$toStringException) {
  356. $errorAsException = self::$toStringException;
  357. self::$toStringException = null;
  358. } elseif (!$throw && !($type & $level)) {
  359. $errorAsException = new SilencedErrorContext($type, $file, $line);
  360. } else {
  361. if ($this->scopedErrors & $type) {
  362. $errorAsException = new ContextErrorException($logMessage, 0, $type, $file, $line, $context);
  363. } else {
  364. $errorAsException = new \ErrorException($logMessage, 0, $type, $file, $line);
  365. }
  366. // Clean the trace by removing function arguments and the first frames added by the error handler itself.
  367. if ($throw || $this->tracedErrors & $type) {
  368. $backtrace = $backtrace ?: $errorAsException->getTrace();
  369. $lightTrace = $backtrace;
  370. for ($i = 0; isset($backtrace[$i]); ++$i) {
  371. if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) {
  372. $lightTrace = array_slice($lightTrace, 1 + $i);
  373. break;
  374. }
  375. }
  376. if (!($throw || $this->scopedErrors & $type)) {
  377. for ($i = 0; isset($lightTrace[$i]); ++$i) {
  378. unset($lightTrace[$i]['args']);
  379. }
  380. }
  381. $this->traceReflector->setValue($errorAsException, $lightTrace);
  382. } else {
  383. $this->traceReflector->setValue($errorAsException, array());
  384. }
  385. }
  386. if ($throw) {
  387. if (E_USER_ERROR & $type) {
  388. for ($i = 1; isset($backtrace[$i]); ++$i) {
  389. if (isset($backtrace[$i]['function'], $backtrace[$i]['type'], $backtrace[$i - 1]['function'])
  390. && '__toString' === $backtrace[$i]['function']
  391. && '->' === $backtrace[$i]['type']
  392. && !isset($backtrace[$i - 1]['class'])
  393. && ('trigger_error' === $backtrace[$i - 1]['function'] || 'user_error' === $backtrace[$i - 1]['function'])
  394. ) {
  395. // Here, we know trigger_error() has been called from __toString().
  396. // HHVM is fine with throwing from __toString() but PHP triggers a fatal error instead.
  397. // A small convention allows working around the limitation:
  398. // given a caught $e exception in __toString(), quitting the method with
  399. // `return trigger_error($e, E_USER_ERROR);` allows this error handler
  400. // to make $e get through the __toString() barrier.
  401. foreach ($context as $e) {
  402. if (($e instanceof \Exception || $e instanceof \Throwable) && $e->__toString() === $message) {
  403. if (1 === $i) {
  404. // On HHVM
  405. $errorAsException = $e;
  406. break;
  407. }
  408. self::$toStringException = $e;
  409. return true;
  410. }
  411. }
  412. if (1 < $i) {
  413. // On PHP (not on HHVM), display the original error message instead of the default one.
  414. $this->handleException($errorAsException);
  415. // Stop the process by giving back the error to the native handler.
  416. return false;
  417. }
  418. }
  419. }
  420. }
  421. throw $errorAsException;
  422. }
  423. if ($this->isRecursive) {
  424. $log = 0;
  425. } elseif (self::$stackedErrorLevels) {
  426. self::$stackedErrors[] = array(
  427. $this->loggers[$type][0],
  428. ($type & $level) ? $this->loggers[$type][1] : LogLevel::DEBUG,
  429. $logMessage,
  430. array('exception' => $errorAsException),
  431. );
  432. } else {
  433. try {
  434. $this->isRecursive = true;
  435. $level = ($type & $level) ? $this->loggers[$type][1] : LogLevel::DEBUG;
  436. $this->loggers[$type][0]->log($level, $logMessage, array('exception' => $errorAsException));
  437. } finally {
  438. $this->isRecursive = false;
  439. }
  440. }
  441. return $type && $log;
  442. }
  443. /**
  444. * Handles an exception by logging then forwarding it to another handler.
  445. *
  446. * @param \Exception|\Throwable $exception An exception to handle
  447. * @param array $error An array as returned by error_get_last()
  448. *
  449. * @internal
  450. */
  451. public function handleException($exception, array $error = null)
  452. {
  453. if (!$exception instanceof \Exception) {
  454. $exception = new FatalThrowableError($exception);
  455. }
  456. $type = $exception instanceof FatalErrorException ? $exception->getSeverity() : E_ERROR;
  457. if (($this->loggedErrors & $type) || $exception instanceof FatalThrowableError) {
  458. if ($exception instanceof FatalErrorException) {
  459. if ($exception instanceof FatalThrowableError) {
  460. $error = array(
  461. 'type' => $type,
  462. 'message' => $message = $exception->getMessage(),
  463. 'file' => $exception->getFile(),
  464. 'line' => $exception->getLine(),
  465. );
  466. } else {
  467. $message = 'Fatal '.$exception->getMessage();
  468. }
  469. } elseif ($exception instanceof \ErrorException) {
  470. $message = 'Uncaught '.$exception->getMessage();
  471. if ($exception instanceof ContextErrorException) {
  472. $e['context'] = $exception->getContext();
  473. }
  474. } else {
  475. $message = 'Uncaught Exception: '.$exception->getMessage();
  476. }
  477. }
  478. if ($this->loggedErrors & $type) {
  479. try {
  480. $this->loggers[$type][0]->log($this->loggers[$type][1], $message, array('exception' => $exception));
  481. } catch (\Exception $handlerException) {
  482. } catch (\Throwable $handlerException) {
  483. }
  484. }
  485. if ($exception instanceof FatalErrorException && !$exception instanceof OutOfMemoryException && $error) {
  486. foreach ($this->getFatalErrorHandlers() as $handler) {
  487. if ($e = $handler->handleError($error, $exception)) {
  488. $exception = $e;
  489. break;
  490. }
  491. }
  492. }
  493. if (empty($this->exceptionHandler)) {
  494. throw $exception; // Give back $exception to the native handler
  495. }
  496. try {
  497. call_user_func($this->exceptionHandler, $exception);
  498. } catch (\Exception $handlerException) {
  499. } catch (\Throwable $handlerException) {
  500. }
  501. if (isset($handlerException)) {
  502. $this->exceptionHandler = null;
  503. $this->handleException($handlerException);
  504. }
  505. }
  506. /**
  507. * Shutdown registered function for handling PHP fatal errors.
  508. *
  509. * @param array $error An array as returned by error_get_last()
  510. *
  511. * @internal
  512. */
  513. public static function handleFatalError(array $error = null)
  514. {
  515. if (null === self::$reservedMemory) {
  516. return;
  517. }
  518. self::$reservedMemory = null;
  519. $handler = set_error_handler('var_dump');
  520. $handler = is_array($handler) ? $handler[0] : null;
  521. restore_error_handler();
  522. if (!$handler instanceof self) {
  523. return;
  524. }
  525. if (null === $error) {
  526. $error = error_get_last();
  527. }
  528. try {
  529. while (self::$stackedErrorLevels) {
  530. static::unstackErrors();
  531. }
  532. } catch (\Exception $exception) {
  533. // Handled below
  534. } catch (\Throwable $exception) {
  535. // Handled below
  536. }
  537. if ($error && $error['type'] &= E_PARSE | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR) {
  538. // Let's not throw anymore but keep logging
  539. $handler->throwAt(0, true);
  540. $trace = isset($error['backtrace']) ? $error['backtrace'] : null;
  541. if (0 === strpos($error['message'], 'Allowed memory') || 0 === strpos($error['message'], 'Out of memory')) {
  542. $exception = new OutOfMemoryException($handler->levels[$error['type']].': '.$error['message'], 0, $error['type'], $error['file'], $error['line'], 2, false, $trace);
  543. } else {
  544. $exception = new FatalErrorException($handler->levels[$error['type']].': '.$error['message'], 0, $error['type'], $error['file'], $error['line'], 2, true, $trace);
  545. }
  546. } elseif (!isset($exception)) {
  547. return;
  548. }
  549. try {
  550. $handler->handleException($exception, $error);
  551. } catch (FatalErrorException $e) {
  552. // Ignore this re-throw
  553. }
  554. }
  555. /**
  556. * Configures the error handler for delayed handling.
  557. * Ensures also that non-catchable fatal errors are never silenced.
  558. *
  559. * As shown by http://bugs.php.net/42098 and http://bugs.php.net/60724
  560. * PHP has a compile stage where it behaves unusually. To workaround it,
  561. * we plug an error handler that only stacks errors for later.
  562. *
  563. * The most important feature of this is to prevent
  564. * autoloading until unstackErrors() is called.
  565. */
  566. public static function stackErrors()
  567. {
  568. self::$stackedErrorLevels[] = error_reporting(error_reporting() | E_PARSE | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR);
  569. }
  570. /**
  571. * Unstacks stacked errors and forwards to the logger.
  572. */
  573. public static function unstackErrors()
  574. {
  575. $level = array_pop(self::$stackedErrorLevels);
  576. if (null !== $level) {
  577. $errorReportingLevel = error_reporting($level);
  578. if ($errorReportingLevel !== ($level | E_PARSE | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR)) {
  579. // If the user changed the error level, do not overwrite it
  580. error_reporting($errorReportingLevel);
  581. }
  582. }
  583. if (empty(self::$stackedErrorLevels)) {
  584. $errors = self::$stackedErrors;
  585. self::$stackedErrors = array();
  586. foreach ($errors as $error) {
  587. $error[0]->log($error[1], $error[2], $error[3]);
  588. }
  589. }
  590. }
  591. /**
  592. * Gets the fatal error handlers.
  593. *
  594. * Override this method if you want to define more fatal error handlers.
  595. *
  596. * @return FatalErrorHandlerInterface[] An array of FatalErrorHandlerInterface
  597. */
  598. protected function getFatalErrorHandlers()
  599. {
  600. return array(
  601. new UndefinedFunctionFatalErrorHandler(),
  602. new UndefinedMethodFatalErrorHandler(),
  603. new ClassNotFoundFatalErrorHandler(),
  604. );
  605. }
  606. }