Response.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  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;
  12. use think\response\Json as JsonResponse;
  13. use think\response\Jsonp as JsonpResponse;
  14. use think\response\Redirect as RedirectResponse;
  15. use think\response\View as ViewResponse;
  16. use think\response\Xml as XmlResponse;
  17. class Response
  18. {
  19. // 原始数据
  20. protected $data;
  21. // 当前的contentType
  22. protected $contentType = 'text/html';
  23. // 字符集
  24. protected $charset = 'utf-8';
  25. //状态
  26. protected $code = 200;
  27. // 输出参数
  28. protected $options = [];
  29. // header参数
  30. protected $header = [];
  31. protected $content = null;
  32. /**
  33. * 构造函数
  34. * @access public
  35. * @param mixed $data 输出数据
  36. * @param int $code
  37. * @param array $header
  38. * @param array $options 输出参数
  39. */
  40. public function __construct($data = '', $code = 200, array $header = [], $options = [])
  41. {
  42. $this->data($data);
  43. if (!empty($options)) {
  44. $this->options = array_merge($this->options, $options);
  45. }
  46. $this->contentType($this->contentType, $this->charset);
  47. $this->header = array_merge($this->header, $header);
  48. $this->code = $code;
  49. }
  50. /**
  51. * 创建Response对象
  52. * @access public
  53. * @param mixed $data 输出数据
  54. * @param string $type 输出类型
  55. * @param int $code
  56. * @param array $header
  57. * @param array $options 输出参数
  58. * @return Response|JsonResponse|ViewResponse|XmlResponse|RedirectResponse|JsonpResponse
  59. */
  60. public static function create($data = '', $type = '', $code = 200, array $header = [], $options = [])
  61. {
  62. $class = false !== strpos($type, '\\') ? $type : '\\think\\response\\' . ucfirst(strtolower($type));
  63. if (class_exists($class)) {
  64. $response = new $class($data, $code, $header, $options);
  65. } else {
  66. $response = new static($data, $code, $header, $options);
  67. }
  68. return $response;
  69. }
  70. /**
  71. * 发送数据到客户端
  72. * @access public
  73. * @return mixed
  74. * @throws \InvalidArgumentException
  75. */
  76. public function send()
  77. {
  78. // 监听response_send
  79. Hook::listen('response_send', $this);
  80. // 处理输出数据
  81. $data = $this->getContent();
  82. // Trace调试注入
  83. if (Env::get('app_trace', Config::get('app_trace'))) {
  84. Debug::inject($this, $data);
  85. }
  86. if (200 == $this->code) {
  87. $cache = Request::instance()->getCache();
  88. if ($cache) {
  89. $this->header['Cache-Control'] = 'max-age=' . $cache[1] . ',must-revalidate';
  90. $this->header['Last-Modified'] = gmdate('D, d M Y H:i:s') . ' GMT';
  91. $this->header['Expires'] = gmdate('D, d M Y H:i:s', $_SERVER['REQUEST_TIME'] + $cache[1]) . ' GMT';
  92. Cache::tag($cache[2])->set($cache[0], [$data, $this->header], $cache[1]);
  93. }
  94. }
  95. if (!headers_sent() && !empty($this->header)) {
  96. // 发送状态码
  97. http_response_code($this->code);
  98. // 发送头部信息
  99. foreach ($this->header as $name => $val) {
  100. if (is_null($val)) {
  101. header($name);
  102. } else {
  103. header($name . ':' . $val);
  104. }
  105. }
  106. }
  107. echo $data;
  108. if (function_exists('fastcgi_finish_request')) {
  109. // 提高页面响应
  110. fastcgi_finish_request();
  111. }
  112. // 监听response_end
  113. Hook::listen('response_end', $this);
  114. // 清空当次请求有效的数据
  115. if (!($this instanceof RedirectResponse)) {
  116. Session::flush();
  117. }
  118. }
  119. /**
  120. * 处理数据
  121. * @access protected
  122. * @param mixed $data 要处理的数据
  123. * @return mixed
  124. */
  125. protected function output($data)
  126. {
  127. return $data;
  128. }
  129. /**
  130. * 输出的参数
  131. * @access public
  132. * @param mixed $options 输出参数
  133. * @return $this
  134. */
  135. public function options($options = [])
  136. {
  137. $this->options = array_merge($this->options, $options);
  138. return $this;
  139. }
  140. /**
  141. * 输出数据设置
  142. * @access public
  143. * @param mixed $data 输出数据
  144. * @return $this
  145. */
  146. public function data($data)
  147. {
  148. $this->data = $data;
  149. return $this;
  150. }
  151. /**
  152. * 设置响应头
  153. * @access public
  154. * @param string|array $name 参数名
  155. * @param string $value 参数值
  156. * @return $this
  157. */
  158. public function header($name, $value = null)
  159. {
  160. if (is_array($name)) {
  161. $this->header = array_merge($this->header, $name);
  162. } else {
  163. $this->header[$name] = $value;
  164. }
  165. return $this;
  166. }
  167. /**
  168. * 设置页面输出内容
  169. * @param $content
  170. * @return $this
  171. */
  172. public function content($content)
  173. {
  174. if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([
  175. $content,
  176. '__toString',
  177. ])
  178. ) {
  179. throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content)));
  180. }
  181. $this->content = (string) $content;
  182. return $this;
  183. }
  184. /**
  185. * 发送HTTP状态
  186. * @param integer $code 状态码
  187. * @return $this
  188. */
  189. public function code($code)
  190. {
  191. $this->code = $code;
  192. return $this;
  193. }
  194. /**
  195. * LastModified
  196. * @param string $time
  197. * @return $this
  198. */
  199. public function lastModified($time)
  200. {
  201. $this->header['Last-Modified'] = $time;
  202. return $this;
  203. }
  204. /**
  205. * Expires
  206. * @param string $time
  207. * @return $this
  208. */
  209. public function expires($time)
  210. {
  211. $this->header['Expires'] = $time;
  212. return $this;
  213. }
  214. /**
  215. * ETag
  216. * @param string $eTag
  217. * @return $this
  218. */
  219. public function eTag($eTag)
  220. {
  221. $this->header['ETag'] = $eTag;
  222. return $this;
  223. }
  224. /**
  225. * 页面缓存控制
  226. * @param string $cache 状态码
  227. * @return $this
  228. */
  229. public function cacheControl($cache)
  230. {
  231. $this->header['Cache-control'] = $cache;
  232. return $this;
  233. }
  234. /**
  235. * 页面输出类型
  236. * @param string $contentType 输出类型
  237. * @param string $charset 输出编码
  238. * @return $this
  239. */
  240. public function contentType($contentType, $charset = 'utf-8')
  241. {
  242. $this->header['Content-Type'] = $contentType . '; charset=' . $charset;
  243. return $this;
  244. }
  245. /**
  246. * 获取头部信息
  247. * @param string $name 头部名称
  248. * @return mixed
  249. */
  250. public function getHeader($name = '')
  251. {
  252. if (!empty($name)) {
  253. return isset($this->header[$name]) ? $this->header[$name] : null;
  254. } else {
  255. return $this->header;
  256. }
  257. }
  258. /**
  259. * 获取原始数据
  260. * @return mixed
  261. */
  262. public function getData()
  263. {
  264. return $this->data;
  265. }
  266. /**
  267. * 获取输出数据
  268. * @return mixed
  269. */
  270. public function getContent()
  271. {
  272. if (null == $this->content) {
  273. $content = $this->output($this->data);
  274. if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([
  275. $content,
  276. '__toString',
  277. ])
  278. ) {
  279. throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content)));
  280. }
  281. $this->content = (string) $content;
  282. }
  283. return $this->content;
  284. }
  285. /**
  286. * 获取状态码
  287. * @return integer
  288. */
  289. public function getCode()
  290. {
  291. return $this->code;
  292. }
  293. }