-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathResponse.php
More file actions
364 lines (304 loc) · 7.47 KB
/
Copy pathResponse.php
File metadata and controls
364 lines (304 loc) · 7.47 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
declare(strict_types=1);
namespace Bow\Http;
use Bow\Contracts\ResponseInterface;
use Bow\View\View;
use stdClass;
class Response implements ResponseInterface
{
/**
* The Response instance
*
* @var ?Response
*/
private static ?Response $instance = null;
/**
* The Response content
*
* @var string
*/
private string $content = '';
/**
* The Response code
*
* @var int
*/
private int $code;
/**
* Define the headers
*
* @var array
*/
private array $headers = [];
/**
* Define the cookies
*
* @var array
*/
private array $cookies = [];
/**
* Downloadable flag
*
* @var bool
*/
private bool $download = false;
/**
* The downloadable filename
*
* @var ?string
*/
private ?string $download_filename = null;
/**
* The override the response
*
* @var bool
*/
private bool $override = false;
/**
* Response constructor.
*
* @param string $content
* @param int $code
* @param array $headers
*/
private function __construct(string $content = '', int $code = 200, array $headers = [])
{
$this->content = $content;
$this->code = $code;
$this->headers = $headers;
$this->override = false;
}
/**
* Get response
*
* @return Response
*/
public static function getInstance(): Response
{
if (is_null(static::$instance)) {
static::$instance = new Response();
}
return static::$instance;
}
/**
* Get status code
*
* @return int
*/
public function getCode(): int
{
return $this->code;
}
/**
* Download the given file as an argument
*
* @param string $file
* @param ?string $filename
* @param array $headers
* @return string
*/
public function download(
string $file,
?string $filename = null,
array $headers = []
): string {
$type = mime_content_type($file);
if (is_null($filename)) {
$filename = basename($file);
}
$disposition = $headers["disposition"] ?? 'attachment';
$this->withHeader('Content-Disposition', $disposition . '; filename=' . $filename);
$this->withHeader('Content-Type', $type);
$file_size = filesize($file);
$this->withHeader('Content-Length', (string)(is_int($file_size) ? $file_size : ''));
$this->withHeader('Content-Encoding', 'base64');
// We put the new headers
foreach ($headers as $key => $value) {
$this->withHeader($key, $value);
}
$this->download_filename = $file;
$this->download = true;
return $this->buildHttpResponse();
}
/**
* Add http headers
*
* @param array $headers
* @return Response
*/
public function withHeaders(array $headers): Response
{
$this->headers = array_merge($this->headers, $headers);
return $this;
}
/**
* Add http header
*
* @param string $key
* @param string $value
* @return Response
*/
public function withHeader(string $key, string $value): Response
{
$this->headers[$key] = $value;
return $this;
}
/**
* Set cookie header
*
* @param string $key
* @param string $value
* @return Response
*/
public function withCookie(string $key, string $value): Response
{
$this->cookies[$key] = $value;
return $this;
}
/**
* Set multiple cookies
*
* @param array $cookies
* @return Response
*/
public function withCookies(array $cookies): Response
{
$this->cookies = array_merge($this->cookies, $cookies);
return $this;
}
/**
* Build HTTP Response
*
* @return string
*/
private function buildHttpResponse(): string
{
$status_text = HttpStatus::getMessage($this->code) ?? 'Unknown';
@header('HTTP/1.1 ' . $this->code . ' ' . $status_text, $this->override, $this->code);
foreach ($this->getHeaders() as $key => $header) {
header(sprintf('%s: %s', $key, $header));
}
foreach ($this->cookies as $key => $value) {
cookie($key, $value);
}
if ($this->download) {
readfile($this->download_filename);
die;
}
return $this->getContent();
}
/**
* Get headers
*
* @return array
*/
public function getHeaders(): array
{
return $this->headers;
}
/**
* Get response message
*
* @return ?string
*/
public function getContent(): ?string
{
return (string)$this->content;
}
/**
* Get response message
*
* @param string $content
* @return Response
*/
public function setContent(string $content): Response
{
$this->content = $content;
return $this;
}
/**
* Modify http headers
*
* @param int $code
* @return mixed
*/
public function status(int $code): Response
{
$this->code = $code;
if (in_array($code, HttpStatus::getCodes(), true)) {
@header('HTTP/1.1 ' . $code . ' ' . HttpStatus::getMessage($code), $this->override, $code);
}
return $this;
}
/**
* Equivalent to an echo, except that it ends the application
*
* @param mixed $data
* @param int $code
* @param array $headers
* @return string
*/
public function send(mixed $data, int $code = 200, array $headers = []): string
{
if (is_array($data) || $data instanceof stdClass || is_object($data)) {
return $this->json($data, $code, $headers);
}
$this->code = $code;
foreach ($headers as $key => $value) {
$this->withHeader($key, $value);
}
$this->content = $data;
return $this->buildHttpResponse();
}
/**
* JSON response
*
* @param mixed $data
* @param int $code
* @param array $headers
* @return string
*/
public function json(mixed $data, int $code = 200, array $headers = []): string
{
$this->withHeader('Content-Type', 'application/json; charset=UTF-8');
$this->withHeaders($headers);
$this->content = json_encode($data);
$this->code = $code;
return $this->buildHttpResponse();
}
/**
* Make view rendering
*
* @param string $template
* @param array $data
* @param int $code
* @param array $headers
* @return string
* @throws
*/
public function render(string $template, array $data = [], int $code = 200, array $headers = []): string
{
$this->code = $code;
$this->withHeaders($headers);
$view = View::parse($template, $data);
$this->content = $view->getContent();
return $this->buildHttpResponse();
}
/**
* Modify the service access control from ServerAccessControl instance
*
* @return ServerAccessControl
*/
public function getServerAccessControl(): ServerAccessControl
{
return new ServerAccessControl($this);
}
/**
* @inheritdoc
*/
public function sendContent(): void
{
echo $this->buildHttpResponse();
return;
}
}