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