AbstractResponse.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ KOTv+gKRXdVXLBg1e5E6zgGghZwBUSof3WZZvq9W6GI=
  5. *
  6. * @link
  7. * @copyright Copyright (c) 2014 CodeCube SRL
  8. * @license
  9. *
  10. * @version 1.2
  11. */
  12. /**
  13. * abstract response class
  14. */
  15. namespace Cube\Controller\Response;
  16. abstract class AbstractResponse implements ResponseInterface
  17. {
  18. /**
  19. * http 1.1
  20. */
  21. const HTTP_11 = "HTTP/1.1";
  22. /**
  23. * http 1.0
  24. */
  25. const HTTP_10 = "HTTP/1.0";
  26. /**
  27. *
  28. * http version
  29. *
  30. * @var string
  31. */
  32. protected $_version;
  33. /**
  34. *
  35. * headers
  36. *
  37. * @var array
  38. */
  39. protected $_headers = array();
  40. /**
  41. *
  42. * body variable
  43. *
  44. * @var string
  45. */
  46. protected $_body = null;
  47. /**
  48. *
  49. * http response code
  50. *
  51. * @var integer
  52. */
  53. protected $_responseCode;
  54. /**
  55. *
  56. * class constructor
  57. *
  58. * @param string $version
  59. */
  60. public function __construct($version = self::HTTP_11)
  61. {
  62. $this->_version = $version;
  63. }
  64. /**
  65. *
  66. * get http version
  67. *
  68. * @return string
  69. */
  70. public function getVersion()
  71. {
  72. return $this->_version;
  73. }
  74. /**
  75. *
  76. * get headers in array format
  77. *
  78. * @return array
  79. */
  80. public function getHeaders()
  81. {
  82. return $this->_headers;
  83. }
  84. /**
  85. *
  86. * set header, clear all previous headers
  87. *
  88. * @param string $header
  89. *
  90. * @return \Cube\Controller\Response\AbstractResponse
  91. */
  92. public function setHeader($header)
  93. {
  94. $this->clearHeaders()
  95. ->addHeader($header);
  96. return $this;
  97. }
  98. /**
  99. *
  100. * add new header line
  101. * 7.2: if we have 404 in the header, empty the content automatically (workaround for fastcgi header status codes)
  102. *
  103. * @param string $header
  104. *
  105. * @return \Cube\Controller\Response\AbstractResponse
  106. */
  107. public function addHeader($header)
  108. {
  109. $this->_headers[] = "$header";
  110. return $this;
  111. }
  112. /**
  113. *
  114. * add multiple headers
  115. *
  116. * @param array $headers
  117. *
  118. * @return \Cube\Controller\Response\AbstractResponse
  119. */
  120. public function addHeaders(array $headers)
  121. {
  122. foreach ($headers as $header) {
  123. $this->addHeader($header);
  124. }
  125. return $this;
  126. }
  127. /**
  128. *
  129. * clear all headers
  130. *
  131. * @return \Cube\Controller\Response\AbstractResponse
  132. */
  133. public function clearHeaders()
  134. {
  135. $this->_headers = array();
  136. return $this;
  137. }
  138. /**
  139. * clear specified header
  140. *
  141. * @param string $header
  142. *
  143. * @return \Cube\Controller\Response\AbstractResponse
  144. */
  145. public function clearHeader($header)
  146. {
  147. $key = array_search($header, $this->_headers);
  148. if ($key !== false) {
  149. unset($this->_headers[$key]);
  150. }
  151. return $this;
  152. }
  153. /**
  154. *
  155. * get body variable
  156. *
  157. * @return string
  158. */
  159. public function getBody()
  160. {
  161. return $this->_body;
  162. }
  163. /**
  164. *
  165. * set body variable, reset if it already had content
  166. *
  167. * @param string $body
  168. *
  169. * @return \Cube\Controller\Response\AbstractResponse
  170. */
  171. public function setBody($body)
  172. {
  173. $this->_body = (string)$body;
  174. return $this;
  175. }
  176. /**
  177. *
  178. * append content to the body
  179. *
  180. * @param string $body
  181. *
  182. * @return \Cube\Controller\Response\AbstractResponse
  183. */
  184. public function appendBody($body)
  185. {
  186. if (!isset($this->_body)) {
  187. $this->setBody($body);
  188. }
  189. else {
  190. $this->_body .= (string)$body;
  191. }
  192. return $this;
  193. }
  194. /**
  195. *
  196. * clear body variable
  197. *
  198. * @return \Cube\Controller\Response\AbstractResponse
  199. */
  200. public function clearBody()
  201. {
  202. $this->_body = null;
  203. return $this;
  204. }
  205. /**
  206. *
  207. * get the http response code (if set)
  208. *
  209. * @return integer
  210. */
  211. public function getResponseCode()
  212. {
  213. return $this->_responseCode;
  214. }
  215. /**
  216. *
  217. * set http response code
  218. *
  219. * @param integer $responseCode
  220. *
  221. * @throws \InvalidArgumentException
  222. * @return $this
  223. */
  224. public function setResponseCode($responseCode)
  225. {
  226. if ($responseCode >= 100 && $responseCode <= 599) {
  227. $this->_responseCode = $responseCode;
  228. }
  229. else {
  230. throw new \InvalidArgumentException(
  231. sprintf("Invalid HTTP response code set (%s).", $responseCode));
  232. }
  233. return $this;
  234. }
  235. /**
  236. *
  237. * send headers
  238. *
  239. * @return \Cube\Controller\Response\AbstractResponse
  240. * @throws \RuntimeException
  241. */
  242. public function sendHeaders()
  243. {
  244. $responseCodeSent = false;
  245. if (!headers_sent()) {
  246. foreach ($this->_headers as $header) {
  247. if (isset($this->_responseCode) && $responseCodeSent === false) {
  248. header($header, true, $this->_responseCode);
  249. $responseCodeSent = true;
  250. }
  251. else {
  252. header($header, true);
  253. }
  254. }
  255. }
  256. else {
  257. throw new \RuntimeException('Cannot call "sendHeaders" function. Headers already sent.');
  258. }
  259. return $this;
  260. }
  261. /**
  262. *
  263. * set redirect url and code
  264. *
  265. * @param string $url
  266. * @param int $code
  267. *
  268. * @return \Cube\Controller\Response\AbstractResponse
  269. */
  270. public function setRedirect($url, $code = 302)
  271. {
  272. $this->clearHeaders()
  273. ->addHeader('Location: ' . $url)
  274. ->setResponseCode($code);
  275. return $this;
  276. }
  277. /**
  278. *
  279. * output body
  280. *
  281. * @return string
  282. */
  283. public function outputBody()
  284. {
  285. echo $this->_body;
  286. }
  287. /**
  288. *
  289. * send response
  290. */
  291. public function send()
  292. {
  293. $this->sendHeaders()
  294. ->outputBody();
  295. }
  296. }