-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPsrRequestFactory.php
More file actions
89 lines (74 loc) · 2.99 KB
/
PsrRequestFactory.php
File metadata and controls
89 lines (74 loc) · 2.99 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
<?php
declare(strict_types=1);
namespace Chubbyphp\SwooleRequestHandler;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UploadedFileInterface;
use Swoole\Http\Request as SwooleRequest;
final class PsrRequestFactory implements PsrRequestFactoryInterface
{
public function __construct(
private ServerRequestFactoryInterface $serverRequestFactory,
private StreamFactoryInterface $streamFactory,
private UploadedFileFactoryInterface $uploadedFileFactory
) {}
public function create(SwooleRequest $swooleRequest): ServerRequestInterface
{
$server = array_change_key_case($swooleRequest->server ?? [], CASE_UPPER);
list($scheme, $protocolVersion) = explode('/', $server['SERVER_PROTOCOL']);
$scheme = strtolower($scheme);
$host = $server["SERVER_HOST"] ?? '0.0.0.0';
$port = $server["SERVER_PORT"] ?? '';
$port = in_array($port, [80, 443]) ? '' : ":{$port}";
$requestUri = $server['REQUEST_URI'] ?? '';
$queryString = $server['QUERY_STRING'] ?? '';
$uri = $scheme . '://' . $host . $port . $requestUri . ($queryString ? "?{$queryString}" : '');
$request = $this->serverRequestFactory->createServerRequest(
$server['REQUEST_METHOD'] ?? 'GET',
$uri,
$server
);
foreach ($swooleRequest->header ?? [] as $name => $value) {
$request = $request->withHeader($name, $value);
}
$request = $request->withCookieParams($swooleRequest->cookie ?? []);
$request = $request->withQueryParams($swooleRequest->get ?? []);
$request = $request->withParsedBody($swooleRequest->post ?? []);
$request = $request->withUploadedFiles($this->uploadedFiles($swooleRequest->files ?? []));
$request->getBody()->write($swooleRequest->rawContent());
return $request;
}
/**
* @param array<string, array<string, int|string>> $files
*
* @return array<string, UploadedFileInterface>
*/
private function uploadedFiles(array $files): array
{
$uploadedFiles = [];
foreach ($files as $key => $file) {
$uploadedFiles[$key] = isset($file['tmp_name']) ? $this->createUploadedFile($file) : $this->uploadedFiles($file);
}
return $uploadedFiles;
}
/**
* @param array<string, int|string> $file
*/
private function createUploadedFile(array $file): UploadedFileInterface
{
try {
$stream = $this->streamFactory->createStreamFromFile($file['tmp_name']);
} catch (\RuntimeException) {
$stream = $this->streamFactory->createStream();
}
return $this->uploadedFileFactory->createUploadedFile(
$stream,
$file['size'],
$file['error'],
$file['name'],
$file['type']
);
}
}