Skip to content

Commit dceaa1a

Browse files
authored
Support swow psr7-plus interface for all components. (#5839)
1 parent cdb3e6a commit dceaa1a

File tree

3 files changed

+199
-8
lines changed

3 files changed

+199
-8
lines changed

src/Base/MessageTrait.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,7 @@ public function withHeader(mixed $name, mixed $value): static
184184

185185
public function withHeaders(array $headers): static
186186
{
187-
$new = clone $this;
188-
foreach ($headers as $name => $value) {
189-
$new = $new->withHeader(str_replace('_', '-', $name), $value);
190-
}
191-
return $new;
187+
return (clone $this)->setHeaders($headers);
192188
}
193189

194190
/**
@@ -444,8 +440,10 @@ public function setHeaders(array $headers): static
444440
*/
445441
private function trimHeaderValues(array $values): array
446442
{
447-
return array_map(function ($value) {
448-
return trim((string) $value, " \t");
449-
}, $values);
443+
$result = [];
444+
foreach ($values as $value) {
445+
$result[] = trim((string) $value, " \t");
446+
}
447+
return $result;
450448
}
451449
}

src/Base/Response.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ public function withAttribute(string $name, mixed $value): static
149149
return $clone;
150150
}
151151

152+
public function setAttribute(string $name, mixed $value): static
153+
{
154+
$this->attributes[$name] = $value;
155+
return $this;
156+
}
157+
152158
/**
153159
* Gets the response status code.
154160
* The status code is a 3-digit integer result code of the server's attempt

src/Server/ResponsePlusProxy.php

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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+
* @contact [email protected]
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

Comments
 (0)