123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- <?php
- /**
- *
- * Cube Framework $Id$ KOTv+gKRXdVXLBg1e5E6zgGghZwBUSof3WZZvq9W6GI=
- *
- * @link
- * @copyright Copyright (c) 2014 CodeCube SRL
- * @license
- *
- * @version 1.2
- */
- /**
- * abstract response class
- */
- namespace Cube\Controller\Response;
- abstract class AbstractResponse implements ResponseInterface
- {
- /**
- * http 1.1
- */
- const HTTP_11 = "HTTP/1.1";
- /**
- * http 1.0
- */
- const HTTP_10 = "HTTP/1.0";
- /**
- *
- * http version
- *
- * @var string
- */
- protected $_version;
- /**
- *
- * headers
- *
- * @var array
- */
- protected $_headers = array();
- /**
- *
- * body variable
- *
- * @var string
- */
- protected $_body = null;
- /**
- *
- * http response code
- *
- * @var integer
- */
- protected $_responseCode;
- /**
- *
- * class constructor
- *
- * @param string $version
- */
- public function __construct($version = self::HTTP_11)
- {
- $this->_version = $version;
- }
- /**
- *
- * get http version
- *
- * @return string
- */
- public function getVersion()
- {
- return $this->_version;
- }
- /**
- *
- * get headers in array format
- *
- * @return array
- */
- public function getHeaders()
- {
- return $this->_headers;
- }
- /**
- *
- * set header, clear all previous headers
- *
- * @param string $header
- *
- * @return \Cube\Controller\Response\AbstractResponse
- */
- public function setHeader($header)
- {
- $this->clearHeaders()
- ->addHeader($header);
- return $this;
- }
- /**
- *
- * add new header line
- * 7.2: if we have 404 in the header, empty the content automatically (workaround for fastcgi header status codes)
- *
- * @param string $header
- *
- * @return \Cube\Controller\Response\AbstractResponse
- */
- public function addHeader($header)
- {
- $this->_headers[] = "$header";
- return $this;
- }
- /**
- *
- * add multiple headers
- *
- * @param array $headers
- *
- * @return \Cube\Controller\Response\AbstractResponse
- */
- public function addHeaders(array $headers)
- {
- foreach ($headers as $header) {
- $this->addHeader($header);
- }
- return $this;
- }
- /**
- *
- * clear all headers
- *
- * @return \Cube\Controller\Response\AbstractResponse
- */
- public function clearHeaders()
- {
- $this->_headers = array();
- return $this;
- }
- /**
- * clear specified header
- *
- * @param string $header
- *
- * @return \Cube\Controller\Response\AbstractResponse
- */
- public function clearHeader($header)
- {
- $key = array_search($header, $this->_headers);
- if ($key !== false) {
- unset($this->_headers[$key]);
- }
- return $this;
- }
- /**
- *
- * get body variable
- *
- * @return string
- */
- public function getBody()
- {
- return $this->_body;
- }
- /**
- *
- * set body variable, reset if it already had content
- *
- * @param string $body
- *
- * @return \Cube\Controller\Response\AbstractResponse
- */
- public function setBody($body)
- {
- $this->_body = (string)$body;
- return $this;
- }
- /**
- *
- * append content to the body
- *
- * @param string $body
- *
- * @return \Cube\Controller\Response\AbstractResponse
- */
- public function appendBody($body)
- {
- if (!isset($this->_body)) {
- $this->setBody($body);
- }
- else {
- $this->_body .= (string)$body;
- }
- return $this;
- }
- /**
- *
- * clear body variable
- *
- * @return \Cube\Controller\Response\AbstractResponse
- */
- public function clearBody()
- {
- $this->_body = null;
- return $this;
- }
- /**
- *
- * get the http response code (if set)
- *
- * @return integer
- */
- public function getResponseCode()
- {
- return $this->_responseCode;
- }
- /**
- *
- * set http response code
- *
- * @param integer $responseCode
- *
- * @throws \InvalidArgumentException
- * @return $this
- */
- public function setResponseCode($responseCode)
- {
- if ($responseCode >= 100 && $responseCode <= 599) {
- $this->_responseCode = $responseCode;
- }
- else {
- throw new \InvalidArgumentException(
- sprintf("Invalid HTTP response code set (%s).", $responseCode));
- }
- return $this;
- }
- /**
- *
- * send headers
- *
- * @return \Cube\Controller\Response\AbstractResponse
- * @throws \RuntimeException
- */
- public function sendHeaders()
- {
- $responseCodeSent = false;
- if (!headers_sent()) {
- foreach ($this->_headers as $header) {
- if (isset($this->_responseCode) && $responseCodeSent === false) {
- header($header, true, $this->_responseCode);
- $responseCodeSent = true;
- }
- else {
- header($header, true);
- }
- }
- }
- else {
- throw new \RuntimeException('Cannot call "sendHeaders" function. Headers already sent.');
- }
- return $this;
- }
- /**
- *
- * set redirect url and code
- *
- * @param string $url
- * @param int $code
- *
- * @return \Cube\Controller\Response\AbstractResponse
- */
- public function setRedirect($url, $code = 302)
- {
- $this->clearHeaders()
- ->addHeader('Location: ' . $url)
- ->setResponseCode($code);
- return $this;
- }
- /**
- *
- * output body
- *
- * @return string
- */
- public function outputBody()
- {
- echo $this->_body;
- }
- /**
- *
- * send response
- */
- public function send()
- {
- $this->sendHeaders()
- ->outputBody();
- }
- }
|