Response.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 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. declare (strict_types = 1);
  12. namespace think;
  13. /**
  14. * 响应输出基础类
  15. * @package think
  16. */
  17. abstract class Response
  18. {
  19. /**
  20. * 原始数据
  21. * @var mixed
  22. */
  23. protected $data;
  24. /**
  25. * 当前contentType
  26. * @var string
  27. */
  28. protected $contentType = 'text/html';
  29. /**
  30. * 字符集
  31. * @var string
  32. */
  33. protected $charset = 'utf-8';
  34. /**
  35. * 状态码
  36. * @var integer
  37. */
  38. protected $code = 200;
  39. /**
  40. * 是否允许请求缓存
  41. * @var bool
  42. */
  43. protected $allowCache = true;
  44. /**
  45. * 输出参数
  46. * @var array
  47. */
  48. protected $options = [];
  49. /**
  50. * header参数
  51. * @var array
  52. */
  53. protected $header = [];
  54. /**
  55. * 输出内容
  56. * @var string
  57. */
  58. protected $content = null;
  59. /**
  60. * Cookie对象
  61. * @var Cookie
  62. */
  63. protected $cookie;
  64. /**
  65. * Session对象
  66. * @var Session
  67. */
  68. protected $session;
  69. /**
  70. * 初始化
  71. * @access protected
  72. * @param mixed $data 输出数据
  73. * @param int $code 状态码
  74. */
  75. protected function init($data = '', int $code = 200)
  76. {
  77. $this->data($data);
  78. $this->code = $code;
  79. $this->contentType($this->contentType, $this->charset);
  80. }
  81. /**
  82. * 创建Response对象
  83. * @access public
  84. * @param mixed $data 输出数据
  85. * @param string $type 输出类型
  86. * @param int $code 状态码
  87. * @return Response
  88. */
  89. public static function create($data = '', string $type = 'html', int $code = 200): Response
  90. {
  91. $class = false !== strpos($type, '\\') ? $type : '\\think\\response\\' . ucfirst(strtolower($type));
  92. return Container::getInstance()->invokeClass($class, [$data, $code]);
  93. }
  94. /**
  95. * 设置Session对象
  96. * @access public
  97. * @param Session $session Session对象
  98. * @return $this
  99. */
  100. public function setSession(Session $session)
  101. {
  102. $this->session = $session;
  103. return $this;
  104. }
  105. /**
  106. * 发送数据到客户端
  107. * @access public
  108. * @return void
  109. * @throws \InvalidArgumentException
  110. */
  111. public function send(): void
  112. {
  113. // 处理输出数据
  114. $data = $this->getContent();
  115. if (!headers_sent() && !empty($this->header)) {
  116. // 发送状态码
  117. http_response_code($this->code);
  118. // 发送头部信息
  119. foreach ($this->header as $name => $val) {
  120. header($name . (!is_null($val) ? ':' . $val : ''));
  121. }
  122. }
  123. $this->cookie->save();
  124. $this->sendData($data);
  125. if (function_exists('fastcgi_finish_request')) {
  126. // 提高页面响应
  127. fastcgi_finish_request();
  128. }
  129. }
  130. /**
  131. * 处理数据
  132. * @access protected
  133. * @param mixed $data 要处理的数据
  134. * @return mixed
  135. */
  136. protected function output($data)
  137. {
  138. return $data;
  139. }
  140. /**
  141. * 输出数据
  142. * @access protected
  143. * @param string $data 要处理的数据
  144. * @return void
  145. */
  146. protected function sendData(string $data): void
  147. {
  148. echo $data;
  149. }
  150. /**
  151. * 输出的参数
  152. * @access public
  153. * @param mixed $options 输出参数
  154. * @return $this
  155. */
  156. public function options(array $options = [])
  157. {
  158. $this->options = array_merge($this->options, $options);
  159. return $this;
  160. }
  161. /**
  162. * 输出数据设置
  163. * @access public
  164. * @param mixed $data 输出数据
  165. * @return $this
  166. */
  167. public function data($data)
  168. {
  169. $this->data = $data;
  170. return $this;
  171. }
  172. /**
  173. * 是否允许请求缓存
  174. * @access public
  175. * @param bool $cache 允许请求缓存
  176. * @return $this
  177. */
  178. public function allowCache(bool $cache)
  179. {
  180. $this->allowCache = $cache;
  181. return $this;
  182. }
  183. /**
  184. * 是否允许请求缓存
  185. * @access public
  186. * @return $this
  187. */
  188. public function isAllowCache()
  189. {
  190. return $this->allowCache;
  191. }
  192. /**
  193. * 设置Cookie
  194. * @access public
  195. * @param string $name cookie名称
  196. * @param string $value cookie值
  197. * @param mixed $option 可选参数
  198. * @return $this
  199. */
  200. public function cookie(string $name, string $value, $option = null)
  201. {
  202. $this->cookie->set($name, $value, $option);
  203. return $this;
  204. }
  205. /**
  206. * 设置响应头
  207. * @access public
  208. * @param array $header 参数
  209. * @return $this
  210. */
  211. public function header(array $header = [])
  212. {
  213. $this->header = array_merge($this->header, $header);
  214. return $this;
  215. }
  216. /**
  217. * 设置页面输出内容
  218. * @access public
  219. * @param mixed $content
  220. * @return $this
  221. */
  222. public function content($content)
  223. {
  224. if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([
  225. $content,
  226. '__toString',
  227. ])
  228. ) {
  229. throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content)));
  230. }
  231. $this->content = (string) $content;
  232. return $this;
  233. }
  234. /**
  235. * 发送HTTP状态
  236. * @access public
  237. * @param integer $code 状态码
  238. * @return $this
  239. */
  240. public function code(int $code)
  241. {
  242. $this->code = $code;
  243. return $this;
  244. }
  245. /**
  246. * LastModified
  247. * @access public
  248. * @param string $time
  249. * @return $this
  250. */
  251. public function lastModified(string $time)
  252. {
  253. $this->header['Last-Modified'] = $time;
  254. return $this;
  255. }
  256. /**
  257. * Expires
  258. * @access public
  259. * @param string $time
  260. * @return $this
  261. */
  262. public function expires(string $time)
  263. {
  264. $this->header['Expires'] = $time;
  265. return $this;
  266. }
  267. /**
  268. * ETag
  269. * @access public
  270. * @param string $eTag
  271. * @return $this
  272. */
  273. public function eTag(string $eTag)
  274. {
  275. $this->header['ETag'] = $eTag;
  276. return $this;
  277. }
  278. /**
  279. * 页面缓存控制
  280. * @access public
  281. * @param string $cache 状态码
  282. * @return $this
  283. */
  284. public function cacheControl(string $cache)
  285. {
  286. $this->header['Cache-control'] = $cache;
  287. return $this;
  288. }
  289. /**
  290. * 页面输出类型
  291. * @access public
  292. * @param string $contentType 输出类型
  293. * @param string $charset 输出编码
  294. * @return $this
  295. */
  296. public function contentType(string $contentType, string $charset = 'utf-8')
  297. {
  298. $this->header['Content-Type'] = $contentType . '; charset=' . $charset;
  299. return $this;
  300. }
  301. /**
  302. * 获取头部信息
  303. * @access public
  304. * @param string $name 头部名称
  305. * @return mixed
  306. */
  307. public function getHeader(string $name = '')
  308. {
  309. if (!empty($name)) {
  310. return $this->header[$name] ?? null;
  311. }
  312. return $this->header;
  313. }
  314. /**
  315. * 获取原始数据
  316. * @access public
  317. * @return mixed
  318. */
  319. public function getData()
  320. {
  321. return $this->data;
  322. }
  323. /**
  324. * 获取输出数据
  325. * @access public
  326. * @return string
  327. */
  328. public function getContent(): string
  329. {
  330. if (null == $this->content) {
  331. $content = $this->output($this->data);
  332. if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([
  333. $content,
  334. '__toString',
  335. ])
  336. ) {
  337. throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content)));
  338. }
  339. $this->content = (string) $content;
  340. }
  341. return $this->content;
  342. }
  343. /**
  344. * 获取状态码
  345. * @access public
  346. * @return integer
  347. */
  348. public function getCode(): int
  349. {
  350. return $this->code;
  351. }
  352. }