-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHeaderNameMapper.php
39 lines (31 loc) · 1.12 KB
/
HeaderNameMapper.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
<?php
declare(strict_types=1);
namespace Manyou\WorkermanSymfonyRuntime;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class HeaderNameMapper implements HttpMessageFactoryInterface
{
public function __construct(
private HttpMessageFactoryInterface $httpMessageFactory,
private array $mappings = [],
) {
}
public function createRequest(Request $symfonyRequest): ServerRequestInterface
{
return $this->httpMessageFactory->createRequest($symfonyRequest);
}
public function createResponse(Response $symfonyResponse): ResponseInterface
{
$response = $this->httpMessageFactory->createResponse($symfonyResponse);
$headers = $response->getHeaders();
foreach ($headers as $key => $value) {
if ($name = $this->mappings[$key] ?? false) {
$response = $response->withHeader($name, $value);
}
}
return $response;
}
}