Application.php 3.2 KB

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