|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * This file is part of Hyperf. |
| 6 | + * |
| 7 | + * @link https://www.hyperf.io |
| 8 | + * @document https://hyperf.wiki |
| 9 | + |
| 10 | + * @license https://github.com/hyperf/hyperf/blob/master/LICENSE |
| 11 | + */ |
| 12 | +namespace Hyperf\HttpMessage\Server; |
| 13 | + |
| 14 | +use Psr\Http\Message\ResponseInterface; |
| 15 | +use Psr\Http\Message\StreamInterface; |
| 16 | +use Swow\Psr7\Message\ResponsePlusInterface; |
| 17 | + |
| 18 | +class ResponsePlusProxy implements ResponsePlusInterface |
| 19 | +{ |
| 20 | + public function __construct(protected ResponseInterface $response) |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + public function __toString() |
| 25 | + { |
| 26 | + return $this->toString(); |
| 27 | + } |
| 28 | + |
| 29 | + public function getProtocolVersion(): string |
| 30 | + { |
| 31 | + return $this->response->getProtocolVersion(); |
| 32 | + } |
| 33 | + |
| 34 | + public function setProtocolVersion(string $version): static |
| 35 | + { |
| 36 | + $this->response = $this->response->withProtocolVersion($version); |
| 37 | + return $this; |
| 38 | + } |
| 39 | + |
| 40 | + public function withProtocolVersion(mixed $version): static |
| 41 | + { |
| 42 | + return new static($this->response->withProtocolVersion($version)); |
| 43 | + } |
| 44 | + |
| 45 | + public function hasHeader(mixed $name): bool |
| 46 | + { |
| 47 | + return $this->response->hasHeader($name); |
| 48 | + } |
| 49 | + |
| 50 | + public function getHeader(mixed $name): array |
| 51 | + { |
| 52 | + return $this->response->getHeader($name); |
| 53 | + } |
| 54 | + |
| 55 | + public function getHeaderLine(mixed $name): string |
| 56 | + { |
| 57 | + return $this->response->getHeaderLine($name); |
| 58 | + } |
| 59 | + |
| 60 | + public function setHeader(string $name, mixed $value): static |
| 61 | + { |
| 62 | + $this->response = $this->response->withHeader($name, $value); |
| 63 | + return $this; |
| 64 | + } |
| 65 | + |
| 66 | + public function withHeader(mixed $name, mixed $value): static |
| 67 | + { |
| 68 | + return new static($this->response->withHeader($name, $value)); |
| 69 | + } |
| 70 | + |
| 71 | + public function addHeader(string $name, mixed $value): static |
| 72 | + { |
| 73 | + $this->response = $this->response->withAddedHeader($name, $value); |
| 74 | + return $this; |
| 75 | + } |
| 76 | + |
| 77 | + public function withAddedHeader(mixed $name, mixed $value): static |
| 78 | + { |
| 79 | + return new static($this->response->withAddedHeader($name, $value)); |
| 80 | + } |
| 81 | + |
| 82 | + public function unsetHeader(string $name): static |
| 83 | + { |
| 84 | + $this->response = $this->response->withoutHeader($name); |
| 85 | + return $this; |
| 86 | + } |
| 87 | + |
| 88 | + public function withoutHeader(mixed $name): static |
| 89 | + { |
| 90 | + return new static($this->response->withoutHeader($name)); |
| 91 | + } |
| 92 | + |
| 93 | + public function getHeaders(): array |
| 94 | + { |
| 95 | + return $this->response->getHeaders(); |
| 96 | + } |
| 97 | + |
| 98 | + public function getStandardHeaders(): array |
| 99 | + { |
| 100 | + $headers = $this->getHeaders(); |
| 101 | + if (! $this->hasHeader('connection')) { |
| 102 | + $headers['Connection'] = [$this->shouldKeepAlive() ? 'keep-alive' : 'close']; |
| 103 | + } |
| 104 | + if (! $this->hasHeader('content-length')) { |
| 105 | + $headers['Content-Length'] = [(string) ($this->getBody()->getSize() ?? 0)]; |
| 106 | + } |
| 107 | + return $headers; |
| 108 | + } |
| 109 | + |
| 110 | + public function setHeaders(array $headers): static |
| 111 | + { |
| 112 | + foreach ($this->getHeaders() as $key => $value) { |
| 113 | + $this->unsetHeader($key); |
| 114 | + } |
| 115 | + |
| 116 | + foreach ($headers as $key => $value) { |
| 117 | + $this->setHeader($key, $value); |
| 118 | + } |
| 119 | + |
| 120 | + return $this; |
| 121 | + } |
| 122 | + |
| 123 | + public function withHeaders(array $headers): static |
| 124 | + { |
| 125 | + return new static($this->setHeaders($headers)->response); |
| 126 | + } |
| 127 | + |
| 128 | + public function shouldKeepAlive(): bool |
| 129 | + { |
| 130 | + return strtolower($this->getHeaderLine('Connection')) === 'keep-alive'; |
| 131 | + } |
| 132 | + |
| 133 | + public function getBody(): StreamInterface |
| 134 | + { |
| 135 | + return $this->response->getBody(); |
| 136 | + } |
| 137 | + |
| 138 | + public function setBody(StreamInterface $body): static |
| 139 | + { |
| 140 | + $this->response = $this->response->withBody($body); |
| 141 | + return $this; |
| 142 | + } |
| 143 | + |
| 144 | + public function withBody(StreamInterface $body): static |
| 145 | + { |
| 146 | + return new static($this->response->withBody($body)); |
| 147 | + } |
| 148 | + |
| 149 | + public function toString(bool $withoutBody = false): string |
| 150 | + { |
| 151 | + $headerString = ''; |
| 152 | + foreach ($this->getStandardHeaders() as $key => $values) { |
| 153 | + foreach ($values as $value) { |
| 154 | + $headerString .= sprintf("%s: %s\r\n", $key, $value); |
| 155 | + } |
| 156 | + } |
| 157 | + return sprintf( |
| 158 | + "HTTP/%s %s %s\r\n%s\r\n%s", |
| 159 | + $this->getProtocolVersion(), |
| 160 | + $this->getStatusCode(), |
| 161 | + $this->getReasonPhrase(), |
| 162 | + $headerString, |
| 163 | + $this->getBody() |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + public function getStatusCode(): int |
| 168 | + { |
| 169 | + return $this->response->getStatusCode(); |
| 170 | + } |
| 171 | + |
| 172 | + public function getReasonPhrase(): string |
| 173 | + { |
| 174 | + return $this->response->getReasonPhrase(); |
| 175 | + } |
| 176 | + |
| 177 | + public function setStatus(int $code, string $reasonPhrase = ''): static |
| 178 | + { |
| 179 | + $this->response = $this->response->withStatus($code, $reasonPhrase); |
| 180 | + return $this; |
| 181 | + } |
| 182 | + |
| 183 | + public function withStatus($code, $reasonPhrase = ''): static |
| 184 | + { |
| 185 | + return new static($this->response->withStatus($code, $reasonPhrase)); |
| 186 | + } |
| 187 | +} |
0 commit comments