Няма описание

ErrorHandler.php 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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'] instanceof \ErrorException ? $log[2]['exception']->getSeverity() : E_ERROR;
  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. *
  325. * @return bool Returns false when no handling happens so that the PHP engine can handle the error itself
  326. *
  327. * @throws \ErrorException When $this->thrownErrors requests so
  328. *
  329. * @internal
  330. */
  331. public function handleError($type, $message, $file, $line)
  332. {
  333. // Level is the current error reporting level to manage silent error.
  334. // Strong errors are not authorized to be silenced.
  335. $level = error_reporting() | E_RECOVERABLE_ERROR | E_USER_ERROR | E_DEPRECATED | E_USER_DEPRECATED;
  336. $log = $this->loggedErrors & $type;
  337. $throw = $this->thrownErrors & $type & $level;
  338. $type &= $level | $this->screamedErrors;
  339. if (!$type || (!$log && !$throw)) {
  340. return $type && $log;
  341. }
  342. $scope = $this->scopedErrors & $type;
  343. if (4 < $numArgs = func_num_args()) {
  344. $context = $scope ? (func_get_arg(4) ?: array()) : array();
  345. $backtrace = 5 < $numArgs ? func_get_arg(5) : null; // defined on HHVM
  346. } else {
  347. $context = array();
  348. $backtrace = null;
  349. }
  350. if (isset($context['GLOBALS']) && $scope) {
  351. $e = $context; // Whatever the signature of the method,
  352. unset($e['GLOBALS'], $context); // $context is always a reference in 5.3
  353. $context = $e;
  354. }
  355. if (null !== $backtrace && $type & E_ERROR) {
  356. // E_ERROR fatal errors are triggered on HHVM when
  357. // hhvm.error_handling.call_user_handler_on_fatals=1
  358. // which is the way to get their backtrace.
  359. $this->handleFatalError(compact('type', 'message', 'file', 'line', 'backtrace'));
  360. return true;
  361. }
  362. $logMessage = $this->levels[$type].': '.$message;
  363. if (null !== self::$toStringException) {
  364. $errorAsException = self::$toStringException;
  365. self::$toStringException = null;
  366. } elseif (!$throw && !($type & $level)) {
  367. $errorAsException = new SilencedErrorContext($type, $file, $line);
  368. } else {
  369. if ($scope) {
  370. $errorAsException = new ContextErrorException($logMessage, 0, $type, $file, $line, $context);
  371. } else {
  372. $errorAsException = new \ErrorException($logMessage, 0, $type, $file, $line);
  373. }
  374. // Clean the trace by removing function arguments and the first frames added by the error handler itself.
  375. if ($throw || $this->tracedErrors & $type) {
  376. $backtrace = $backtrace ?: $errorAsException->getTrace();
  377. $lightTrace = $backtrace;
  378. for ($i = 0; isset($backtrace[$i]); ++$i) {
  379. if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) {
  380. $lightTrace = array_slice($lightTrace, 1 + $i);
  381. break;
  382. }
  383. }
  384. if (!($throw || $this->scopedErrors & $type)) {
  385. for ($i = 0; isset($lightTrace[$i]); ++$i) {
  386. unset($lightTrace[$i]['args']);
  387. }
  388. }
  389. $this->traceReflector->setValue($errorAsException, $lightTrace);
  390. } else {
  391. $this->traceReflector->setValue($errorAsException, array());
  392. }
  393. }
  394. if ($throw) {
  395. if (E_USER_ERROR & $type) {
  396. for ($i = 1; isset($backtrace[$i]); ++$i) {
  397. if (isset($backtrace[$i]['function'], $backtrace[$i]['type'], $backtrace[$i - 1]['function'])
  398. && '__toString' === $backtrace[$i]['function']
  399. && '->' === $backtrace[$i]['type']
  400. && !isset($backtrace[$i - 1]['class'])
  401. && ('trigger_error' === $backtrace[$i - 1]['function'] || 'user_error' === $backtrace[$i - 1]['function'])
  402. ) {
  403. // Here, we know trigger_error() has been called from __toString().
  404. // HHVM is fine with throwing from __toString() but PHP triggers a fatal error instead.
  405. // A small convention allows working around the limitation:
  406. // given a caught $e exception in __toString(), quitting the method with
  407. // `return trigger_error($e, E_USER_ERROR);` allows this error handler
  408. // to make $e get through the __toString() barrier.
  409. foreach ($context as $e) {
  410. if (($e instanceof \Exception || $e instanceof \Throwable) && $e->__toString() === $message) {
  411. if (1 === $i) {
  412. // On HHVM
  413. $errorAsException = $e;
  414. break;
  415. }
  416. self::$toStringException = $e;
  417. return true;
  418. }
  419. }
  420. if (1 < $i) {
  421. // On PHP (not on HHVM), display the original error message instead of the default one.
  422. $this->handleException($errorAsException);
  423. // Stop the process by giving back the error to the native handler.
  424. return false;
  425. }
  426. }
  427. }
  428. }
  429. throw $errorAsException;
  430. }
  431. if ($this->isRecursive) {
  432. $log = 0;
  433. } elseif (self::$stackedErrorLevels) {
  434. self::$stackedErrors[] = array(
  435. $this->loggers[$type][0],
  436. ($type & $level) ? $this->loggers[$type][1] : LogLevel::DEBUG,
  437. $logMessage,
  438. array('exception' => $errorAsException),
  439. );
  440. } else {
  441. try {
  442. $this->isRecursive = true;
  443. $level = ($type & $level) ? $this->loggers[$type][1] : LogLevel::DEBUG;
  444. $this->loggers[$type][0]->log($level, $logMessage, array('exception' => $errorAsException));
  445. } finally {
  446. $this->isRecursive = false;
  447. }
  448. }
  449. return $type && $log;
  450. }
  451. /**
  452. * Handles an exception by logging then forwarding it to another handler.
  453. *
  454. * @param \Exception|\Throwable $exception An exception to handle
  455. * @param array $error An array as returned by error_get_last()
  456. *
  457. * @internal
  458. */
  459. public function handleException($exception, array $error = null)
  460. {
  461. if (!$exception instanceof \Exception) {
  462. $exception = new FatalThrowableError($exception);
  463. }
  464. $type = $exception instanceof FatalErrorException ? $exception->getSeverity() : E_ERROR;
  465. if (($this->loggedErrors & $type) || $exception instanceof FatalThrowableError) {
  466. if ($exception instanceof FatalErrorException) {
  467. if ($exception instanceof FatalThrowableError) {
  468. $error = array(
  469. 'type' => $type,
  470. 'message' => $message = $exception->getMessage(),
  471. 'file' => $exception->getFile(),
  472. 'line' => $exception->getLine(),
  473. );
  474. } else {
  475. $message = 'Fatal '.$exception->getMessage();
  476. }
  477. } elseif ($exception instanceof \ErrorException) {
  478. $message = 'Uncaught '.$exception->getMessage();
  479. if ($exception instanceof ContextErrorException) {
  480. $e['context'] = $exception->getContext();
  481. }
  482. } else {
  483. $message = 'Uncaught Exception: '.$exception->getMessage();
  484. }
  485. }
  486. if ($this->loggedErrors & $type) {
  487. try {
  488. $this->loggers[$type][0]->log($this->loggers[$type][1], $message, array('exception' => $exception));
  489. } catch (\Exception $handlerException) {
  490. } catch (\Throwable $handlerException) {
  491. }
  492. }
  493. if ($exception instanceof FatalErrorException && !$exception instanceof OutOfMemoryException && $error) {
  494. foreach ($this->getFatalErrorHandlers() as $handler) {
  495. if ($e = $handler->handleError($error, $exception)) {
  496. $exception = $e;
  497. break;
  498. }
  499. }
  500. }
  501. if (empty($this->exceptionHandler)) {
  502. throw $exception; // Give back $exception to the native handler
  503. }
  504. try {
  505. call_user_func($this->exceptionHandler, $exception);
  506. } catch (\Exception $handlerException) {
  507. } catch (\Throwable $handlerException) {
  508. }
  509. if (isset($handlerException)) {
  510. $this->exceptionHandler = null;
  511. $this->handleException($handlerException);
  512. }
  513. }
  514. /**
  515. * Shutdown registered function for handling PHP fatal errors.
  516. *
  517. * @param array $error An array as returned by error_get_last()
  518. *
  519. * @internal
  520. */
  521. public static function handleFatalError(array $error = null)
  522. {
  523. if (null === self::$reservedMemory) {
  524. return;
  525. }
  526. self::$reservedMemory = null;
  527. $handler = set_error_handler('var_dump');
  528. $handler = is_array($handler) ? $handler[0] : null;
  529. restore_error_handler();
  530. if (!$handler instanceof self) {
  531. return;
  532. }
  533. if (null === $error) {
  534. $error = error_get_last();
  535. }
  536. try {
  537. while (self::$stackedErrorLevels) {
  538. static::unstackErrors();
  539. }
  540. } catch (\Exception $exception) {
  541. // Handled below
  542. } catch (\Throwable $exception) {
  543. // Handled below
  544. }
  545. if ($error && $error['type'] &= E_PARSE | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR) {
  546. // Let's not throw anymore but keep logging
  547. $handler->throwAt(0, true);
  548. $trace = isset($error['backtrace']) ? $error['backtrace'] : null;
  549. if (0 === strpos($error['message'], 'Allowed memory') || 0 === strpos($error['message'], 'Out of memory')) {
  550. $exception = new OutOfMemoryException($handler->levels[$error['type']].': '.$error['message'], 0, $error['type'], $error['file'], $error['line'], 2, false, $trace);
  551. } else {
  552. $exception = new FatalErrorException($handler->levels[$error['type']].': '.$error['message'], 0, $error['type'], $error['file'], $error['line'], 2, true, $trace);
  553. }
  554. } elseif (!isset($exception)) {
  555. return;
  556. }
  557. try {
  558. $handler->handleException($exception, $error);
  559. } catch (FatalErrorException $e) {
  560. // Ignore this re-throw
  561. }
  562. }
  563. /**
  564. * Configures the error handler for delayed handling.
  565. * Ensures also that non-catchable fatal errors are never silenced.
  566. *
  567. * As shown by http://bugs.php.net/42098 and http://bugs.php.net/60724
  568. * PHP has a compile stage where it behaves unusually. To workaround it,
  569. * we plug an error handler that only stacks errors for later.
  570. *
  571. * The most important feature of this is to prevent
  572. * autoloading until unstackErrors() is called.
  573. */
  574. public static function stackErrors()
  575. {
  576. self::$stackedErrorLevels[] = error_reporting(error_reporting() | E_PARSE | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR);
  577. }
  578. /**
  579. * Unstacks stacked errors and forwards to the logger.
  580. */
  581. public static function unstackErrors()
  582. {
  583. $level = array_pop(self::$stackedErrorLevels);
  584. if (null !== $level) {
  585. $errorReportingLevel = error_reporting($level);
  586. if ($errorReportingLevel !== ($level | E_PARSE | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR)) {
  587. // If the user changed the error level, do not overwrite it
  588. error_reporting($errorReportingLevel);
  589. }
  590. }
  591. if (empty(self::$stackedErrorLevels)) {
  592. $errors = self::$stackedErrors;
  593. self::$stackedErrors = array();
  594. foreach ($errors as $error) {
  595. $error[0]->log($error[1], $error[2], $error[3]);
  596. }
  597. }
  598. }
  599. /**
  600. * Gets the fatal error handlers.
  601. *
  602. * Override this method if you want to define more fatal error handlers.
  603. *
  604. * @return FatalErrorHandlerInterface[] An array of FatalErrorHandlerInterface
  605. */
  606. protected function getFatalErrorHandlers()
  607. {
  608. return array(
  609. new UndefinedFunctionFatalErrorHandler(),
  610. new UndefinedMethodFatalErrorHandler(),
  611. new ClassNotFoundFatalErrorHandler(),
  612. );
  613. }
  614. }