04cb16f9ca8ce0cfef2fa09b46114ab5d90a0f9e
[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.3.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.mxchange.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          * Private constructor
42          *
43          * @return      void
44          */
45         private function __construct () {
46                 // Call parent constructor
47                 parent::constructor(__CLASS__);
48
49                 // Set part description
50                 $this->setObjectDescription("HTTP-Antwort");
51
52                 // Create unique ID number
53                 $this->createUniqueID();
54
55                 // Clean up a little
56                 $this->removeNumberFormaters();
57                 $this->removeSystemArray();
58         }
59
60         /**
61          * Creates an object of this class
62          *
63          * @return      $responseInstance       A prepared instance of this class
64          */
65         public final static function createHttpResponse () {
66                 // Get a new instance
67                 $responseInstance = new HttpResponse();
68
69                 // Return the prepared instance
70                 return $responseInstance;
71         }
72
73         /**
74          * Setter for status
75          *
76          * @param       $status         New response status
77          * @return      void
78          */
79         public final function setResponseStatus ($status) {
80                 $this->responseStatus = (string) $status;
81         }
82
83         /**
84          * Add header element
85          *
86          * @param       $name   Name of header element
87          * @param       $value  Value of header element
88          * @return      void
89          */
90         public final function addHeader ($name, $value) {
91                 $this->responseHeaders[$name] = $value;
92         }
93
94         /**
95          * "Writes" data to the response body
96          *
97          * @param       $output         Output we shall sent in the HTTP response
98          * @return      void
99          */
100         public function writeToBody ($output) {
101                 $this->responseBody .= $output;
102         }
103
104         /**
105          * Flushs the cached HTTP response to the outer world
106          *
107          * @param       $foce   Wether we shall force the output or abort if headers are
108          *                                      already sent with an exception
109          * @return      void
110          * @throws      ResponseHeadersAlreadySentException             Thrown if headers are
111          *                                                                                                      already sent
112          */
113         public function flushResponse($force=false) {
114                 if ((headers_sent()) && (!$force)) {
115                         // Headers are already sent!
116                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
117                 } elseif (!headers_sent()) {
118                         // Send headers out
119                         header("HTTP/1.0 {$this->responseStatus}");
120                         foreach ($this->responseHeaders as $name=>$value) {
121                                 header("{$name}: {$value}");
122                         }
123                 }
124
125                 // Flush the output to the world
126                 $this->getWebOutputInstance()->output($this->responseBody);
127                 $this->reponseBody = "";
128                 $this->responseHeaders = array();
129         }
130 }
131
132 // [EOF]
133 ?>