@@ -29,11 +29,15 @@ class Response {
29
29
30
30
private $ type = '' ; //html, json, xml
31
31
32
- private $ typeHeaders = ['html ' => 'text/html ' , 'xml ' => 'text/xml ' , 'text ' => 'text/plain ' , 'json ' => 'application/json ' ];
32
+ private $ status = 200 ;
33
+
34
+ private $ callback = 'callback ' ;
35
+
36
+ private $ typeHeaders = ['html ' => 'text/html ' , 'xml ' => 'text/xml ' , 'text ' => 'text/plain ' , 'json ' => 'application/json ' , 'jsonp ' => 'application/javascript ' ];
33
37
34
38
protected static $ instance ;
35
39
36
- public static function getInstance () {
40
+ final public static function getInstance () {
37
41
if (is_null (static ::$ instance )) {
38
42
static ::$ instance = new static ();
39
43
}
@@ -45,10 +49,14 @@ private function __construct() {
45
49
$ this ->addHeader ('X-Powered-By ' , 'Vvveb ' /* . V_VERSION*/ );
46
50
}
47
51
48
- public function addHeader ($ header , $ value ) {
52
+ public function addHeader ($ header , $ value = null ) {
49
53
$ this ->headers [$ header ] = $ value ;
50
54
}
51
55
56
+ public function removeHeader ($ header ) {
57
+ unset($ this ->headers [$ header ]);
58
+ }
59
+
52
60
public function getHeaders () {
53
61
return $ this ->headers ;
54
62
}
@@ -59,7 +67,7 @@ public function redirect($url, $status = 302) {
59
67
exit ();
60
68
}
61
69
62
- public function getType ($ type ) {
70
+ public function getType () {
63
71
return $ this ->type ;
64
72
}
65
73
@@ -78,20 +86,30 @@ public function output($data = null) {
78
86
}
79
87
80
88
if (! headers_sent ()) {
81
- foreach ($ this ->headers as $ header => $ value ) {
82
- header ("$ header: $ value " , true );
89
+ foreach ($ this ->headers as $ name => $ value ) {
90
+ if ($ value ) {
91
+ $ header = "$ name: $ value " ;
92
+ } else {
93
+ $ header = $ name ;
94
+ }
95
+
96
+ header ($ header , true );
83
97
}
84
98
}
85
-
99
+
86
100
if ($ this ->type == 'text ' && $ data !== null ) {
87
101
echo $ data ;
88
102
} else {
89
- if ($ this ->type == 'json ' && $ data !== null && (! defined ('CLI ' ))) {
90
- if (is_array ($ data )) {
91
- echo json_encode ($ data );
92
- } else {
93
- echo $ data ;
103
+ if (($ this ->type == 'json ' || $ this ->type == 'jsonp ' ) && $ data !== null && (! defined ('CLI ' ))) {
104
+ if (is_array ($ data ) || is_object ($ data )) {
105
+ $ data = json_encode ($ data , JSON_PRETTY_PRINT );
106
+
107
+ if ($ this ->type == 'jsonp ' ) {
108
+ $ data = "/**/ {$ this ->callback }( $ data) " ;
109
+ }
94
110
}
111
+
112
+ echo $ data ;
95
113
} else {
96
114
$ view = View :: getInstance ();
97
115
0 commit comments