Response.php 8.3 KB

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