1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.9.8 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Http\Response; |
16
|
|
|
|
17
|
|
|
use Quantum\Http\Traits\Response\Header; |
18
|
|
|
use Quantum\Http\Traits\Response\Status; |
19
|
|
|
use Quantum\Environment\Constants\Env; |
20
|
|
|
use Quantum\Http\Constants\StatusCode; |
21
|
|
|
use Quantum\Http\Traits\Response\Body; |
22
|
|
|
use Quantum\Environment\Environment; |
23
|
|
|
use Exception; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class HttpResponse |
27
|
|
|
* @package Quantum\Http\Response |
28
|
|
|
*/ |
29
|
|
|
abstract class HttpResponse |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
use Header; |
33
|
|
|
use Body; |
34
|
|
|
use Status; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* XML root element |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private static $xmlRoot = '<data></data>'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Callback function |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private static $callbackFunction = ''; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
private static $initialized = false; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Initialize the Response |
55
|
|
|
*/ |
56
|
|
|
public static function init() |
57
|
|
|
{ |
58
|
|
|
if (self::$initialized) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
self::flush(); |
63
|
|
|
|
64
|
|
|
self::$initialized = true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Flushes the response header and body |
69
|
|
|
*/ |
70
|
|
|
public static function flush() |
71
|
|
|
{ |
72
|
|
|
self::$__statusCode = StatusCode::OK; |
73
|
|
|
self::$__headers = []; |
74
|
|
|
self::$__response = []; |
75
|
|
|
self::$xmlRoot = '<data></data>'; |
76
|
|
|
self::$callbackFunction = ''; |
77
|
|
|
self::$initialized = false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Sends all response data to the client and finishes the request. |
82
|
|
|
* @throws Exception |
83
|
|
|
*/ |
84
|
|
|
public static function send() |
85
|
|
|
{ |
86
|
|
|
if (Environment::getInstance()->getAppEnv() != Env::TESTING) { |
87
|
|
|
while (ob_get_level() > 0) { |
88
|
|
|
ob_end_clean(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
foreach (self::$__headers as $key => $value) { |
93
|
|
|
header($key . ': ' . $value); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
http_response_code(self::getStatusCode()); |
97
|
|
|
|
98
|
|
|
echo self::getContent(); |
99
|
|
|
} |
100
|
|
|
} |