-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerResponse.php
131 lines (115 loc) · 3.86 KB
/
ServerResponse.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
<?php
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <[email protected]>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*
*/
namespace Koded\Http;
use Koded\Http\Interfaces\{HttpStatus, Request, Response};
/**
* Class ServerResponse
*
*/
class ServerResponse implements Response, \JsonSerializable
{
use HeaderTrait, MessageTrait, CookieTrait, JsonSerializeTrait;
private const E_CLIENT_RESPONSE_SEND = 'Cannot send the client response.';
private const E_INVALID_STATUS_CODE = 'Invalid status code %s, expected range between [100-599]';
protected int $statusCode = HttpStatus::OK;
protected string $reasonPhrase = 'OK';
/**
* ServerResponse constructor.
*
* @param mixed $content [optional]
* @param int $statusCode [optional]
* @param array $headers [optional]
*/
public function __construct(
mixed $content = '',
int $statusCode = HttpStatus::OK,
array $headers = [])
{
$this->setStatus($this, $statusCode);
$this->setHeaders($headers);
$this->stream = create_stream($content);
}
public function getStatusCode(): int
{
return $this->statusCode;
}
public function withStatus($code, $reasonPhrase = ''): static
{
return $this->setStatus(clone $this, (int)$code, (string)$reasonPhrase);
}
public function getReasonPhrase(): string
{
return (string)$this->reasonPhrase;
}
public function getContentType(): string
{
return $this->getHeaderLine('Content-Type') ?: 'text/html';
}
public function sendHeaders(): void
{
$this->prepareResponse();
if (false === headers_sent()) {
foreach ($this->getHeaders() as $name => $values) {
header($name . ':' . \join(',', (array)$values), false, $this->statusCode);
}
// Status header
header(\sprintf('HTTP/%s %d %s',
$this->getProtocolVersion(),
$this->getStatusCode(),
$this->getReasonPhrase()),
true,
$this->statusCode);
}
}
public function sendBody(): string
{
try {
return (string)$this->stream;
} finally {
$this->stream->close();
}
}
public function send(): string
{
$this->sendHeaders();
return $this->sendBody();
}
protected function setStatus(ServerResponse $instance, int $statusCode, string $reasonPhrase = ''): ServerResponse
{
if ($statusCode < 100 || $statusCode > 599) {
throw new \InvalidArgumentException(
\sprintf(self::E_INVALID_STATUS_CODE, $statusCode), HttpStatus::UNPROCESSABLE_ENTITY
);
}
$instance->statusCode = (int)$statusCode;
$instance->reasonPhrase = $reasonPhrase ?: HttpStatus::CODE[$statusCode];
return $instance;
}
protected function prepareResponse(): void
{
if (\in_array($this->getStatusCode(), [100, 101, 102, 204, 304])) {
$this->stream = create_stream(null);
unset($this->headersMap['content-length'], $this->headers['Content-Length']);
unset($this->headersMap['content-type'], $this->headers['Content-Type']);
return;
}
if ($size = $this->stream->getSize()) {
$this->normalizeHeader('Content-Length', (string)$size, true);
}
$method = \strtoupper($_SERVER['REQUEST_METHOD'] ?? '');
if (Request::HEAD === $method || Request::OPTIONS === $method) {
$this->stream = create_stream(null);
}
if ($this->hasHeader('Transfer-Encoding') || !$size) {
unset($this->headersMap['content-length'], $this->headers['Content-Length']);
}
}
}