-
Notifications
You must be signed in to change notification settings - Fork 1
/
Response.php
267 lines (217 loc) · 6.43 KB
/
Response.php
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
<?php
namespace Pina;
class Response implements ResponseInterface
{
protected $code = null;
protected $location = null;
protected $headers = [];
protected $errors = [];
protected $content = null;
public function __construct($code)
{
$this->code = $code;
}
public static function code($code)
{
return new Response($code);
}
public static function ok()
{
return static::code('200 OK');
}
public static function created($url)
{
return static::code('201 Created')->header('Location', $url);
}
public static function accepted($url)
{
return static::code('202 Accepted')->header('Content-Location', $url);
}
public static function noContent()
{
return static::code('204 No Content');
}
public static function partialContent($start, $end, $max)
{
return static::code('206 Partial Content')
->header('Content-Range', 'bytes ' . intval($start) . '-' . intval($end) . '/' . intval($max));
}
/* HTTP Codes 3xx */
public static function movedPermanently($url)
{
return static::code('301 Moved Permanently')->header('Location', $url);
}
public static function found($url)
{
return static::code('302 Found')->header('Location', $url);
}
public static function notModified()
{
return static::code('304 Not Modified');
}
/* HTTP Codes 4xx */
public static function badRequest($message = '', $subject = '')
{
return static::stopWithCode('400 Bad Request')->error($message, $subject);
}
public static function unauthorized()
{
return static::stopWithCode('401 Unauthorized');
}
public static function forbidden()
{
return static::stopWithCode('403 Forbidden');
}
public static function notFound()
{
return static::stopWithCode('404 Not Found');
}
public static function requestTimeout()
{
return static::stopWithCode('408 Request Timeout');
}
public static function conflict()
{
return static::stopWithCode('409 Conflict');
}
public static function gone()
{
return static::stopWithCode('410 Gone');
}
public static function internalError($message = '', $subject = '')
{
return static::failWithCode('500 Internal Server Error')->error($message, $subject);
}
public static function notImplemented()
{
return static::failWithCode('501 Not Implemented');
}
public static function badGateway()
{
return static::failWithCode('502 Bad Gateway');
}
public static function serviceUnavailable()
{
return static::failWithCode('503 Service Unavailable');
}
public static function gatewayTimeout()
{
return static::failWithCode('504 Gateway Timeout');
}
private static function stopWithCode($code)
{
$response = self::code($code);
if (Request::isExternalRequest()) {
$codeNumber = substr($code, 0, 3);
if (App::router()->exists($codeNumber, 'get')) {
$data = App::router()->run($codeNumber, 'get', []);
if ($data instanceof Response) {
if (!$data->hasContent()) {
$content = App::createResponseContent([], $codeNumber, 'index');
$data->setContent($content);
}
return $data;
}
$content = new TemplateLayoutContent;
if (Request::isExternalRequest()) {
$layout = $data->getLayout();
$r = $layout->append($data)->drawWithWrappers();
$content->setContent($r);
} else {
$content->setContent($data->drawWithWrappers());
}
$response->setContent($content);
return $response;
}
$content = App::createResponseContent(['code' => $code], 'errors', 'show');
$response->setContent($content);
return $response;
}
return $response->setContent(new EmptyContent);
}
private static function failWithCode($code)
{
return self::stopWithCode($code);
}
/* headers */
public function location($url)
{
return $this->header('Location', $url);
}
public function contentLocation($url)
{
return $this->header('Content-Location', $url);
}
public function contentType($type, $charset = false)
{
if (empty($charset)) {
$charset = App::charset();
}
return $this->header('Content-Type', $type . '; charset=' . $charset);
}
public function contentRange($start, $end, $max)
{
return $this->header('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $max);
}
public function header($name, $value)
{
$this->headers[] = [$name, $value];
return $this;
}
public function error($message, $subject = '')
{
if (empty($message)) {
return $this;
}
$this->errors[] = [$message, $subject];
return $this;
}
public function setErrors($errors)
{
$this->errors = $errors;
return $this;
}
public function hasContent()
{
return $this->content !== null;
}
public function setContent($content)
{
$this->content = $content;
return $this;
}
public function json($data = [])
{
return $this->setContent(new JsonContent($data));
}
public function emptyContent()
{
return $this->setContent(new EmptyContent);
}
public function send()
{
if (!empty($this->code)) {
header('HTTP/1.1 ' . $this->code);
}
foreach ($this->headers as $header) {
header($header[0] . ':' . $header[1]);
}
if (!empty($this->content)) {
header('Content-Type: ' . $this->content->getType());
if ($this->errors) {
$this->content->setErrors($this->errors);
}
echo $this->content->fetch();
}
}
public function fetchContent()
{
if (empty($this->content)) {
return '';
}
if ($this->errors) {
$this->content->setErrors($this->errors);
}
return $this->content->fetch();
}
}