BaseServer.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace fcgisrv;
  3. require_once(BASE_ROOT_PATH . '/helper/http_header.php');
  4. require_once(BASE_ROOT_PATH . '/helper/performance_helper.php');
  5. require_once(BASE_ROOT_PATH . '/helper/request_helper.php');
  6. use http_header;
  7. use Log;
  8. use request_helper;
  9. abstract class BaseServer
  10. {
  11. protected $mSubPath;
  12. public function __construct($subPath) {
  13. $this->mSubPath = $subPath;
  14. }
  15. protected $mExFiles;
  16. protected function setExFiles(array $files)
  17. {
  18. $this->mExFiles = $files;
  19. }
  20. protected function is_exclude($file)
  21. {
  22. return false;
  23. }
  24. private function clear_global()
  25. {
  26. $_SESSION = [];
  27. $_COOKIE = [];
  28. $_POST = [];
  29. $_GET = [];
  30. }
  31. public function handle_error($level, $message, $file, $line)
  32. {
  33. if($level == E_NOTICE) return;
  34. $trace = "handle_error: level={$level},msg={$message} file={$file},line={$line}\n";
  35. $backtrace = debug_backtrace();
  36. foreach ($backtrace as $item) {
  37. $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
  38. }
  39. Log::record($trace,Log::ERR);
  40. }
  41. protected function preLooper()
  42. {
  43. set_error_handler([$this, 'handle_error']);
  44. }
  45. abstract function handle_req($file);
  46. public function run_looper()
  47. {
  48. Log::record(__FUNCTION__,Log::DEBUG);
  49. $this->preLooper();
  50. Log::record('Waiting......',Log::DEBUG);
  51. while(($ret = fcgi_accept()) >= 0)
  52. {
  53. $start = microtime(true);
  54. ob_start();
  55. $this->clear_global();
  56. perfor_clear();
  57. perfor_start();
  58. Log::start_sql_log();
  59. init_request();
  60. init_cookie($_SERVER['HTTP_COOKIE']);
  61. http_header::instance()->start();
  62. $file = request_helper::script_file();
  63. $this->handle_req($file);
  64. fcgi_headers_sent();
  65. $contents = ob_get_clean();
  66. fcgi_echo($contents);
  67. Log::end_sql_log();
  68. $msg = sprintf("request time=%.6f\r\n\r\n",microtime(true) - $start);
  69. Log::record($msg ,Log::DEBUG);
  70. $perlog = perfor_log();
  71. Log::record("perlog = {$perlog}" ,Log::DEBUG);
  72. }
  73. fcgi_fini();
  74. Log::record('Waiting quit......',Log::DEBUG);
  75. }
  76. }