Application.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\worker;
  12. use think\App;
  13. use think\exception\Handle;
  14. use think\exception\HttpException;
  15. use Workerman\Protocols\Http as WorkerHttp;
  16. /**
  17. * Worker应用对象
  18. */
  19. class Application extends App
  20. {
  21. /**
  22. * 处理Worker请求
  23. * @access public
  24. * @param \Workerman\Connection\TcpConnection $connection
  25. * @param void
  26. */
  27. public function worker($connection)
  28. {
  29. try {
  30. $this->beginTime = microtime(true);
  31. $this->beginMem = memory_get_usage();
  32. $this->db->clearQueryTimes();
  33. $pathinfo = ltrim(strpos($_SERVER['REQUEST_URI'], '?') ? strstr($_SERVER['REQUEST_URI'], '?', true) : $_SERVER['REQUEST_URI'], '/');
  34. $this->request
  35. ->setPathinfo($pathinfo)
  36. ->withInput($GLOBALS['HTTP_RAW_POST_DATA']);
  37. while (ob_get_level() > 1) {
  38. ob_end_clean();
  39. }
  40. ob_start();
  41. $response = $this->http->run();
  42. $content = ob_get_clean();
  43. ob_start();
  44. $response->send();
  45. $this->http->end($response);
  46. $content .= ob_get_clean() ?: '';
  47. $this->httpResponseCode($response->getCode());
  48. foreach ($response->getHeader() as $name => $val) {
  49. // 发送头部信息
  50. WorkerHttp::header($name . (!is_null($val) ? ':' . $val : ''));
  51. }
  52. if (strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
  53. $connection->send($content);
  54. } else {
  55. $connection->close($content);
  56. }
  57. } catch (HttpException | \Exception | \Throwable $e) {
  58. $this->exception($connection, $e);
  59. }
  60. }
  61. /**
  62. * 是否运行在命令行下
  63. * @return bool
  64. */
  65. public function runningInConsole(): bool
  66. {
  67. return false;
  68. }
  69. protected function httpResponseCode($code = 200)
  70. {
  71. WorkerHttp::responseCode($code);
  72. }
  73. protected function exception($connection, $e)
  74. {
  75. if ($e instanceof \Exception) {
  76. $handler = $this->make(Handle::class);
  77. $handler->report($e);
  78. $resp = $handler->render($this->request, $e);
  79. $content = $resp->getContent();
  80. $code = $resp->getCode();
  81. $this->httpResponseCode($code);
  82. $connection->send($content);
  83. } else {
  84. $this->httpResponseCode(500);
  85. $connection->send($e->getMessage());
  86. }
  87. }
  88. }