3 namespace Org\Mxchange\CoreFramework\Response;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
8 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
11 * A generic request class
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 * The extended headers are taken from phpMyAdmin setup tool, written by
33 * Michal Cihar <michal@cihar.com>, licensed under GNU GPL 2.0.
35 abstract class BaseResponse extends BaseFrameworkSystem {
39 private $responseStatus = '200 OK';
42 * Array with all headers
44 private $responseHeaders = array();
47 * Cookies we shall sent out
49 private $cookies = array();
52 * Body of the response
54 private $responseBody = '';
57 * Instance of the template engine
59 private $templateInstance = NULL;
64 private $responseType = 'invalid';
67 * Protected constructor
69 * @param $className Name of the concrete response
72 protected function __construct ($className) {
73 // Call parent constructor
74 parent::__construct($className);
80 * @param $status New response status
83 public final function setResponseStatus ($status) {
84 $this->responseStatus = (string) $status;
90 * @param $name Name of header element
91 * @param $value Value of header element
94 public final function addHeader ($name, $value) {
95 $this->responseHeaders[$name] = $value;
99 * Reset the header array
103 public final function resetResponseHeaders () {
104 $this->responseHeaders = array();
108 * "Writes" data to the response body
110 * @param $output Output we shall sent in the HTTP response
113 public final function writeToBody ($output) {
114 $this->responseBody .= $output;
118 * Sets the response body to something new
120 * @param $output Output we shall sent in the HTTP response
123 public final function setResponseBody ($output) {
124 $this->responseBody = $output;
128 * Setter for response type
130 * @param $responseType Response type
133 protected final function setResponseType ($responseType) {
134 $this->responseType = $responseType;
138 * Getter for response type
140 * @param $responseType Response type
143 public final function getResponseType () {
144 return $this->responseType;
148 * Adds a fatal message id to the response. The added messages can then be
149 * processed and outputed to the world
151 * @param $messageId The message id we shall add
154 public final function addFatalMessage ($messageId) {
155 // Get application instance
156 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
158 // Adds the resolved message id to the fatal message list
159 $this->addFatalMessagePlain($applicationInstance->getLanguageInstance()->getMessage($messageId));
163 * Adds a plain fatal message id to the response
165 * @param $message The plain message we shall add
168 public final function addFatalMessagePlain ($message) {
169 // Adds the resolved message id to the fatal message list
170 $this->pushValueToGenericArrayKey('fatal_messages', 'generic', 'message', $message);
174 * Flushs the cached HTTP response to the outer world
176 * @param $force Whether we shall force the output or abort if headers are
177 * already sent with an exception
179 * @throws ResponseHeadersAlreadySentException Thrown if headers are
182 public function flushBuffer ($force = false) {
183 // Get application instance
184 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
186 // Headers already sent?
187 if ((headers_sent()) && ($force === false)) {
188 // Headers are already sent!
189 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
190 } elseif (!headers_sent()) {
192 header('HTTP/1.1 ' . $this->responseStatus);
195 $now = gmdate('D, d M Y H:i:s') . ' GMT';
197 // General header for no caching
198 $this->addHeader('Expired', $now); // RFC2616 - Section 14.21
199 $this->addHeader('Last-Modified', $now);
200 $this->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
201 $this->addHeader('Pragma', 'no-cache'); // HTTP/1.0
203 // Define the charset to be used
204 //$this->addHeader('Content-type:', sprintf("text/html; charset=%s", FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('header_charset')));
207 foreach ($this->responseHeaders as $name => $value) {
208 header($name . ': ' . $value);
209 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('name=' . $name . ',value=' . $value);
213 if (count($this->cookies) > 0) {
215 $cookieString = implode(' ', $this->cookies);
216 header('Set-Cookie: ' . $cookieString);
219 $this->cookies = array();
223 // Are there some error messages?
224 if ((!$this->isValidGenericArrayKey('fatal_messages', 'generic', 'message')) || ($this->countGenericArrayElements('fatal_messages', 'generic', 'message') == 0)) {
225 // Flush the output to the world
226 $this->getWebOutputInstance()->output($this->responseBody);
228 // Display all error messages
229 $applicationInstance->handleFatalMessages($this->getGenericArrayKey('fatal_messages', 'generic', 'message'));
231 // Send the error messages out to the world
232 $this->getWebOutputInstance()->output($this->responseBody);
235 // Clear response header and body
236 $this->setResponseBody('');
237 $this->resetResponseHeaders();
241 * "Getter" for default command
243 * @return $defaultCommand Default command for this response
245 public function determineDefaultCommand () {
246 // Get application instance
247 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
249 // Generate config key
250 $configKey = sprintf('default_%s_%s_command',
251 $applicationInstance->getAppShortName(),
252 $this->getResponseType()
255 // Get default command response
256 $defaultCommand = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configKey);
259 return $defaultCommand;