Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
27
    RequestFactoryInterface,
28
    ResponseFactoryInterface,
29
    ServerRequestFactoryInterface,
30
    StreamFactoryInterface,
31
    UploadedFileFactoryInterface,
32
    UriFactoryInterface
33
{
34
    public function createUploadedFile(
35
        StreamInterface $stream,
36
        int $size = null,
37
        int $error = \UPLOAD_ERR_OK,
38
        string $clientFilename = null,
39
        string $clientMediaType = null
40
    ): UploadedFileInterface {
41
        if ($size === null) {
42
            $size = $stream->getSize();
43
        }
44
 
45
        return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
46
    }
47
 
48
    public function createStream(string $content = ''): StreamInterface
49
    {
50
        return Utils::streamFor($content);
51
    }
52
 
53
    public function createStreamFromFile(string $file, string $mode = 'r'): StreamInterface
54
    {
55
        try {
56
            $resource = Utils::tryFopen($file, $mode);
57
        } catch (\RuntimeException $e) {
58
            if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) {
59
                throw new \InvalidArgumentException(sprintf('Invalid file opening mode "%s"', $mode), 0, $e);
60
            }
61
 
62
            throw $e;
63
        }
64
 
65
        return Utils::streamFor($resource);
66
    }
67
 
68
    public function createStreamFromResource($resource): StreamInterface
69
    {
70
        return Utils::streamFor($resource);
71
    }
72
 
73
    public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
74
    {
75
        if (empty($method)) {
76
            if (!empty($serverParams['REQUEST_METHOD'])) {
77
                $method = $serverParams['REQUEST_METHOD'];
78
            } else {
79
                throw new \InvalidArgumentException('Cannot determine HTTP method');
80
            }
81
        }
82
 
83
        return new ServerRequest($method, $uri, [], null, '1.1', $serverParams);
84
    }
85
 
86
    public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
87
    {
88
        return new Response($code, [], null, '1.1', $reasonPhrase);
89
    }
90
 
91
    public function createRequest(string $method, $uri): RequestInterface
92
    {
93
        return new Request($method, $uri);
94
    }
95
 
96
    public function createUri(string $uri = ''): UriInterface
97
    {
98
        return new Uri($uri);
99
    }
100
}