Several MVC classes added, entryPoint method rewritten
[shipsimu.git] / inc / classes / main / response / class_HttpResponse.php
1 <?php
2 /**
3  * A class for an HTTP response on an HTTP request
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
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
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class HttpResponse extends BaseFrameworkSystem implements Responseable {
25         /**
26          * Response status
27          */
28         private $responseStatus = "200 OK";
29
30         /**
31          * Array with all headers
32          */
33         private $responseHeaders = array();
34
35         /**
36          * Body of the response
37          */
38         private $responseBody = "";
39
40         /**
41          * Instance of the template engine
42          */
43         private $templateEngine = null;
44
45         /**
46          * Protected constructor
47          *
48          * @return      void
49          */
50         protected function __construct () {
51                 // Call parent constructor
52                 parent::__construct(__CLASS__);
53
54                 // Set part description
55                 $this->setObjectDescription("HTTP-Antwort");
56
57                 // Create unique ID number
58                 $this->createUniqueID();
59
60                 // Clean up a little
61                 $this->removeNumberFormaters();
62                 $this->removeSystemArray();
63         }
64
65         /**
66          * Creates an object of this class
67          *
68          * @param       $appInstance            An instance of a manageable application
69          * @return      $responseInstance       A prepared instance of this class
70          */
71         public final static function createHttpResponse (ManageableApplication $appInstance) {
72                 // Get a new instance
73                 $responseInstance = new HttpResponse();
74
75                 // Initialize the template engine here
76                 $responseInstance->initTemplateEngine($appInstance);
77
78                 // Return the prepared instance
79                 return $responseInstance;
80         }
81
82         /**
83          * Setter for status
84          *
85          * @param       $status         New response status
86          * @return      void
87          */
88         public final function setResponseStatus ($status) {
89                 $this->responseStatus = (string) $status;
90         }
91
92         /**
93          * Add header element
94          *
95          * @param       $name   Name of header element
96          * @param       $value  Value of header element
97          * @return      void
98          */
99         public final function addHeader ($name, $value) {
100                 $this->responseHeaders[$name] = $value;
101         }
102
103         /**
104          * "Writes" data to the response body
105          *
106          * @param       $output         Output we shall sent in the HTTP response
107          * @return      void
108          */
109         public function writeToBody ($output) {
110                 $this->responseBody .= $output;
111         }
112
113         /**
114          * Flushs the cached HTTP response to the outer world
115          *
116          * @param       $foce   Wether we shall force the output or abort if headers are
117          *                                      already sent with an exception
118          * @return      void
119          * @throws      ResponseHeadersAlreadySentException             Thrown if headers are
120          *                                                                                                      already sent
121          */
122         public function flushResponse ($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()) {
127                         // Send headers out
128                         header("HTTP/1.0 {$this->responseStatus}");
129                         foreach ($this->responseHeaders as $name=>$value) {
130                                 header("{$name}: {$value}");
131                         }
132                 }
133
134                 // Flush the output to the world
135                 $this->getWebOutputInstance()->output($this->responseBody);
136                 $this->reponseBody = "";
137                 $this->responseHeaders = array();
138         }
139
140         /**
141          * Initializes the template engine instance
142          *
143          * @param       $appInstance    An instance of a manageable application
144          * @return      void
145          */
146         public final function initTemplateEngine (ManageableApplication $appInstance) {
147                 $this->templateEngine = $this->prepareTemplateEngine($appInstance);
148         }
149
150         /**
151          * Getter for the template engine instance
152          *
153          * @return      $templateEngine         An instance of the used template engine
154          */
155         public final function getTemplateEngine () {
156                 return $this->templateEngine;
157         }
158 }
159
160 // [EOF]
161 ?>