Response.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. namespace Qii\Base;
  3. use Qii\Response\Cli;
  4. class Response
  5. {
  6. /**
  7. * Default body name
  8. */
  9. const DEFAULT_BODY = 'html';
  10. const FORMAT_JSON = 'json';
  11. const FORMAT_HTML = 'html';
  12. public static $render = null;
  13. /**
  14. * Body content
  15. * @var array
  16. */
  17. protected $body = array();
  18. /**
  19. * data
  20. * @param array $data
  21. */
  22. protected $data = array();
  23. /**
  24. * Array of headers. Each header is an array with keys 'name' and 'value'
  25. * @var array
  26. */
  27. protected $headers = array();
  28. /**
  29. * Determine to send the headers or not
  30. * @var bool $_sendHeader 是否发送头信息
  31. */
  32. protected $_sendHeader = true;
  33. /**
  34. * @var bool|string 返回数据格式
  35. */
  36. private $format;
  37. public function __construct($data = array())
  38. {
  39. $this->format = isset($data['format']) ? $data['format'] : self::FORMAT_HTML;
  40. $this->data = $data;
  41. }
  42. /**
  43. * 设置页面渲染类
  44. * @param \Qii\View\Intf $render 渲染类
  45. */
  46. public function setRender(\Qii\View\Intf $render)
  47. {
  48. Response::$render = $render;
  49. }
  50. /**
  51. * @param bool $isSend 是否发送头信息
  52. * @return void
  53. */
  54. public function setSendHeader($isSend = true) {
  55. $this->_sendHeader = $isSend;
  56. }
  57. /**
  58. * Append content to the body content
  59. *
  60. * @param string $content
  61. * @param string $key
  62. * @return $this
  63. */
  64. public function appendBody($body, $key = NULL)
  65. {
  66. if (!strlen($key)) {
  67. $key = self::DEFAULT_BODY;
  68. }
  69. if (!isset($this->body[$key])) {
  70. $this->body[$key] = '';
  71. }
  72. $this->body[$key] .= (string)$body;
  73. return $this;
  74. }
  75. /**
  76. * Clear the entire body
  77. *
  78. * @param string $key
  79. * @return boolean
  80. */
  81. public function clearBody($key = NULL)
  82. {
  83. if (strlen($key)) {
  84. if (array_key_exists($key, $this->body)) {
  85. unset($this->body[$key]);
  86. }
  87. } else {
  88. $this->body = array();
  89. }
  90. return true;
  91. }
  92. /**
  93. * Clear headers
  94. *
  95. * @return $this
  96. */
  97. public function clearHeaders()
  98. {
  99. $this->headers = array();
  100. return $this;
  101. }
  102. /**
  103. * Return the body content
  104. *
  105. * @param string $key
  106. * @return string
  107. */
  108. public function getBody($key = NULL)
  109. {
  110. if (!strlen($key)) {
  111. $key = self::DEFAULT_BODY;
  112. }
  113. return array_key_exists($key, $this->body) ? $this->body[$key] : null;
  114. }
  115. /**
  116. * Return array of headers; see {@link $headers} for format
  117. *
  118. * @return array
  119. */
  120. public function getHeader()
  121. {
  122. return $this->headers;
  123. }
  124. /**
  125. * Prepend content the body
  126. *
  127. * @param string $body
  128. * @param string $key
  129. * @return $this
  130. */
  131. public function prependBody($body, $key = null)
  132. {
  133. if (!strlen($key)) {
  134. $key = self::DEFAULT_BODY;
  135. }
  136. if (!isset($this->body[$key])) {
  137. $this->body[$key] = '';
  138. }
  139. $this->body[$key] = $body . $this->body[$key];
  140. return $this;
  141. }
  142. /**
  143. * 是否需要view来渲染页面
  144. * @return bool
  145. */
  146. public function needRender()
  147. {
  148. if(isset($this->data['body']) && is_array($this->data['body']) && isset($this->data['body']['tpl']))
  149. {
  150. return true;
  151. }
  152. return false;
  153. }
  154. /**
  155. * Send the response, including all headers
  156. *
  157. * @return void
  158. */
  159. public function response()
  160. {
  161. if ($this->data && isset($this->data['body'])) {
  162. switch ($this->data['format']) {
  163. case self::FORMAT_JSON:
  164. $this->setHeader('Content-Type', 'text/json');
  165. $this->sendHeaders();
  166. echo json_encode($this->data['body'], JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
  167. break;
  168. default:
  169. $body = $this->data['body'];
  170. if (is_array($this->data['body'])) {
  171. $body = '';
  172. if (isset($this->data['body']['render']) && $this->data['body']['render'] instanceof \Qii\View\Intf) {
  173. Response::$render = $this->data['body']['render'];
  174. }
  175. if (Response::$render != null && Response::$render instanceof \Qii\View\Intf) {
  176. $tplData = isset($this->data['body']['tplData']) ? $this->data['body']['tplData'] : [];
  177. Response::$render->assign($tplData);
  178. $body = Response::$render->fetch($this->data['body']['tpl']);
  179. }
  180. }
  181. $this->setBody($body);
  182. echo(IS_CLI ? (new Cli())->stdout($body) : $body);
  183. break;
  184. }
  185. return;
  186. }
  187. if ($this->_sendHeader) {
  188. $this->sendHeaders();
  189. }
  190. foreach ($this->body as $body) {
  191. echo IS_CLI ? new Cli($body) : $body;
  192. }
  193. }
  194. /**
  195. * @param $headers 头信息 [['name' => 'value'], ...]
  196. * @return $this
  197. */
  198. public function setAllHeaders($headers = array())
  199. {
  200. if (is_array($headers)) {
  201. foreach ($headers as $value) {
  202. if (!isset($value['name'])) {
  203. continue;
  204. }
  205. $this->setHeader($value['name'], $value['value']);
  206. }
  207. }
  208. return $this;
  209. }
  210. /**
  211. * Set body content
  212. *
  213. * @param string $body
  214. * @param string $key
  215. * @return $this
  216. */
  217. public function setBody($body, $key = NULL)
  218. {
  219. if (!strlen($key)) {
  220. $key = self::DEFAULT_BODY;
  221. }
  222. $this->body[$key] = (string)$body;
  223. return $this;
  224. }
  225. /**
  226. * Set a header
  227. *
  228. * If $replace is true, replaces any headers already defined with that
  229. * $name.
  230. *
  231. * @param string $name 名称
  232. * @param string $value 值
  233. * @param boolean $replace 如果存在是否替换
  234. * @return $this
  235. */
  236. public function setHeader($name, $value, $replace = true)
  237. {
  238. $name = $this->_normalizeHeader($name);
  239. $value = (string)$value;
  240. if ($replace) {
  241. foreach ($this->headers as $key => $header) {
  242. if ($name == $header['name']) {
  243. unset($this->headers[$key]);
  244. }
  245. }
  246. }
  247. $this->headers[] = array(
  248. 'name' => $name,
  249. 'value' => $value,
  250. 'replace' => $replace
  251. );
  252. return $this;
  253. }
  254. /**
  255. * Set redirect URL
  256. *
  257. * Sets Location header. Forces replacement of any prior redirects.
  258. *
  259. * @param string $url
  260. * @return $this
  261. */
  262. public function setRedirect($url)
  263. {
  264. $this->setHeader('Location', $url, true);
  265. return $this;
  266. }
  267. /**
  268. * Magic __toString functionality
  269. *
  270. * Returns response value as string
  271. * using output buffering.
  272. *
  273. * @return string
  274. */
  275. public function __toString()
  276. {
  277. ob_start();
  278. $this->response();
  279. return ob_get_clean();
  280. }
  281. /**
  282. * Normalize a header name
  283. *
  284. * Normalizes a header name to X-Capitalized-Names
  285. *
  286. * @param string $name
  287. * @return string
  288. */
  289. protected function _normalizeHeader($name)
  290. {
  291. $filtered = str_replace(array('-', '_'), ' ', (string)$name);
  292. $filtered = ucwords(strtolower($filtered));
  293. return str_replace(' ', '-', $filtered);
  294. }
  295. /**
  296. * Send all headers
  297. *
  298. * Sends any headers specified.
  299. * If an {@link setHttpResponseCode() HTTP response code}
  300. * has been specified, it is sent with the first header.
  301. *
  302. * @return $this
  303. */
  304. protected function sendHeaders()
  305. {
  306. foreach ($this->headers as $key => $header) {
  307. header(
  308. $header['name'] . ': ' . $header['value'],
  309. $header['replace']
  310. );
  311. }
  312. $this->clearHeaders();
  313. return $this;
  314. }
  315. }