YurunHttp.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace Yurun\Util;
  3. use Swoole\Coroutine;
  4. use Yurun\Util\YurunHttp\Handler\IHandler;
  5. abstract class YurunHttp
  6. {
  7. /**
  8. * 默认处理器类.
  9. *
  10. * @var string|null
  11. */
  12. private static $defaultHandler = null;
  13. /**
  14. * 属性.
  15. *
  16. * @var array
  17. */
  18. private static $attributes = [];
  19. /**
  20. * 版本号.
  21. */
  22. const VERSION = '4.3';
  23. /**
  24. * 设置默认处理器类.
  25. *
  26. * @param string|null $class
  27. *
  28. * @return void
  29. */
  30. public static function setDefaultHandler($class)
  31. {
  32. static::$defaultHandler = $class;
  33. }
  34. /**
  35. * 获取默认处理器类.
  36. *
  37. * @return string|null
  38. */
  39. public static function getDefaultHandler()
  40. {
  41. return static::$defaultHandler;
  42. }
  43. /**
  44. * 获取处理器类.
  45. *
  46. * @return \Yurun\Util\YurunHttp\Handler\IHandler
  47. */
  48. public static function getHandler()
  49. {
  50. if (static::$defaultHandler)
  51. {
  52. $class = static::$defaultHandler;
  53. }
  54. elseif (\defined('SWOOLE_VERSION') && Coroutine::getuid() > -1)
  55. {
  56. $class = \Yurun\Util\YurunHttp\Handler\Swoole::class;
  57. }
  58. else
  59. {
  60. $class = \Yurun\Util\YurunHttp\Handler\Curl::class;
  61. }
  62. return new $class();
  63. }
  64. /**
  65. * 发送请求并获取结果.
  66. *
  67. * @param \Yurun\Util\YurunHttp\Http\Request $request
  68. * @param \Yurun\Util\YurunHttp\Handler\IHandler|string|null $handlerClass
  69. *
  70. * @return \Yurun\Util\YurunHttp\Http\Response|null
  71. */
  72. public static function send($request, $handlerClass = null)
  73. {
  74. if ($handlerClass instanceof IHandler)
  75. {
  76. $handler = $handlerClass;
  77. $needClose = false;
  78. }
  79. else
  80. {
  81. $needClose = true;
  82. if (null === $handlerClass)
  83. {
  84. $handler = static::getHandler();
  85. }
  86. else
  87. {
  88. $handler = new $handlerClass();
  89. }
  90. }
  91. /** @var IHandler $handler */
  92. $time = microtime(true);
  93. foreach (static::$attributes as $name => $value)
  94. {
  95. if (null === $request->getAttribute($name))
  96. {
  97. $request = $request->withAttribute($name, $value);
  98. }
  99. }
  100. $handler->send($request);
  101. $response = $handler->recv();
  102. if (!$response)
  103. {
  104. return $response;
  105. }
  106. $response = $response->withTotalTime(microtime(true) - $time);
  107. if ($needClose)
  108. {
  109. $handler->close();
  110. }
  111. return $response;
  112. }
  113. /**
  114. * 发起 WebSocket 连接.
  115. *
  116. * @param \Yurun\Util\YurunHttp\Http\Request $request
  117. * @param \Yurun\Util\YurunHttp\Handler\IHandler|string $handlerClass
  118. *
  119. * @return \Yurun\Util\YurunHttp\WebSocket\IWebSocketClient
  120. */
  121. public static function websocket($request, $handlerClass = null)
  122. {
  123. if ($handlerClass instanceof IHandler)
  124. {
  125. $handler = $handlerClass;
  126. }
  127. elseif (null === $handlerClass)
  128. {
  129. $handler = static::getHandler();
  130. }
  131. else
  132. {
  133. $handler = new $handlerClass();
  134. }
  135. foreach (static::$attributes as $name => $value)
  136. {
  137. if (null === $request->getAttribute($name))
  138. {
  139. $request = $request->withAttribute($name, $value);
  140. }
  141. }
  142. return $handler->websocket($request);
  143. }
  144. /**
  145. * 获取所有全局属性.
  146. *
  147. * @return array
  148. */
  149. public static function getAttributes()
  150. {
  151. return static::$attributes;
  152. }
  153. /**
  154. * 获取全局属性值
  155. *
  156. * @param string $name
  157. * @param mixed $default
  158. *
  159. * @return mixed
  160. */
  161. public static function getAttribute($name, $default = null)
  162. {
  163. if (\array_key_exists($name, static::$attributes))
  164. {
  165. return static::$attributes[$name];
  166. }
  167. else
  168. {
  169. return $default;
  170. }
  171. }
  172. /**
  173. * 设置全局属性值
  174. *
  175. * @param string $name
  176. * @param mixed $value
  177. *
  178. * @return mixed
  179. */
  180. public static function setAttribute($name, $value)
  181. {
  182. static::$attributes[$name] = $value;
  183. }
  184. }