-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpFactory.php
98 lines (85 loc) · 2.53 KB
/
HttpFactory.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
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
90
91
92
93
94
95
96
97
98
<?php
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <[email protected]>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*
*/
namespace Koded\Http;
/*
*
* Implementation of PSR-17 (HTTP Message Factories)
* @see https://www.php-fig.org/psr/psr-17/
*
*/
use Psr\Http\Message\{RequestFactoryInterface,
RequestInterface,
ResponseFactoryInterface,
ResponseInterface,
ServerRequestFactoryInterface,
ServerRequestInterface,
StreamFactoryInterface,
StreamInterface,
UploadedFileFactoryInterface,
UploadedFileInterface,
UriFactoryInterface,
UriInterface};
class HttpFactory implements RequestFactoryInterface,
ResponseFactoryInterface,
ServerRequestFactoryInterface,
StreamFactoryInterface,
UploadedFileFactoryInterface,
UriFactoryInterface
{
public function createRequest(string $method, $uri): RequestInterface
{
return new ClientRequest($method, $uri);
}
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
{
if ($serverParams) {
$_SERVER = \array_replace($_SERVER, $serverParams);
}
$_SERVER['REQUEST_METHOD'] = $method;
$_SERVER['REQUEST_URI'] = (string)$uri;
return new ServerRequest;
}
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
{
return (new ServerResponse)->withStatus($code, $reasonPhrase);
}
public function createStream(string $content = ''): StreamInterface
{
return create_stream($content);
}
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
{
return new FileStream($filename, $mode);
}
public function createStreamFromResource($resource): StreamInterface
{
return create_stream($resource);
}
public function createUri(string $uri = ''): UriInterface
{
return new Uri($uri);
}
public function createUploadedFile(
StreamInterface $stream,
?int $size = null,
?int $error = \UPLOAD_ERR_OK,
?string $clientFilename = null,
?string $clientMediaType = null
): UploadedFileInterface {
return new UploadedFile([
'tmp_name' => $stream,
'name' => $clientFilename,
'type' => $clientMediaType,
'size' => $size,
'error' => $error,
]);
}
}