|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ResponseManager.php - Jaxon ResponsePlugin PluginManager |
|
5
|
|
|
* |
|
6
|
|
|
* This class stores and tracks the response that will be returned after processing a request. |
|
7
|
|
|
* The Response Manager represents a single point of contact for working with <ResponsePlugin> objects. |
|
8
|
|
|
* |
|
9
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
10
|
|
|
* @author Jared White |
|
|
|
|
|
|
11
|
|
|
* @author J. Max Wilson |
|
|
|
|
|
|
12
|
|
|
* @author Joseph Woolley |
|
|
|
|
|
|
13
|
|
|
* @author Steffen Konerow |
|
|
|
|
|
|
14
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
15
|
|
|
* @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson |
|
|
|
|
|
|
16
|
|
|
* @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson |
|
|
|
|
|
|
17
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
|
18
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
19
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
20
|
|
|
*/ |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
namespace Jaxon\Response; |
|
23
|
|
|
|
|
24
|
|
|
use Jaxon\Di\Container; |
|
25
|
|
|
use Jaxon\Utils\Translation\Translator; |
|
26
|
|
|
|
|
27
|
|
|
use function header; |
|
28
|
|
|
use function strlen; |
|
29
|
|
|
use function gmdate; |
|
30
|
|
|
use function get_class; |
|
31
|
|
|
|
|
32
|
|
|
class ResponseManager |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var Container |
|
36
|
|
|
*/ |
|
37
|
|
|
private $di; |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private $sCharacterEncoding; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Translator |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $xTranslator; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The current response object that will be sent back to the browser |
|
51
|
|
|
* once the request processing phase is complete |
|
52
|
|
|
* |
|
53
|
|
|
* @var AbstractResponse |
|
54
|
|
|
*/ |
|
55
|
|
|
private $xResponse = null; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* The debug messages |
|
59
|
|
|
* |
|
60
|
|
|
* @var array |
|
61
|
|
|
*/ |
|
62
|
|
|
private $aDebugMessages; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* The class constructor |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $sCharacterEncoding |
|
|
|
|
|
|
68
|
|
|
* @param Container $di |
|
|
|
|
|
|
69
|
|
|
* @param Translator $xTranslator |
|
|
|
|
|
|
70
|
|
|
*/ |
|
71
|
|
|
public function __construct(string $sCharacterEncoding, Container $di, Translator $xTranslator) |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
$this->di = $di; |
|
|
|
|
|
|
74
|
|
|
$this->sCharacterEncoding = $sCharacterEncoding; |
|
75
|
|
|
$this->xTranslator = $xTranslator; |
|
|
|
|
|
|
76
|
|
|
$this->aDebugMessages = []; |
|
|
|
|
|
|
77
|
|
|
} |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Clear the current response |
|
81
|
|
|
* |
|
82
|
|
|
* A new response will need to be appended before the request processing is complete. |
|
83
|
|
|
* |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
public function clear() |
|
87
|
|
|
{ |
|
88
|
|
|
$this->xResponse = null; |
|
89
|
|
|
} |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get the response to the Jaxon request |
|
93
|
|
|
* |
|
94
|
|
|
* @return AbstractResponse |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getResponse(): ?AbstractResponse |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->xResponse; |
|
99
|
|
|
} |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Append one response object onto the end of another |
|
103
|
|
|
* |
|
104
|
|
|
* You cannot append a given response onto the end of a response of different type. |
|
105
|
|
|
* If no prior response has been appended, this response becomes the main response |
|
106
|
|
|
* object to which other response objects will be appended. |
|
107
|
|
|
* |
|
108
|
|
|
* @param AbstractResponse $xResponse The response object to be appended |
|
|
|
|
|
|
109
|
|
|
* |
|
110
|
|
|
* @return void |
|
111
|
|
|
*/ |
|
112
|
|
|
public function append(AbstractResponse $xResponse) |
|
113
|
|
|
{ |
|
114
|
|
|
if(!$this->xResponse) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->xResponse = $xResponse; |
|
117
|
|
|
} |
|
118
|
|
|
elseif(get_class($this->xResponse) === get_class($xResponse)) |
|
119
|
|
|
{ |
|
120
|
|
|
if($this->xResponse !== $xResponse) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->xResponse->appendResponse($xResponse); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
else |
|
126
|
|
|
{ |
|
127
|
|
|
$this->debug($this->xTranslator->trans('errors.mismatch.types', ['class' => get_class($xResponse)])); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Appends a debug message on the end of the debug message queue |
|
133
|
|
|
* |
|
134
|
|
|
* Debug messages will be sent to the client with the normal response |
|
135
|
|
|
* (if the response object supports the sending of debug messages, see: <ResponsePlugin>) |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $sMessage The debug message |
|
|
|
|
|
|
138
|
|
|
* |
|
139
|
|
|
* @return void |
|
140
|
|
|
*/ |
|
141
|
|
|
public function debug(string $sMessage) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->aDebugMessages[] = $sMessage; |
|
144
|
|
|
} |
|
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Clear the response and appends a debug message on the end of the debug message queue |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $sMessage The debug message |
|
|
|
|
|
|
150
|
|
|
* |
|
151
|
|
|
* @return void |
|
152
|
|
|
*/ |
|
153
|
|
|
public function error(string $sMessage) |
|
154
|
|
|
{ |
|
155
|
|
|
$this->clear(); |
|
156
|
|
|
$this->append($this->di->newResponse()); |
|
157
|
|
|
$this->debug($sMessage); |
|
158
|
|
|
} |
|
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Prints the debug messages into the current response object |
|
162
|
|
|
* |
|
163
|
|
|
* @return void |
|
164
|
|
|
*/ |
|
165
|
|
|
public function printDebug() |
|
166
|
|
|
{ |
|
167
|
|
|
if(($this->xResponse)) |
|
168
|
|
|
{ |
|
169
|
|
|
foreach($this->aDebugMessages as $sMessage) |
|
170
|
|
|
{ |
|
171
|
|
|
$this->xResponse->debug($sMessage); |
|
172
|
|
|
} |
|
173
|
|
|
$this->aDebugMessages = []; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Sends the HTTP headers back to the browser |
|
179
|
|
|
* |
|
180
|
|
|
* @return void |
|
181
|
|
|
*/ |
|
182
|
|
|
public function sendHeaders() |
|
183
|
|
|
{ |
|
184
|
|
|
if($this->di->getRequest()->getMethod() === 'GET') |
|
185
|
|
|
{ |
|
186
|
|
|
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); |
|
187
|
|
|
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); |
|
188
|
|
|
header("Cache-Control: no-cache, must-revalidate"); |
|
189
|
|
|
header("Pragma: no-cache"); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$sCharacterSet = ''; |
|
193
|
|
|
if(strlen($this->sCharacterEncoding) > 0) |
|
194
|
|
|
{ |
|
195
|
|
|
$sCharacterSet = '; charset="' . $this->sCharacterEncoding . '"'; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
header('content-type: ' . $this->xResponse->getContentType() . ' ' . $sCharacterSet); |
|
199
|
|
|
} |
|
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Get the JSON output of the response |
|
203
|
|
|
* |
|
204
|
|
|
* @return string |
|
205
|
|
|
*/ |
|
206
|
|
|
public function getOutput(): string |
|
207
|
|
|
{ |
|
208
|
|
|
return ($this->xResponse) ? $this->xResponse->getOutput() : ''; |
|
209
|
|
|
} |
|
|
|
|
|
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Prints the response object to the output stream, thus sending the response to the browser |
|
213
|
|
|
* |
|
214
|
|
|
* @return void |
|
215
|
|
|
*/ |
|
216
|
|
|
public function sendOutput() |
|
217
|
|
|
{ |
|
218
|
|
|
if(($this->xResponse)) |
|
219
|
|
|
{ |
|
220
|
|
|
$this->sendHeaders(); |
|
221
|
|
|
$this->xResponse->printOutput(); |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
|
|
|
|
|
224
|
|
|
} |
|
225
|
|
|
|