This repository has been archived by the owner on Feb 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WellKnownPsr17Factory.php
94 lines (80 loc) · 3.38 KB
/
WellKnownPsr17Factory.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
<?php
/*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FriendsOfPHP\WellKnownImplementations;
use FriendsOfPHP\WellKnownImplementations\Internal\Psr7Helper;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface;
final class WellKnownPsr17Factory implements RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface, UriFactoryInterface
{
/**
* @param UriInterface|string $uri
*/
public function createRequest(string $method, $uri): RequestInterface
{
return new WellKnownPsr7Request(...\func_get_args());
}
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
{
return new WellKnownPsr7Response(...\func_get_args());
}
/**
* @param UriInterface|string $uri
*/
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
{
return new WellKnownPsr7ServerRequest(...\func_get_args());
}
public function createServerRequestFromGlobals(array $server = null, array $get = null, array $post = null, array $cookie = null, array $files = null, StreamInterface $body = null): ServerRequestInterface
{
$server = $server ?? $_SERVER;
$request = new WellKnownPsr7ServerRequest($server['REQUEST_METHOD'] ?? 'GET', $this->createUriFromGlobals($server), $server);
return Psr7Helper::buildServerRequestFromGlobals($request, $server, $files ?? $_FILES)
->withQueryParams($get ?? $_GET)
->withParsedBody($post ?? $_POST)
->withCookieParams($cookie ?? $_COOKIE)
->withBody($body ?? new WellKnownPsr7Stream('php://input', 'r+'));
;
}
public function createStream(string $content = ''): StreamInterface
{
return new WellKnownPsr7Stream($content);
}
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
{
return new WellKnownPsr7Stream($filename, $mode);
}
/**
* @param resource $resource
*/
public function createStreamFromResource($resource): StreamInterface
{
return new WellKnownPsr7Stream($resource);
}
public function createUploadedFile(StreamInterface $stream, int $size = null, int $error = \UPLOAD_ERR_OK, string $clientFilename = null, string $clientMediaType = null): UploadedFileInterface
{
return new WellKnownPsr7UploadedFile(...\func_get_args());
}
public function createUri(string $uri = ''): UriInterface
{
return new WellKnownPsr7Uri(...\func_get_args());
}
public function createUriFromGlobals(array $server = null): UriInterface
{
return Psr7Helper::buildUriFromGlobals(new WellKnownPsr7Uri(''), $server ?? $_SERVER);
}
}