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\StreamFactoryInterface; |
8
|
|
|
use Psr\Http\Message\StreamInterface; |
9
|
|
|
use Yiisoft\Yii\Web\Formatter\ResponseFormatterInterface; |
10
|
|
|
|
11
|
|
|
class WebResponse implements ResponseInterface |
12
|
|
|
{ |
13
|
|
|
private ResponseInterface $response; |
14
|
|
|
|
15
|
|
|
private $data; |
16
|
|
|
|
17
|
|
|
private ?StreamInterface $dataStream = null; |
18
|
|
|
|
19
|
|
|
private ?ResponseFormatterInterface $responseFormatter = null; |
20
|
|
|
|
21
|
|
|
public function __construct($data, int $code, ResponseFactoryInterface $responseFactory) |
22
|
|
|
{ |
23
|
|
|
$this->response = $responseFactory->createResponse($code); |
24
|
|
|
$this->data = $data; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getBody(): StreamInterface |
28
|
|
|
{ |
29
|
|
|
if ($this->dataStream !== null) { |
30
|
|
|
return $this->dataStream; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if ($this->data === null) { |
34
|
|
|
return $this->dataStream = $this->response->getBody(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($this->responseFormatter !== null) { |
38
|
|
|
$this->response = $this->responseFormatter->format($this); |
39
|
|
|
return $this->dataStream = $this->response->getBody(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$data = $this->getData(); |
43
|
|
|
if (is_string($data)) { |
44
|
|
|
$this->response->getBody()->write($data); |
45
|
|
|
return $this->dataStream = $this->response->getBody(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
throw new \RuntimeException('Data must be a string value.'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getHeader($name): array |
52
|
|
|
{ |
53
|
|
|
return $this->response->getHeader($name); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getHeaderLine($name): string |
57
|
|
|
{ |
58
|
|
|
return $this->response->getHeaderLine($name); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getHeaders(): array |
62
|
|
|
{ |
63
|
|
|
return $this->response->getHeaders(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getProtocolVersion(): string |
67
|
|
|
{ |
68
|
|
|
return $this->response->getProtocolVersion(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getReasonPhrase(): string |
72
|
|
|
{ |
73
|
|
|
return $this->response->getReasonPhrase(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getStatusCode(): int |
77
|
|
|
{ |
78
|
|
|
return $this->response->getStatusCode(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function hasHeader($name): bool |
82
|
|
|
{ |
83
|
|
|
return $this->response->hasHeader($name); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function withAddedHeader($name, $value): WebResponse |
87
|
|
|
{ |
88
|
|
|
$response = clone $this; |
89
|
|
|
$response->response = $this->response->withAddedHeader($name, $value); |
90
|
|
|
return $response; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function withBody(StreamInterface $body): WebResponse |
94
|
|
|
{ |
95
|
|
|
$response = clone $this; |
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
|
|
|
|