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