Skip to content

Commit 533b4c0

Browse files
committed
Optimized code for resource.
1 parent 37fa2c0 commit 533b4c0

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

src/Server/ResponseProxyTrait.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 group@hyperf.io
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 RuntimeException;
17+
18+
trait ResponseProxyTrait
19+
{
20+
/**
21+
* @var null|ResponseInterface
22+
*/
23+
protected $response;
24+
25+
public function setResponse(ResponseInterface $response)
26+
{
27+
$this->response = $response;
28+
}
29+
30+
public function getProtocolVersion()
31+
{
32+
return $this->getResponse()->getProtocolVersion();
33+
}
34+
35+
public function withProtocolVersion($version)
36+
{
37+
$this->setResponse($this->getResponse()->withProtocolVersion($version));
38+
return $this;
39+
}
40+
41+
public function getHeaders()
42+
{
43+
return $this->getResponse()->getHeaders();
44+
}
45+
46+
public function hasHeader($name)
47+
{
48+
return $this->getResponse()->hasHeader($name);
49+
}
50+
51+
public function getHeader($name)
52+
{
53+
return $this->getResponse()->getHeader($name);
54+
}
55+
56+
public function getHeaderLine($name)
57+
{
58+
return $this->getResponse()->getHeaderLine($name);
59+
}
60+
61+
public function withHeader($name, $value)
62+
{
63+
$this->setResponse($this->getResponse()->withHeader($name, $value));
64+
return $this;
65+
}
66+
67+
public function withAddedHeader($name, $value)
68+
{
69+
$this->setResponse($this->getResponse()->withAddedHeader($name, $value));
70+
return $this;
71+
}
72+
73+
public function withoutHeader($name)
74+
{
75+
$this->setResponse($this->getResponse()->withoutHeader($name));
76+
return $this;
77+
}
78+
79+
public function getBody()
80+
{
81+
return $this->getResponse()->getBody();
82+
}
83+
84+
public function withBody(StreamInterface $body)
85+
{
86+
$this->setResponse($this->getResponse()->withBody($body));
87+
return $this;
88+
}
89+
90+
public function getStatusCode()
91+
{
92+
return $this->getResponse()->getStatusCode();
93+
}
94+
95+
public function withStatus($code, $reasonPhrase = '')
96+
{
97+
$this->setResponse($this->getResponse()->withStatus($code, $reasonPhrase));
98+
return $this;
99+
}
100+
101+
public function getReasonPhrase()
102+
{
103+
return $this->getResponse()->getReasonPhrase();
104+
}
105+
106+
public function getResponse(): ResponseInterface
107+
{
108+
if (! $this->response instanceof ResponseInterface) {
109+
throw new RuntimeException('response is invalid.');
110+
}
111+
return $this->response;
112+
}
113+
}

0 commit comments

Comments
 (0)