-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactServerCompat.php
More file actions
351 lines (291 loc) · 10.3 KB
/
Copy pathReactServerCompat.php
File metadata and controls
351 lines (291 loc) · 10.3 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
declare(strict_types=1);
namespace PivotPHP\ReactPHP\Server;
use PivotPHP\Core\Core\Application;
use React\EventLoop\LoopInterface;
use React\Http\HttpServer;
use React\Socket\SocketServer;
use React\Promise\Promise;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
/**
* ReactPHP Server with compatibility workarounds for PSR-7 conflicts
*/
final class ReactServerCompat
{
private Application $application;
private LoopInterface $loop;
private LoggerInterface $logger;
private array $config;
private ?HttpServer $server = null;
private ?SocketServer $socket = null;
private bool $running = false;
public function __construct(
Application $application,
LoopInterface $loop,
?LoggerInterface $logger = null,
array $config = []
) {
$this->application = $application;
$this->loop = $loop;
$this->logger = $logger ?? new NullLogger();
$this->config = array_merge([
'host' => '0.0.0.0',
'port' => 8080,
'workers' => 1,
'middleware' => [],
], $config);
}
public function start(): void
{
if ($this->running) {
throw new \RuntimeException('Server is already running');
}
$this->setupSignalHandlers();
$host = $this->config['host'];
$port = $this->config['port'];
$address = "{$host}:{$port}";
$this->logger->info('Starting ReactPHP server', [
'address' => $address,
'workers' => $this->config['workers'],
'pid' => getmypid(),
]);
// Create HTTP server with a simple handler that avoids PSR-7 conflicts
$this->server = new HttpServer(
$this->loop,
function ($request) {
return $this->handleRequestCompat($request);
}
);
$this->socket = new SocketServer($address, [], $this->loop);
$this->server->listen($this->socket);
$this->running = true;
$this->logger->info('ReactPHP server started', ['address' => $address]);
// Emit server started event
$this->emitEvent('server.started', ['address' => $address]);
// Start the event loop
$this->loop->run();
}
/**
* Handle request with compatibility workaround
*/
private function handleRequestCompat(\React\Http\Message\ServerRequest $reactRequest): Promise
{
return new Promise(function ($resolve, $reject) use ($reactRequest) {
try {
// Extract basic request data from ReactPHP request
$requestData = $this->extractRequestData($reactRequest);
// Create PivotPHP Request from extracted data
$pivotRequest = $this->createPivotRequest($requestData);
// Process the request through routing and get response
$reactResponse = $this->processRoute($pivotRequest, $requestData);
$resolve($reactResponse);
} catch (\Exception $e) {
$errorResponse = $this->createErrorResponse($e);
$resolve($errorResponse);
}
});
}
public function getApplication(): Application
{
return $this->application;
}
public function stop(): void
{
if (!$this->running) {
return;
}
$this->logger->info('Stopping ReactPHP server...');
$this->emitEvent('server.stopping');
if ($this->socket !== null) {
$this->socket->close();
}
$this->loop->stop();
$this->running = false;
$this->logger->info('ReactPHP server stopped');
}
private function setupSignalHandlers(): void
{
if (!function_exists('pcntl_signal')) {
return;
}
$handler = function ($signal) {
$this->logger->info('Received signal', ['signal' => $signal]);
$this->stop();
exit(0);
};
pcntl_signal(SIGTERM, $handler);
pcntl_signal(SIGINT, $handler);
// Enable async signals
pcntl_async_signals(true);
}
private function emitEvent(string $event, array $data = []): void
{
// Log event instead of using event dispatcher to avoid PSR-14 issues
$this->logger->info($event, $data);
}
public function isRunning(): bool
{
return $this->running;
}
/**
* Extract basic request data from ReactPHP ServerRequest
*/
private function extractRequestData(\React\Http\Message\ServerRequest $reactRequest): array
{
$uri = (string) $reactRequest->getUri();
$parsedUrl = parse_url($uri);
return [
'method' => $reactRequest->getMethod(),
'uri' => $uri,
'path' => $parsedUrl['path'] ?? '/',
'query' => $parsedUrl['query'] ?? '',
'headers' => $reactRequest->getHeaders(),
'body' => (string) $reactRequest->getBody(),
];
}
/**
* Create PivotPHP Request from extracted request data
*/
private function createPivotRequest(array $requestData): \PivotPHP\Core\Http\Request
{
// Create base PivotPHP Request
$pivotRequest = new \PivotPHP\Core\Http\Request(
$requestData['method'],
$requestData['path'],
$requestData['path']
);
// Apply headers
$pivotRequest = $this->applyHeaders($pivotRequest, $requestData['headers']);
// Apply query parameters
$pivotRequest = $this->applyQueryParameters($pivotRequest, $requestData['query']);
// Apply body data
$pivotRequest = $this->applyBodyData($pivotRequest, $requestData['body']);
return $pivotRequest;
}
/**
* Apply headers to PivotPHP Request
*/
private function applyHeaders(
\PivotPHP\Core\Http\Request $pivotRequest,
array $headers
): \PivotPHP\Core\Http\Request {
foreach ($headers as $name => $values) {
$headerValue = is_array($values) ? implode(', ', $values) : $values;
$pivotRequest = $pivotRequest->withHeader($name, $headerValue);
}
return $pivotRequest;
}
/**
* Apply query parameters to PivotPHP Request
*/
private function applyQueryParameters(
\PivotPHP\Core\Http\Request $pivotRequest,
string $query
): \PivotPHP\Core\Http\Request {
if ($query !== '') {
parse_str($query, $queryParams);
return $pivotRequest->withQueryParams($queryParams);
}
return $pivotRequest;
}
/**
* Apply body data to PivotPHP Request based on content type
*/
private function applyBodyData(\PivotPHP\Core\Http\Request $pivotRequest, string $body): \PivotPHP\Core\Http\Request
{
if ($body === '') {
return $pivotRequest;
}
$contentType = $pivotRequest->getHeaderLine('Content-Type');
// Parse body based on content type
if (str_contains($contentType, 'application/json')) {
$pivotRequest = $this->parseJsonBody($pivotRequest, $body);
} elseif (str_contains($contentType, 'application/x-www-form-urlencoded')) {
$pivotRequest = $this->parseFormBody($pivotRequest, $body);
}
// Set body stream using StreamFactory for consistency
$streamFactory = new \PivotPHP\Core\Http\Psr7\Factory\StreamFactory();
$stream = $streamFactory->createStream($body);
$pivotRequest = $pivotRequest->withBody($stream);
return $pivotRequest;
}
/**
* Parse JSON body and apply to request
*/
private function parseJsonBody(\PivotPHP\Core\Http\Request $pivotRequest, string $body): \PivotPHP\Core\Http\Request
{
$parsedBody = json_decode($body, true);
if (is_array($parsedBody)) {
return $pivotRequest->withParsedBody($parsedBody);
}
return $pivotRequest;
}
/**
* Parse form-encoded body and apply to request
*/
private function parseFormBody(\PivotPHP\Core\Http\Request $pivotRequest, string $body): \PivotPHP\Core\Http\Request
{
parse_str($body, $parsedBody);
return $pivotRequest->withParsedBody($parsedBody);
}
/**
* Process route and create ReactPHP response
*/
private function processRoute(
\PivotPHP\Core\Http\Request $pivotRequest,
array $requestData
): \React\Http\Message\Response {
$route = \PivotPHP\Core\Routing\Router::identify($requestData['method'], $requestData['path']);
if ($route !== null) {
return $this->executeRoute($route, $pivotRequest);
}
return $this->createNotFoundResponse();
}
/**
* Execute route handler and create response
*/
private function executeRoute(array $route, \PivotPHP\Core\Http\Request $pivotRequest): \React\Http\Message\Response
{
$handler = $route['handler'];
$pivotResponse = new \PivotPHP\Core\Http\Response();
// Capture output from route handler
ob_start();
$handler($pivotRequest, $pivotResponse);
$output = ob_get_clean();
// Create ReactPHP response
return new \React\Http\Message\Response(
200,
['Content-Type' => 'application/json'],
$output !== false ? $output : ''
);
}
/**
* Create 404 Not Found response
*/
private function createNotFoundResponse(): \React\Http\Message\Response
{
$notFoundBody = json_encode(['error' => 'Not Found']);
return new \React\Http\Message\Response(
404,
['Content-Type' => 'application/json'],
$notFoundBody !== false ? $notFoundBody : '{"error":"Not Found"}'
);
}
/**
* Create error response from exception
*/
private function createErrorResponse(\Exception $e): \React\Http\Message\Response
{
$this->logger->error('Error handling request', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
$errorBody = json_encode(['error' => 'Internal Server Error']);
return new \React\Http\Message\Response(
500,
['Content-Type' => 'application/json'],
$errorBody !== false ? $errorBody : '{"error":"Internal Server Error"}'
);
}
}