-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathKernelAdapter.php
More file actions
49 lines (41 loc) · 1.67 KB
/
KernelAdapter.php
File metadata and controls
49 lines (41 loc) · 1.67 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
<?php declare(strict_types=1);
namespace Bref\SymfonyBridge\Http;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface;
/**
* This turns a Symfony Kernel into a PSR-15 handler.
*
* That means the Symfony Kernel can now be used by Bref (which supports PSR-15)
* to handle HTTP requests from API Gateway.
*/
class KernelAdapter implements RequestHandlerInterface
{
private HttpKernelInterface $kernel;
// PSR-15 to Symfony converters
private HttpFoundationFactory $symfonyFactory;
private PsrHttpFactory $psrFactory;
public function __construct(HttpKernelInterface $kernel)
{
$this->kernel = $kernel;
$this->symfonyFactory = new HttpFoundationFactory;
$psr17Factory = new Psr17Factory;
$this->psrFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
// From PSR-7 to Symfony
$symfonyRequest = $this->symfonyFactory->createRequest($request);
$symfonyResponse = $this->kernel->handle($symfonyRequest);
if ($this->kernel instanceof TerminableInterface) {
$this->kernel->terminate($symfonyRequest, $symfonyResponse);
}
// From Symfony to PSR-7
return $this->psrFactory->createResponse($symfonyResponse);
}
}