handler.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\error;
  3. use MGModule\DNSManager2 as main;
  4. /**
  5. * Error Handler
  6. *
  7. * DON'T USE IN PRODUCTION MODULES
  8. *
  9. * @author Michal Czech <michael@modulesgarden.com>
  10. */
  11. class handler{
  12. const EXCEPTION_HANDLER = "handleException";
  13. const ERROR_HANDLER = "handleError";
  14. const SHUTDOWN_HANDLER = "handleShutdown";
  15. /**
  16. * set FALSE for production modules
  17. */
  18. const VERBOSE = false;
  19. /**
  20. * Register Error Functions
  21. *
  22. * @author Michal Czech <michael@modulesgarden.com>
  23. */
  24. public function register()
  25. {
  26. set_error_handler(array($this, self::ERROR_HANDLER));
  27. set_exception_handler(array($this, self::EXCEPTION_HANDLER));
  28. register_shutdown_function(array($this, self::SHUTDOWN_HANDLER));
  29. }
  30. public function handleException(\Exception $exception)
  31. {
  32. if(static::$VERBOSE)
  33. {
  34. echo "<pre>";
  35. print_r($exception);
  36. echo "</pre>";
  37. }
  38. if(method_exists($exception, 'getToken'))
  39. {
  40. die("Error type H: ".$exception->getToken());
  41. }
  42. }
  43. public function handleError($level, $message, $file = null, $line = null)
  44. {
  45. if($line == 0)
  46. {
  47. return true;
  48. }
  49. if(strpos($file,'tpl.php') !== false)
  50. {
  51. return true;
  52. }
  53. if(in_array($level,array(
  54. E_NOTICE
  55. ,E_USER_NOTICE
  56. )))
  57. {
  58. return true;
  59. }
  60. throw new main\mgLibs\exceptions\syntaxError($message, $level, 0, $line, $file);
  61. }
  62. public function handleShutdown()
  63. {
  64. $error = error_get_last();
  65. if($error)
  66. {
  67. if($line == 0)
  68. {
  69. return true;
  70. }
  71. if(static::$VERBOSE)
  72. {
  73. echo "<pre>";
  74. print_r($error);
  75. echo "</pre>";
  76. }
  77. if(in_array($error['type'],array(
  78. E_NOTICE
  79. ,E_USER_NOTICE
  80. )))
  81. {
  82. return true;
  83. }
  84. die("Error");
  85. }
  86. }
  87. }