You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.1 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage app
  7. * @since 2010-02-25
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class ErrorAction extends Action
  12. {
  13. /**
  14. * @var ErrorException
  15. */
  16. protected $exception;
  17. public function __construct($exception)
  18. {
  19. $this->exception = $exception;
  20. parent::__construct();
  21. }
  22. protected function execute()
  23. {
  24. $this->template = 500;
  25. if ($this->exception instanceof Error404Exception) {
  26. $this->template = 404;
  27. }
  28. }
  29. protected function getTemplate()
  30. {
  31. return '/static/' . $this->template;
  32. }
  33. protected function sendHttpCode()
  34. {
  35. if (headers_sent()) {
  36. return;
  37. }
  38. switch ($this->template) {
  39. case 404:
  40. header('HTTP/1.0 404 Not Found');
  41. break;
  42. default:
  43. header('HTTP/1.0 500 Internal Server Error');
  44. }
  45. }
  46. protected function logError()
  47. {
  48. if ($this->template = 500) {
  49. $error = 0;
  50. $ex = $this->exception;
  51. if ($ex instanceof ErrorException) {
  52. $error = $ex->getSeverity();
  53. }
  54. switch ($error) {
  55. case E_NOTICE:
  56. $error = 'Notice';
  57. break;
  58. case E_WARNING:
  59. $error = 'Warning';
  60. break;
  61. case E_ERROR:
  62. $error = 'Fatal Error';
  63. break;
  64. default:
  65. $error = 'Unknown Error';
  66. break;
  67. }
  68. $message = 'PHP ' . $error . ': ' . $ex->getMessage() . ' in ' . $ex->getFile()
  69. . ' on line ' . $ex->getLine();
  70. error_log($message);
  71. }
  72. }
  73. public function fetch()
  74. {
  75. $this->logError();
  76. $this->sendHTTPCode();
  77. return $this->view->fetch($this->getTemplate());
  78. }
  79. }