1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* the Response class |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Phile\Core; |
8
|
|
|
|
9
|
|
|
use Phile\Http\ResponseFactory; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* the Response class is responsible for sending a HTTP response to the client |
13
|
|
|
* |
14
|
|
|
* Response is chainable and can be used anywhere: |
15
|
|
|
* |
16
|
|
|
* (new Respose)->setBody('Hello World')->send(); |
17
|
|
|
* |
18
|
|
|
* After send() Phile is terminated. |
19
|
|
|
* |
20
|
|
|
* @author PhileCMS |
21
|
|
|
* @link https://philecms.github.io |
22
|
|
|
* @license http://opensource.org/licenses/MIT |
23
|
|
|
* @package Phile |
24
|
|
|
*/ |
25
|
|
|
class Response extends ResponseFactory |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string HTTP body |
29
|
|
|
*/ |
30
|
|
|
protected $body = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string charset |
34
|
|
|
*/ |
35
|
|
|
protected $charset = 'utf-8'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array HTTP-headers |
39
|
|
|
*/ |
40
|
|
|
protected $headers = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int HTTP status code |
44
|
|
|
*/ |
45
|
|
|
protected $statusCode = 200; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* redirect to another URL |
49
|
|
|
* |
50
|
|
|
* @param string $url URL |
51
|
|
|
* @param int $statusCode |
52
|
|
|
*/ |
53
|
1 |
|
public function redirect($url, $statusCode = 302) |
54
|
|
|
{ |
55
|
1 |
|
$this->setStatusCode($statusCode) |
56
|
1 |
|
->setHeader('Location', $url, true) |
57
|
1 |
|
->setBody('') |
58
|
1 |
|
->send() |
59
|
1 |
|
->stop(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* set the response body |
64
|
|
|
* |
65
|
|
|
* @param string $body |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
2 |
|
public function setBody(string $body): self |
69
|
|
|
{ |
70
|
2 |
|
$this->body = $body; |
71
|
2 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* set the response character-set |
76
|
|
|
* |
77
|
|
|
* @param string $charset |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
1 |
|
public function setCharset(string $charset): self |
81
|
|
|
{ |
82
|
1 |
|
$this->charset = $charset; |
83
|
1 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* set a response HTTP-header |
88
|
|
|
* |
89
|
|
|
* @param string $key |
90
|
|
|
* @param string $value |
91
|
|
|
* @param bool $clear clear out any existing headers |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
1 |
|
public function setHeader($key, $value, $clear = false) |
95
|
|
|
{ |
96
|
1 |
|
if ($clear) { |
97
|
|
|
$this->headers = []; |
98
|
|
|
} |
99
|
1 |
|
$this->headers[$key] = "$key: $value"; |
100
|
1 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* set the response HTTP status code |
105
|
|
|
* |
106
|
|
|
* @param int $code |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
|
public function setStatusCode($code) |
110
|
|
|
{ |
111
|
|
|
$this->statusCode = $code; |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* sends the HTTP response |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
3 |
|
public function send() |
121
|
|
|
{ |
122
|
3 |
|
if (!isset($this->headers['Content-Type'])) { |
123
|
3 |
|
$this->setHeader('Content-Type', 'text/html; charset=' . $this->charset); |
124
|
|
|
} |
125
|
3 |
|
$this->outputHeader(); |
126
|
3 |
|
http_response_code($this->statusCode); |
127
|
3 |
|
echo $this->body; |
128
|
3 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* helper for easy testing |
133
|
|
|
*/ |
134
|
|
|
public function stop(): void |
135
|
|
|
{ |
136
|
|
|
die(); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* output all set response headers |
141
|
|
|
*/ |
142
|
2 |
|
protected function outputHeader(): void |
143
|
|
|
{ |
144
|
2 |
|
foreach ($this->headers as $header) { |
145
|
|
|
header($header); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.