|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace PTS\Psr7\Factory; |
|
5
|
|
|
|
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Nyholm\Psr7Server\ServerRequestCreator; |
|
8
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
|
9
|
|
|
use Psr\Http\Message\RequestInterface; |
|
10
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
use Psr\Http\Message\ServerRequestFactoryInterface; |
|
13
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
14
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
|
15
|
|
|
use Psr\Http\Message\StreamInterface; |
|
16
|
|
|
use Psr\Http\Message\UploadedFileFactoryInterface; |
|
17
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
|
18
|
|
|
use Psr\Http\Message\UriFactoryInterface; |
|
19
|
|
|
use Psr\Http\Message\UriInterface; |
|
20
|
|
|
use PTS\Psr7\Request; |
|
21
|
|
|
use PTS\Psr7\Response; |
|
22
|
|
|
use PTS\Psr7\ServerRequest; |
|
23
|
|
|
use PTS\Psr7\Stream; |
|
24
|
|
|
use PTS\Psr7\UploadedFile; |
|
25
|
|
|
use PTS\Psr7\Uri; |
|
26
|
|
|
use RuntimeException; |
|
27
|
|
|
use Throwable; |
|
28
|
|
|
use function error_get_last; |
|
29
|
|
|
use function fopen; |
|
30
|
|
|
use function func_num_args; |
|
31
|
|
|
use function in_array; |
|
32
|
|
|
use function sprintf; |
|
33
|
|
|
|
|
34
|
|
|
class Psr17Factory |
|
35
|
|
|
implements RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, |
|
36
|
|
|
UploadedFileFactoryInterface, UriFactoryInterface |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
1 |
|
/** @var ServerRequestCreator|null */ |
|
40
|
|
|
protected ?ServerRequestCreator $creatorFromGlobal = null; |
|
41
|
1 |
|
|
|
42
|
1 |
|
public function createRequest(string $method, $uri): RequestInterface |
|
43
|
|
|
{ |
|
44
|
|
|
if (is_string($uri)) { |
|
45
|
1 |
|
$uri = $this->createUri($uri); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
return new Request($method, $uri); |
|
49
|
|
|
} |
|
50
|
1 |
|
|
|
51
|
1 |
|
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
|
52
|
|
|
{ |
|
53
|
|
|
if (2 > func_num_args()) { |
|
54
|
1 |
|
$reasonPhrase = null; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
10 |
|
return new Response($code, [], null, '1.1', $reasonPhrase); |
|
58
|
|
|
} |
|
59
|
10 |
|
|
|
60
|
|
|
public function createStream(string $content = ''): StreamInterface |
|
61
|
|
|
{ |
|
62
|
3 |
|
return Stream::create($content); |
|
63
|
|
|
} |
|
64
|
3 |
|
|
|
65
|
3 |
|
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
|
66
|
2 |
|
{ |
|
67
|
1 |
|
$resource = @fopen($filename, $mode); |
|
68
|
|
|
if (false === $resource) { |
|
69
|
|
|
if ('' === $mode || false === in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) { |
|
70
|
1 |
|
throw new InvalidArgumentException(sprintf('The mode "%s" is invalid.', $mode)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
throw new RuntimeException(sprintf('The file "%s" cannot be opened.', $filename)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
return Stream::create($resource); |
|
77
|
|
|
} |
|
78
|
2 |
|
|
|
79
|
|
|
public function createStreamFromResource($resource): StreamInterface |
|
80
|
|
|
{ |
|
81
|
1 |
|
return Stream::create($resource); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function createUploadedFile( |
|
85
|
|
|
StreamInterface $stream, |
|
86
|
|
|
int $size = null, |
|
87
|
|
|
int $error = \UPLOAD_ERR_OK, |
|
88
|
1 |
|
string $clientFilename = null, |
|
89
|
1 |
|
string $clientMediaType = null |
|
90
|
|
|
): UploadedFileInterface { |
|
91
|
|
|
if (null === $size) { |
|
92
|
1 |
|
$size = $stream->getSize(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
3 |
|
return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType); |
|
96
|
|
|
} |
|
97
|
3 |
|
|
|
98
|
|
|
public function createUri(string $uri = ''): UriInterface |
|
99
|
|
|
{ |
|
100
|
2 |
|
return new Uri($uri); |
|
101
|
|
|
} |
|
102
|
2 |
|
|
|
103
|
1 |
|
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface |
|
104
|
|
|
{ |
|
105
|
|
|
if (is_string($uri)) { |
|
106
|
2 |
|
$uri = $this->createUri($uri); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
return new ServerRequest($method, $uri, [], null, '1.1', $serverParams); |
|
110
|
|
|
} |
|
111
|
1 |
|
|
|
112
|
1 |
|
public function fromGlobals(): ServerRequestInterface |
|
113
|
1 |
|
{ |
|
114
|
|
|
if ($this->creatorFromGlobal === null) { |
|
115
|
|
|
$psr17 = new static; |
|
116
|
1 |
|
$this->creatorFromGlobal = new ServerRequestCreator($psr17, $psr17, $psr17, $psr17); |
|
117
|
|
|
} |
|
118
|
1 |
|
|
|
119
|
|
|
return $this->creatorFromGlobal->fromGlobals(); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.