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