1 | <?php
|
---|
2 |
|
---|
3 | declare(strict_types=1);
|
---|
4 |
|
---|
5 | namespace GuzzleHttp\Psr7;
|
---|
6 |
|
---|
7 | use Psr\Http\Message\RequestFactoryInterface;
|
---|
8 | use Psr\Http\Message\RequestInterface;
|
---|
9 | use Psr\Http\Message\ResponseFactoryInterface;
|
---|
10 | use Psr\Http\Message\ResponseInterface;
|
---|
11 | use Psr\Http\Message\ServerRequestFactoryInterface;
|
---|
12 | use Psr\Http\Message\ServerRequestInterface;
|
---|
13 | use Psr\Http\Message\StreamFactoryInterface;
|
---|
14 | use Psr\Http\Message\StreamInterface;
|
---|
15 | use Psr\Http\Message\UploadedFileFactoryInterface;
|
---|
16 | use Psr\Http\Message\UploadedFileInterface;
|
---|
17 | use Psr\Http\Message\UriFactoryInterface;
|
---|
18 | use Psr\Http\Message\UriInterface;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Implements all of the PSR-17 interfaces.
|
---|
22 | *
|
---|
23 | * Note: in consuming code it is recommended to require the implemented interfaces
|
---|
24 | * and inject the instance of this class multiple times.
|
---|
25 | */
|
---|
26 | final class HttpFactory implements RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface, UriFactoryInterface
|
---|
27 | {
|
---|
28 | public function createUploadedFile(
|
---|
29 | StreamInterface $stream,
|
---|
30 | ?int $size = null,
|
---|
31 | int $error = \UPLOAD_ERR_OK,
|
---|
32 | ?string $clientFilename = null,
|
---|
33 | ?string $clientMediaType = null
|
---|
34 | ): UploadedFileInterface {
|
---|
35 | if ($size === null) {
|
---|
36 | $size = $stream->getSize();
|
---|
37 | }
|
---|
38 |
|
---|
39 | return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
|
---|
40 | }
|
---|
41 |
|
---|
42 | public function createStream(string $content = ''): StreamInterface
|
---|
43 | {
|
---|
44 | return Utils::streamFor($content);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public function createStreamFromFile(string $file, string $mode = 'r'): StreamInterface
|
---|
48 | {
|
---|
49 | try {
|
---|
50 | $resource = Utils::tryFopen($file, $mode);
|
---|
51 | } catch (\RuntimeException $e) {
|
---|
52 | if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) {
|
---|
53 | throw new \InvalidArgumentException(sprintf('Invalid file opening mode "%s"', $mode), 0, $e);
|
---|
54 | }
|
---|
55 |
|
---|
56 | throw $e;
|
---|
57 | }
|
---|
58 |
|
---|
59 | return Utils::streamFor($resource);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public function createStreamFromResource($resource): StreamInterface
|
---|
63 | {
|
---|
64 | return Utils::streamFor($resource);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
|
---|
68 | {
|
---|
69 | if (empty($method)) {
|
---|
70 | if (!empty($serverParams['REQUEST_METHOD'])) {
|
---|
71 | $method = $serverParams['REQUEST_METHOD'];
|
---|
72 | } else {
|
---|
73 | throw new \InvalidArgumentException('Cannot determine HTTP method');
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | return new ServerRequest($method, $uri, [], null, '1.1', $serverParams);
|
---|
78 | }
|
---|
79 |
|
---|
80 | public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
|
---|
81 | {
|
---|
82 | return new Response($code, [], null, '1.1', $reasonPhrase);
|
---|
83 | }
|
---|
84 |
|
---|
85 | public function createRequest(string $method, $uri): RequestInterface
|
---|
86 | {
|
---|
87 | return new Request($method, $uri);
|
---|
88 | }
|
---|
89 |
|
---|
90 | public function createUri(string $uri = ''): UriInterface
|
---|
91 | {
|
---|
92 | return new Uri($uri);
|
---|
93 | }
|
---|
94 | }
|
---|