-
Notifications
You must be signed in to change notification settings - Fork 2
/
OnMessage.php
44 lines (36 loc) · 1.47 KB
/
OnMessage.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
<?php
declare(strict_types=1);
namespace Manyou\WorkermanSymfonyRuntime;
use Chubbyphp\WorkermanRequestHandler\OnMessageInterface;
use Chubbyphp\WorkermanRequestHandler\PsrRequestFactoryInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Workerman\Connection\TcpConnection as WorkermanTcpConnection;
use Workerman\Protocols\Http\Request as WorkermanRequest;
use Workerman\Protocols\Http\Response as WorkermanResponse;
use function explode;
use function in_array;
use function strtolower;
final class OnMessage implements OnMessageInterface
{
public function __construct(
private PsrRequestFactoryInterface $psrRequestFactory,
private RequestHandlerInterface $requestHander,
) {
}
public function __invoke(WorkermanTcpConnection $workermanTcpConnection, WorkermanRequest $workermanRequest): void
{
$response = $this->requestHander->handle(
$this->psrRequestFactory->create($workermanTcpConnection, $workermanRequest),
);
$workermanTcpConnection->send(
(new WorkermanResponse())
->withStatus($response->getStatusCode(), $response->getReasonPhrase())
->withHeaders($response->getHeaders())
->withBody((string) $response->getBody()),
);
$keepAlive = in_array('keep-alive', explode(',', strtolower($workermanRequest->header('connection', 'close'))), true);
if (! $keepAlive) {
$workermanTcpConnection->close();
}
}
}