1: <?php
2: /*
3: File: xajaxResponseManager.inc.php
4:
5: Contains the xajaxResponseManager class
6:
7: Title: xajaxResponseManager class
8:
9: Please see <copyright.inc.php> for a detailed description, copyright
10: and license information.
11: */
12:
13: /*
14: @package xajax
15: @version $Id: xajaxResponseManager.inc.php 362 2007-05-29 15:32:24Z calltoconstruct $
16: @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
17: @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson
18: @license http://www.xajaxproject.org/bsd_license.txt BSD License
19: */
20:
21: /*
22: Class: xajaxResponseManager
23:
24: This class stores and tracks the response that will be returned after
25: processing a request. The response manager represents a single point
26: of contact for working with <xajaxResponse> objects as well as
27: <xajaxCustomResponse> objects.
28: */
29: final class xajaxResponseManager
30: {
31: /*
32: Object: objResponse
33:
34: The current response object that will be sent back to the browser
35: once the request processing phase is complete.
36: */
37: private $objResponse;
38:
39: /*
40: String: sCharacterEncoding
41: */
42: private $sCharacterEncoding;
43:
44: /*
45: Boolean: bOutputEntities
46: */
47: private $bOutputEntities;
48:
49: /*
50: Array: aDebugMessages
51: */
52: private $aDebugMessages;
53:
54: /*
55: Function: xajaxResponseManager
56:
57: Construct and initialize the one and only xajaxResponseManager object.
58: */
59: private function __construct()
60: {
61: $this->objResponse = NULL;
62: $this->aDebugMessages = array();
63: }
64:
65: /*
66: Function: getInstance
67:
68: Implementation of the singleton pattern: provide a single instance of the <xajaxResponseManager>
69: to all who request it.
70: */
71: public static function &getInstance()
72: {
73: static $obj;
74: if (!$obj) {
75: $obj = new xajaxResponseManager();
76: }
77: return $obj;
78: }
79:
80: /*
81: Function: configure
82:
83: Called by the xajax object when configuration options are set in the main script. Option
84: values are passed to each of the main xajax components and stored locally as needed. The
85: <xajaxResponseManager> will track the characterEncoding and outputEntities settings.
86:
87: Parameters:
88: $sName - (string): Setting name
89: $mValue - (mixed): Value
90: */
91: public function configure($sName, $mValue)
92: {
93: if ('characterEncoding' == $sName)
94: {
95: $this->sCharacterEncoding = $mValue;
96:
97: if (isset($this->objResponse))
98: $this->objResponse->setCharacterEncoding($this->sCharacterEncoding);
99: }
100: else if ('contentType' == $sName)
101: {
102: if (isset($this->objResponse))
103: $this->objResponse->setContentType($mValue);
104: }
105: else if ('outputEntities' == $sName)
106: {
107: if (true === $mValue || false === $mValue)
108: {
109: $this->bOutputEntities = $mValue;
110:
111: if (isset($this->objResponse))
112: $this->objResponse->setOutputEntities($this->bOutputEntities);
113: }
114: }
115: $this->aSettings[$sName] = $mValue;
116:
117: }
118:
119:
120:
121: /*
122: Function: getConfiguration
123:
124: Get the current value of a configuration setting that was previously set
125: via <xajax->configure> or <xajax->configureMany>
126:
127: Parameters:
128:
129: $sName - (string): The name of the configuration setting
130:
131: Returns:
132:
133: $mValue : (mixed): The value of the setting if set, null otherwise.
134: */
135:
136: public function getConfiguration($sName)
137: {
138: if (isset($this->aSettings[$sName]))
139: return $this->aSettings[$sName];
140: return NULL;
141: }
142:
143: /*
144: Function: clear
145:
146: Clear the current response. A new response will need to be appended
147: before the request processing is complete.
148: */
149: public function clear()
150: {
151: $this->objResponse = NULL;
152: }
153:
154: /*
155: Function: append
156:
157: Used, primarily internally, to append one response object onto the end of another. You can
158: append one xajaxResponse to the end of another, or append a xajaxCustomResponse onto the end of
159: another xajaxCustomResponse. However, you cannot append a standard response object onto the end
160: of a custom response and likewise, you cannot append a custom response onto the end of a standard
161: response.
162:
163: Parameters:
164:
165: $mResponse - (object): The new response object to be added to the current response object.
166:
167: If no prior response has been appended, this response becomes the main response object to which other
168: response objects will be appended.
169: */
170: public function append($mResponse)
171: {
172: if ( $mResponse instanceof xajaxResponse ) {
173: if (NULL == $this->objResponse) {
174: $this->objResponse = $mResponse;
175: } else if ( $this->objResponse instanceof xajaxResponse ) {
176: if ($this->objResponse != $mResponse)
177: $this->objResponse->appendResponse($mResponse);
178: } else {
179: $objLanguageManager = xajaxLanguageManager::getInstance();
180: $this->debug(
181: $objLanguageManager->getText('XJXRM:MXRTERR')
182: . get_class($this->objResponse)
183: . ')'
184: );
185: }
186: } else if ( $mResponse instanceof xajaxCustomResponse ) {
187: if (NULL == $this->objResponse) {
188: $this->objResponse = $mResponse;
189: } else if ( $this->objResponse instanceof xajaxCustomResponse ) {
190: if ($this->objResponse != $mResponse)
191: $this->objResponse->appendResponse($mResponse);
192: } else {
193: $objLanguageManager = xajaxLanguageManager::getInstance();
194: $this->debug(
195: $objLanguageManager->getText('XJXRM:MXRTERR')
196: . get_class($this->objResponse)
197: . ')'
198: );
199: }
200: } else {
201: $objLanguageManager = xajaxLanguageManager::getInstance();
202: $this->debug($objLanguageManager->getText('XJXRM:IRERR'));
203: }
204: }
205:
206: /*
207: Function: debug
208:
209: Appends a debug message on the end of the debug message queue. Debug messages
210: will be sent to the client with the normal response (if the response object supports
211: the sending of debug messages, see: <xajaxResponse>)
212:
213: Parameters:
214:
215: $sMessage - (string): The text of the debug message to be sent.
216: */
217: public function debug($sMessage)
218: {
219: $this->aDebugMessages[] = $sMessage;
220: }
221:
222: /*
223: Function: send
224:
225: Prints the response object to the output stream, thus sending the response to the client.
226: */
227: public function send()
228: {
229: if (NULL != $this->objResponse) {
230: foreach ($this->aDebugMessages as $sMessage)
231: $this->objResponse->debug($sMessage);
232: $this->aDebugMessages = array();
233: $this->objResponse->printOutput();
234: }
235: }
236:
237: /*
238: Function: getCharacterEncoding
239:
240: Called automatically by new response objects as they are constructed to obtain the
241: current character encoding setting. As the character encoding is changed, the <xajaxResponseManager>
242: will automatically notify the current response object since it would have been constructed
243: prior to the setting change, see <xajaxResponseManager::configure>.
244: */
245: public function getCharacterEncoding()
246: {
247: return $this->sCharacterEncoding;
248: }
249:
250: /*
251: Function: getOutputEntities
252:
253: Called automatically by new response objects as they are constructed to obtain the
254: current output entities setting. As the output entities setting is changed, the
255: <xajaxResponseManager> will automatically notify the current response object since it would
256: have been constructed prior to the setting change, see <xajaxResponseManager::configure>.
257: */
258: public function getOutputEntities()
259: {
260: return $this->bOutputEntities;
261: }
262: }