1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Psr\Http\Message\StreamInterface; |
8
|
|
|
use Yiisoft\Yii\Web\Formatter\ResponseFormatterInterface; |
9
|
|
|
|
10
|
|
|
class WebResponse implements ResponseInterface |
11
|
|
|
{ |
12
|
|
|
private ResponseInterface $response; |
13
|
|
|
|
14
|
|
|
private $data; |
15
|
|
|
|
16
|
|
|
private ?StreamInterface $dataStream = null; |
17
|
|
|
|
18
|
|
|
private ?ResponseFormatterInterface $responseFormatter = null; |
19
|
|
|
|
20
|
|
|
public function __construct($data, int $code, string $responsePhrase, ResponseFactoryInterface $responseFactory) |
21
|
|
|
{ |
22
|
|
|
$this->response = $responseFactory->createResponse($code, $responsePhrase); |
23
|
|
|
$this->data = $data; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function getBody(): StreamInterface |
27
|
|
|
{ |
28
|
|
|
if ($this->dataStream !== null) { |
29
|
|
|
return $this->dataStream; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ($this->data === null) { |
33
|
|
|
return $this->dataStream = $this->response->getBody(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($this->responseFormatter !== null) { |
37
|
|
|
$this->response = $this->responseFormatter->format($this); |
38
|
|
|
return $this->dataStream = $this->response->getBody(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$data = $this->getData(); |
42
|
|
|
if (is_string($data)) { |
43
|
|
|
$this->response->getBody()->write($data); |
44
|
|
|
return $this->dataStream = $this->response->getBody(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
throw new \RuntimeException('Data must be a string value.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getHeader($name): array |
51
|
|
|
{ |
52
|
|
|
return $this->response->getHeader($name); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getHeaderLine($name): string |
56
|
|
|
{ |
57
|
|
|
return $this->response->getHeaderLine($name); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getHeaders(): array |
61
|
|
|
{ |
62
|
|
|
return $this->response->getHeaders(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getProtocolVersion(): string |
66
|
|
|
{ |
67
|
|
|
return $this->response->getProtocolVersion(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getReasonPhrase(): string |
71
|
|
|
{ |
72
|
|
|
return $this->response->getReasonPhrase(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getStatusCode(): int |
76
|
|
|
{ |
77
|
|
|
return $this->response->getStatusCode(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function hasHeader($name): bool |
81
|
|
|
{ |
82
|
|
|
return $this->response->hasHeader($name); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function withAddedHeader($name, $value): WebResponse |
86
|
|
|
{ |
87
|
|
|
$response = clone $this; |
88
|
|
|
$response->response = $this->response->withAddedHeader($name, $value); |
89
|
|
|
return $response; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function withBody(StreamInterface $body): WebResponse |
93
|
|
|
{ |
94
|
|
|
$response = clone $this; |
95
|
|
|
$response->response = $this->response->withBody($body); |
96
|
|
|
$response->dataStream = $body; |
97
|
|
|
return $response; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function withHeader($name, $value): WebResponse |
101
|
|
|
{ |
102
|
|
|
$response = clone $this; |
103
|
|
|
$response->response = $this->response->withHeader($name, $value); |
104
|
|
|
return $response; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function withoutHeader($name): WebResponse |
108
|
|
|
{ |
109
|
|
|
$response = clone $this; |
110
|
|
|
$response->response = $this->response->withoutHeader($name); |
111
|
|
|
return $response; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function withProtocolVersion($version): WebResponse |
115
|
|
|
{ |
116
|
|
|
$response = clone $this; |
117
|
|
|
$response->response = $this->response->withProtocolVersion($version); |
118
|
|
|
return $response; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function withStatus($code, $reasonPhrase = ''): WebResponse |
122
|
|
|
{ |
123
|
|
|
$response = clone $this; |
124
|
|
|
$response->response = $this->response->withStatus($code, $reasonPhrase); |
125
|
|
|
return $response; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function withResponseFormatter(ResponseFormatterInterface $responseFormatter): WebResponse |
129
|
|
|
{ |
130
|
|
|
$response = clone $this; |
131
|
|
|
$response->responseFormatter = $responseFormatter; |
132
|
|
|
return $response; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function withData($data): WebResponse |
136
|
|
|
{ |
137
|
|
|
$response = clone $this; |
138
|
|
|
$response->data = $data; |
139
|
|
|
|
140
|
|
|
return $response; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function hasResponseFormatter(): bool |
144
|
|
|
{ |
145
|
|
|
return $this->responseFormatter !== null; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getResponse(): ResponseInterface |
149
|
|
|
{ |
150
|
|
|
return $this->response; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getData() |
154
|
|
|
{ |
155
|
|
|
if (is_callable($this->data)) { |
156
|
|
|
$this->data = ($this->data)(); |
157
|
|
|
} |
158
|
|
|
return is_object($this->data) ? clone $this->data : $this->data; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|