* @package MCMS\System\Response * @since 1.1.0.0 */ class HTTPResponse implements iResponse { private $header; private $buffer = NULL; private $bufferEnabled = true; /** * Creates a new HTTPResponse object */ public function __construct() { $this->header = new HTTPHeader(); } /** * (non-PHPdoc) * * @see \MCMS\Response\iResponse::getHeader() */ public function getHeader() { return $this->header; } /** * Add data to the output buffer * * @param string $data */ public function addToBuffer($data) { $this->buffer .= $data; if(!$this->bufferEnabled) $this->flush(); } /** * Defines the buffer content. Can only be called on empty buffers. If the new content is an instance of an iDocument * object, the header mimetype is modified to match the document if no header has been send. * * @param mixed $c the new content * @throws IllegalStateException if the */ public function setBuffer($c) { if(!is_null($this->buffer)) throw new IllegalStateException("Buffer contains data"); if ($c instanceof iDocument) { $this->buffer = $c->getContent(); if (!$this->header->isSended()) { $this->header->setHeader(new ContentType($c->getMimeType())); $this->header->setHeader(new ContentLength(strlen($this->buffer))); } } else $this->buffer = $c; } /** * Returns the content of the output buffer */ public function getBuffer() { return $this->buffer; } /** * Removes all data from the output buffer */ public function clearBuffer() { $this->buffer = NULL; } /** * Enables/disables buffering. * If set to disable, all content will be send to the client. * * @param boolean $enable */ public function setBufferEnabled($enable) { $this->bufferEnabled = (bool) $enable; if(!$this->bufferEnabled) $this->flush(); } /** * * @return boolean true if buffering is enabled */ public function isBufferEnabled() { return $this->bufferEnabled; } /** * Send the data to the client and clears the buffer. * If the header data has not been send, it will be done now. * * @see \MCMS\Response\iResponse::flush() */ public function flush() { if(!$this->header->isSended()) $this->header->send(); if(!is_null($this->buffer)) { echo $this->buffer; $this->clearBuffer(); } } }