_inc.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. require_once 'vendor/autoload.php';
  3. $dir = array(
  4. __DIR__ . '/output',
  5. __DIR__ . '/output/clone',
  6. __DIR__ . '/output/cache'
  7. );
  8. foreach ($dir as $v) {
  9. if (! is_dir($v)) {
  10. mkdir($v);
  11. }
  12. }
  13. if (! function_exists('printr')) {
  14. /**
  15. *
  16. * @param mixed $expression
  17. */
  18. function printr()
  19. {
  20. foreach (func_get_args() as $v) {
  21. if (is_scalar($v)) {
  22. echo $v . "\n";
  23. } else {
  24. print_r($v);
  25. }
  26. }
  27. exit();
  28. }
  29. }
  30. if (! function_exists('vardump')) {
  31. /**
  32. *
  33. * @param mixed $expression
  34. */
  35. function vardump()
  36. {
  37. call_user_func_array('var_dump', func_get_args());
  38. exit();
  39. }
  40. }
  41. class ErrorHandler
  42. {
  43. /**
  44. * error to exception
  45. *
  46. * @throws \ErrorException
  47. */
  48. static function error2exception()
  49. {
  50. set_error_handler(
  51. function ($errno, $errstr, $errfile, $errline) {
  52. $r = error_reporting();
  53. if ($r & $errno) {
  54. $exception = new \ErrorException($errstr, null, $errno,
  55. $errfile, $errline);
  56. if ($errno == E_USER_ERROR || $errno == E_RECOVERABLE_ERROR) {
  57. throw $exception;
  58. }
  59. static::catchException($exception);
  60. }
  61. });
  62. }
  63. /**
  64. *
  65. * @param int $severity
  66. * @return string|null
  67. */
  68. static function severity2string($severity)
  69. {
  70. static $map;
  71. if (! isset($map)) {
  72. $map = get_defined_constants(true);
  73. $map = $map['Core'];
  74. foreach (array_keys($map) as $v) {
  75. if (0 !== strpos($v, 'E_')) {
  76. unset($map[$v]);
  77. }
  78. }
  79. $map = array_flip($map);
  80. }
  81. if (array_key_exists($severity, $map)) {
  82. return $map[$severity];
  83. }
  84. }
  85. /**
  86. * deal with exception
  87. *
  88. * @param \Exception $exception
  89. */
  90. static function catchException($exception)
  91. {
  92. $str = '';
  93. if ($exception instanceof \ErrorException) {
  94. $str .= static::severity2string($exception->getSeverity()) . ': ';
  95. }
  96. $str .= $exception->__toString();
  97. if (ini_get('display_errors')) {
  98. echo $str . "\n\n";
  99. }
  100. if (ini_get('log_errors')) {
  101. error_log($str);
  102. }
  103. }
  104. }
  105. ErrorHandler::error2exception();