-
Notifications
You must be signed in to change notification settings - Fork 2
/
SymfonyRequestHandler.php
40 lines (33 loc) · 1.35 KB
/
SymfonyRequestHandler.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
<?php
declare(strict_types=1);
namespace Manyou\WorkermanSymfonyRuntime;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface;
use Workerman\Timer;
class SymfonyRequestHandler implements RequestHandlerInterface
{
public function __construct(
private HttpKernelInterface $kernel,
private HttpFoundationFactoryInterface $httpFoundationFactory,
private HttpMessageFactoryInterface $httpMessageFactory,
) {
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
$sfRequest = $this->httpFoundationFactory->createRequest($request);
$sfResponse = $this->kernel->handle($sfRequest);
$response = $this->httpMessageFactory->createResponse($sfResponse);
if ($this->kernel instanceof TerminableInterface) {
$kernel = $this->kernel;
Timer::add(1, static function () use ($kernel, $sfRequest, $sfResponse) {
$kernel->terminate($sfRequest, $sfResponse);
}, persistent: false);
}
return $response;
}
}