-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueRequestHandler.php
233 lines (217 loc) · 6.61 KB
/
QueueRequestHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* Queue-based PSR-15 HTTP Server Request Handler
* Copyright (C) 2023 Sebastian Meyer <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace OCC\PSR15;
use Exception;
use RuntimeException;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use GuzzleHttp\Psr7\ServerRequest as GuzzleRequest;
use OCC\Basics\Traits\Getter;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as ServerRequest;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use function array_keys;
use function count;
use function filter_var;
use function get_debug_type;
use function header;
use function headers_sent;
use function sprintf;
/**
* A queue-based PSR-15 HTTP Server Request Handler.
*
* @author Sebastian Meyer <[email protected]>
* @package PSR15
*
* @property-read MiddlewareQueue $queue
* @property-read ServerRequest $request
* @property-read Response $response
*/
class QueueRequestHandler implements RequestHandler
{
use Getter;
/**
* The PSR-7 HTTP Server Request.
*
* @var ServerRequest
*
* @internal
*/
protected ServerRequest $request;
/**
* The queue of middlewares to process the server request.
*
* @var MiddlewareQueue
*
* @internal
*/
protected MiddlewareQueue $queue;
/**
* The PSR-7 HTTP Response.
*
* @var Response
*
* @internal
*/
protected Response $response;
/**
* Handles a request by invoking a queue of middlewares.
*
* @param ?ServerRequest $request The PSR-7 server request to handle
*
* @return Response A PSR-7 compatible HTTP response
*
* @api
*/
public function handle(?ServerRequest $request = null): Response
{
$this->request = $request ?? $this->request;
if (count($this->queue) > 0) {
$middleware = $this->queue->dequeue();
// It is RECOMMENDED that any application using middleware includes a
// component that catches exceptions and converts them into responses.
// This middleware SHOULD be the first component executed and wrap all
// further processing to ensure that a response is always generated.
try {
$this->response = $middleware->process($this->request, $this);
} catch (Exception $exception) {
$options = [
'options' => [
'default' => 500,
'min_range' => 100,
'max_range' => 599
]
];
$this->response = new GuzzleResponse(
filter_var($exception->getCode(), FILTER_VALIDATE_INT, $options),
[
'Warning' => [sprintf(
'Error %d in %s',
$exception->getCode(),
get_debug_type($middleware)
)]
],
sprintf(
'Exception %d thrown in middleware %s: %s',
$exception->getCode(),
get_debug_type($middleware),
$exception->getMessage()
)
);
}
}
return $this->response;
}
/**
* Return the current response to the client.
*
* @return void
*
* @throws RuntimeException if headers were already sent
*
* @api
*/
public function respond(): void
{
$file = 'unknown file';
$line = 0;
if (headers_sent($file, $line)) {
throw new RuntimeException(
sprintf(
'Headers already sent in %s on line %d',
$file,
$line
)
);
}
header(
sprintf(
'HTTP/%s %d %s',
$this->response->getProtocolVersion(),
$this->response->getStatusCode(),
$this->response->getReasonPhrase()
),
true
);
foreach (array_keys($this->response->getHeaders()) as $name) {
/** @var string $name */
$header = sprintf('%s: %s', $name, $this->response->getHeaderLine($name));
header($header, false);
}
echo $this->response->getBody();
}
/**
* Magic getter method for $this->queue.
*
* @return MiddlewareQueue The queue of PSR-15 middlewares
*
* @internal
*/
protected function _magicGetQueue(): MiddlewareQueue
{
return $this->queue;
}
/**
* Magic getter method for $this->request.
*
* @return ServerRequest The PSR-7 server request
*
* @internal
*/
protected function _magicGetRequest(): ServerRequest
{
return $this->request;
}
/**
* Magic getter method for $this->response.
*
* @return Response The PSR-7 response
*
* @internal
*/
protected function _magicGetResponse(): Response
{
return $this->response;
}
/**
* Create a queue-based PSR-15 HTTP Server Request Handler.
*
* @param iterable<array-key, Middleware> $middlewares Initial set of middlewares
*
* @return void
*/
public function __construct(iterable $middlewares = [])
{
$this->request = GuzzleRequest::fromGlobals();
$this->queue = MiddlewareQueue::getInstance($middlewares);
$this->response = new GuzzleResponse();
}
/**
* Allow the request handler to be invoked directly.
*
* @param ?ServerRequest $request The PSR-7 server request to handle
*
* @return Response A PSR-7 compatible HTTP response
*/
public function __invoke(?ServerRequest $request = null): Response
{
return $this->handle($request);
}
}