-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerRequestFactory.php
More file actions
49 lines (42 loc) · 1.58 KB
/
ServerRequestFactory.php
File metadata and controls
49 lines (42 loc) · 1.58 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
namespace HackPHP\Http\Request;
use HackPHP\Http\Parsers\UploadedFilesParser;
use HackPHP\Http\Request\ServerRequest;
use HackPHP\Http\Stream\StreamFactory;
use HackPHP\Http\URI\UriFactory;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Swoole\Http\Request;
class ServerRequestFactory implements ServerRequestFactoryInterface
{
/**
* @inheritDoc
*/
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
{
return new ServerRequest($method, $uri, [], null, $serverParams);
}
/**
* Create ServerRequest from Swoole Request.
*
* @param Request $request
* @return ServerRequestInterface
*/
public function createFromSwoole(Request $request): ServerRequestInterface
{
$server = $request->server;
$method = $server['request_method'] ?? 'GET';
$headers = $request->header ?? [];
$uri = (new UriFactory)->createFromSwoole($request);
$body = (new StreamFactory)->createStream($request->rawContent());
$files = (new UploadedFilesParser)($request->files ?? []);
$serverRequest = new ServerRequest($method, $uri, $headers, $body, $server);
$serverRequest = $serverRequest
->withCookieParams($request->cookie ?? [])
->withQueryParams($request->get ?? [])
->withParsedBody($request->post ?? [])
->withUploadedFiles($files);
unset($server, $method, $headers, $uri, $body, $files);
return $serverRequest;
}
}