-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResponse.php
More file actions
344 lines (308 loc) · 8.43 KB
/
Response.php
File metadata and controls
344 lines (308 loc) · 8.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* File defining Response
*
* PHP Version 5.3
*
* @category Backend
* @package Core
* @author J Jurgens du Toit <jrgns@backend-php.net>
* @copyright 2011 - 2012 Jade IT (cc)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @link http://backend-php.net
*/
namespace Backend\Core;
use Backend\Interfaces\ResponseInterface;
use Backend\Core\Exception as CoreException;
/**
* The response that will be sent back to the client
*
* @category Backend
* @package Core
* @author J Jurgens du Toit <jrgns@backend-php.net>
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @link http://backend-php.net
*/
class Response implements ResponseInterface
{
/**
* The body of the response
*
* @var mixed
*/
protected $body = null;
/**
* @var int The HTTP response code
*/
protected $status = 200;
/**
* @var string The HTTP version
*/
protected $httpVersion = null;
/**
* @var array A list of HTTP Response Codes with their default texts
*
* Copied from the Zend_Http_Response object
*/
protected static $messages = array(
// Informational 1xx
100 => 'Continue',
101 => 'Switching Protocols',
// Success 2xx
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
// Redirection 3xx
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found', // 1.1
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
// 306 is deprecated but reserved
307 => 'Temporary Redirect',
// Client Error 4xx
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
// Server Error 5xx
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
509 => 'Bandwidth Limit Exceeded'
);
/**
* @var array An associative array containing headers to be sent along with the response
*/
protected $headers = array();
/**
* The constructor for the Response class
*
* @param string $body The body for the response
* @param int $status The status code for the response
* @param array $headers The headers for the response
*/
public function __construct($body = '', $status = 200, array $headers = array())
{
$this->setStatusCode($status);
$this->setHeaders($headers);
$this->setBody($body);
//TODO Don't use SERVER, use the REQUEST
$this->httpVersion = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
}
/**
* Return the current status code for the Response
*
* @return int The status code
*/
public function getStatusCode()
{
return $this->status;
}
/**
* Set the status code for the Response
*
* @param int $code The new status code
*
* @return Response The current object
* @throws \RuntimeException If the response code is smaller than 100 or larger than 600.
*/
public function setStatusCode($code)
{
$code = (int) $code;
if ($code < 100 || $code > 600) {
throw new \RuntimeException('HTTP Response Code must be between 100 and 600');
}
$this->status = (int) $code;
return $this;
}
/**
* Get the text associated with a status code
*
* @param int $code The status code to get the text for
*
* @return string The status code text
*/
public function getStatusText($code = null)
{
$code = $code ?: $this->getStatusCode();
if (array_key_exists($code, self::$messages)) {
return self::$messages[$code];
}
return 'Unknown Status';
}
/**
* Return the Response's body
*
* @return mixed The Response's body
*/
public function getBody()
{
return $this->body;
}
/**
* Set the body for the Response
*
* @param mixed $body The new body
*
* @return Response The current object
*/
public function setBody($body)
{
$this->body = $body;
return $this;
}
/**
* Return the specified Request header.
*
* @param string $name The name of the header to return.
*
* @return string
*/
public function getHeader($name)
{
$name = strtolower($name);
return array_key_exists($name, $this->headers) ? $this->headers[$name] : null;
}
/**
* Set the specified Request headers.
*
* If the name is null, the header won't have a name, and will contain only
* the value of the header.
*
* @param string $name The name of the header to set.
* @param string $value The value of the header.
*
* @return array
*/
public function setHeader($name, $value)
{
if ($name === null) {
$this->headers[] = $value;
} else {
$name = strtolower($name);
$this->headers[$name] = $value;
}
return $this;
}
/**
* Return the Response's headers
*
* @return array An array containing the Response's headers
*/
public function getHeaders()
{
$headers = array();
foreach ($this->headers as $name => $content) {
if (is_numeric($name) === false) {
$content = ucwords($name) . ': ' . $content;
}
$headers[] = $content;
}
return $headers;
}
/**
* Set the headers for the Response
*
* @param array $headers The new headers
*
* @return Response The current object
*/
public function setHeaders(array $headers)
{
$this->headers = $headers;
return $this;
}
/**
* Output the Response to the client
*
* @return null
*/
public function output()
{
$this->sendHeaders()
->sendBody();
return $this;
}
/**
* Send the Response's headers to the client
*
* @return Response The current object
*/
public function sendHeaders()
{
if (headers_sent($file, $line)) {
throw new CoreException('Headers already sent in ' . $file . ', line ' . $line);
}
//Always send the HTTP status header first
$httpStatus = $this->httpVersion . ' ' . $this->status
. ' ' . $this->getStatusText($this->status);
array_unshift($this->headers, $httpStatus);
if (!array_key_exists('X-Application', $this->headers)) {
$this->headers['X-Application'] = 'Backend-PHP (Core)';
}
foreach ($this->getHeaders() as $content) {
$this->writeHeader($content);
}
return $this;
}
/**
* Write a HTTP Header.
*
* @param string $content The contents of the header.
*
* @return void
* @todo This function isn't easily testable. Fix it!
*/
public function writeHeader($content)
{
header($content);
}
/**
* Send the Response's body to the client
*
* @return Response The current object
*/
public function sendBody()
{
echo $this->body;
return $this;
}
/**
* Convert the Response to a string
*
* @return string The response as a string
*/
public function __toString()
{
try {
ob_start();
$this->sendBody();
return ob_get_clean();
} catch (\Exception $e) {
return 'Exception: ' . $e->getMessage();
}
}
}