Response.php 7.5 KB

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