ErrorHandler.php 24 KB

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