3 * A class for an HTTP response on an HTTP request
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class HttpResponse extends BaseFrameworkSystem implements Responseable {
28 private $responseStatus = "200 OK";
31 * Array with all headers
33 private $responseHeaders = array();
36 * Body of the response
38 private $responseBody = "";
41 * Instance of the template engine
43 private $templateEngine = null;
46 * Protected constructor
50 protected function __construct () {
51 // Call parent constructor
52 parent::__construct(__CLASS__);
54 // Set part description
55 $this->setObjectDescription("HTTP-Antwort");
57 // Create unique ID number
58 $this->createUniqueID();
61 $this->removeNumberFormaters();
62 $this->removeSystemArray();
66 * Creates an object of this class
68 * @param $appInstance An instance of a manageable application
69 * @return $responseInstance A prepared instance of this class
71 public final static function createHttpResponse (ManageableApplication $appInstance) {
73 $responseInstance = new HttpResponse();
75 // Initialize the template engine here
76 $responseInstance->initTemplateEngine($appInstance);
78 // Return the prepared instance
79 return $responseInstance;
85 * @param $status New response status
88 public final function setResponseStatus ($status) {
89 $this->responseStatus = (string) $status;
95 * @param $name Name of header element
96 * @param $value Value of header element
99 public final function addHeader ($name, $value) {
100 $this->responseHeaders[$name] = $value;
104 * "Writes" data to the response body
106 * @param $output Output we shall sent in the HTTP response
109 public function writeToBody ($output) {
110 $this->responseBody .= $output;
114 * Flushs the cached HTTP response to the outer world
116 * @param $foce Wether we shall force the output or abort if headers are
117 * already sent with an exception
119 * @throws ResponseHeadersAlreadySentException Thrown if headers are
122 public function flushBuffer ($force=false) {
123 if ((headers_sent()) && (!$force)) {
124 // Headers are already sent!
125 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
126 } elseif (!headers_sent()) {
128 header("HTTP/1.0 {$this->responseStatus}");
129 foreach ($this->responseHeaders as $name=>$value) {
130 header("{$name}: {$value}");
134 // Flush the output to the world
135 $this->getWebOutputInstance()->output($this->responseBody);
136 $this->reponseBody = "";
137 $this->responseHeaders = array();
141 * Initializes the template engine instance
143 * @param $appInstance An instance of a manageable application
146 public final function initTemplateEngine (ManageableApplication $appInstance) {
147 $this->templateEngine = $this->prepareTemplateEngine($appInstance);
151 * Getter for the template engine instance
153 * @return $templateEngine An instance of the used template engine
155 public final function getTemplateEngine () {
156 return $this->templateEngine;