SystemFacade.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * @package Grav\Common\Errors
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Errors;
  9. /**
  10. * Class SystemFacade
  11. * @package Grav\Common\Errors
  12. */
  13. class SystemFacade extends \Whoops\Util\SystemFacade
  14. {
  15. /** @var callable */
  16. protected $whoopsShutdownHandler;
  17. /**
  18. * @param callable $function
  19. * @return void
  20. */
  21. public function registerShutdownFunction(callable $function)
  22. {
  23. $this->whoopsShutdownHandler = $function;
  24. register_shutdown_function([$this, 'handleShutdown']);
  25. }
  26. /**
  27. * Special case to deal with Fatal errors and the like.
  28. *
  29. * @return void
  30. */
  31. public function handleShutdown()
  32. {
  33. $error = $this->getLastError();
  34. // Ignore core warnings and errors.
  35. if ($error && !($error['type'] & (E_CORE_WARNING | E_CORE_ERROR))) {
  36. $handler = $this->whoopsShutdownHandler;
  37. $handler();
  38. }
  39. }
  40. /**
  41. * @param int $httpCode
  42. *
  43. * @return int
  44. */
  45. public function setHttpResponseCode($httpCode)
  46. {
  47. if (!headers_sent()) {
  48. // Ensure that no 'location' header is present as otherwise this
  49. // will override the HTTP code being set here, and mask the
  50. // expected error page.
  51. header_remove('location');
  52. // Work around PHP bug #8218 (8.0.17 & 8.1.4).
  53. header_remove('Content-Encoding');
  54. }
  55. return http_response_code($httpCode);
  56. }
  57. }