Response.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace Qii\Response;
  3. class Response
  4. {
  5. /**
  6. * Default body name
  7. */
  8. const DEFAULT_BODY = 'html';
  9. /**
  10. * Body content
  11. * @var array
  12. */
  13. protected $_body = array();
  14. /**
  15. * Array of headers. Each header is an array with keys 'name' and 'value'
  16. * @var array
  17. */
  18. protected $_headers = array();
  19. /**
  20. * Determine to send the headers or not
  21. * @var unknown_type
  22. */
  23. protected $_sendHeader = false;
  24. public function __construct()
  25. {
  26. }
  27. /**
  28. * Append content to the body content
  29. *
  30. * @param string $content
  31. * @param string $key
  32. * @return Qii_Response_Abstract
  33. */
  34. public function appendBody($body, $key = NULL)
  35. {
  36. if (!strlen($key)) {
  37. $key = self::DEFAULT_BODY;
  38. }
  39. if (!isset($this->_body[$key])) {
  40. $this->_body[$key] = '';
  41. }
  42. $this->_body[$key] .= (string) $body;
  43. return $this;
  44. }
  45. /**
  46. * Clear the entire body
  47. *
  48. * @param string $key
  49. * @return boolean
  50. */
  51. public function clearBody($key = NULL)
  52. {
  53. if (strlen($key)) {
  54. if (array_key_exists($key, $this->_body)) {
  55. unset($this->_body[$key]);
  56. }
  57. } else {
  58. $this->_body = array();
  59. }
  60. return true;
  61. }
  62. /**
  63. * Clear headers
  64. *
  65. * @return Qii\Response\Abstract
  66. */
  67. public function clearHeaders()
  68. {
  69. $this->_headers = array();
  70. return $this;
  71. }
  72. /**
  73. * Return the body content
  74. *
  75. * @param string $key
  76. * @return string
  77. */
  78. public function getBody($key = NULL)
  79. {
  80. if (!strlen($key)) {
  81. $key = self::DEFAULT_BODY;
  82. }
  83. return array_key_exists($key, $this->_body) ? $this->_body[$key] : null;
  84. }
  85. /**
  86. * Return array of headers; see {@link $_headers} for format
  87. *
  88. * @return array
  89. */
  90. public function getHeader()
  91. {
  92. return $this->_headers;
  93. }
  94. /**
  95. * Prepend content the body
  96. *
  97. * @param string $body
  98. * @param string $key
  99. * @return Qii_Response_Abstract
  100. */
  101. public function prependBody($body, $key = null)
  102. {
  103. if (!strlen($key)) {
  104. $key = self::DEFAULT_BODY;
  105. }
  106. if (!isset($this->_body[$key])) {
  107. $this->_body[$key] = '';
  108. }
  109. $this->_body[$key] = $body . $this->_body[$key];
  110. return $this;
  111. }
  112. /**
  113. * Send the response, including all headers
  114. *
  115. * @return void
  116. */
  117. public function response()
  118. {
  119. if ($this->_sendHeader == true) {
  120. $this->sendHeaders();
  121. }
  122. foreach ($this->_body as $key => $body) {
  123. echo $body;
  124. }
  125. }
  126. public function setAllHeaders()
  127. {
  128. return false;
  129. }
  130. /**
  131. * Set body content
  132. *
  133. * @param string $body
  134. * @param string $key
  135. * @return Qii_Response_Abstract
  136. */
  137. public function setBody($body, $key = NULL)
  138. {
  139. if (!strlen($key)) {
  140. $key = self::DEFAULT_BODY;
  141. }
  142. $this->_body[$key] = (string) $body;
  143. return $this;
  144. }
  145. /**
  146. * Set a header
  147. *
  148. * If $replace is true, replaces any headers already defined with that
  149. * $name.
  150. *
  151. * @param string $name
  152. * @param string $value
  153. * @param boolean $replace
  154. * @return Qii_Response_Abstract
  155. */
  156. public function setHeader($name, $value, $replace = false)
  157. {
  158. $name = $this->_normalizeHeader($name);
  159. $value = (string) $value;
  160. if ($replace) {
  161. foreach ($this->_headers as $key => $header) {
  162. if ($name == $header['name']) {
  163. unset($this->_headers[$key]);
  164. }
  165. }
  166. }
  167. $this->_headers[] = array(
  168. 'name' => $name,
  169. 'value' => $value,
  170. 'replace' => $replace
  171. );
  172. return $this;
  173. }
  174. /**
  175. * Set redirect URL
  176. *
  177. * Sets Location header. Forces replacement of any prior redirects.
  178. *
  179. * @param string $url
  180. * @return Qii_Response_Abstract
  181. */
  182. public function setRedirect($url)
  183. {
  184. $this->setHeader('Location', $url, true);
  185. return $this;
  186. }
  187. /**
  188. * Magic __toString functionality
  189. *
  190. * Returns response value as string
  191. * using output buffering.
  192. *
  193. * @return string
  194. */
  195. public function __toString()
  196. {
  197. ob_start();
  198. $this->response();
  199. return ob_get_clean();
  200. }
  201. /**
  202. * Normalize a header name
  203. *
  204. * Normalizes a header name to X-Capitalized-Names
  205. *
  206. * @param string $name
  207. * @return string
  208. */
  209. protected function _normalizeHeader($name)
  210. {
  211. $filtered = str_replace(array('-', '_'), ' ', (string) $name);
  212. $filtered = ucwords(strtolower($filtered));
  213. $filtered = str_replace(' ', '-', $filtered);
  214. return $filtered;
  215. }
  216. /**
  217. * Send all headers
  218. *
  219. * Sends any headers specified.
  220. * If an {@link setHttpResponseCode() HTTP response code}
  221. * has been specified, it is sent with the first header.
  222. *
  223. * @return Qii_Response_Abstract
  224. */
  225. protected function sendHeaders()
  226. {
  227. foreach ($this->_headers as $header) {
  228. header(
  229. $header['name'] . ': ' . $header['value'],
  230. $header['replace']
  231. );
  232. }
  233. return $this;
  234. }
  235. public function render()
  236. {
  237. }
  238. }