1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Nyholm\Psr7\Factory; |
6
|
|
|
|
7
|
|
|
use Nyholm\Psr7\{Request, Response, ServerRequest, Stream, UploadedFile, Uri}; |
8
|
|
|
use Psr\Http\Message\{RequestFactoryInterface, RequestInterface, ResponseFactoryInterface, ResponseInterface, ServerRequestFactoryInterface, ServerRequestInterface, StreamFactoryInterface, StreamInterface, UploadedFileFactoryInterface, UploadedFileInterface, UriFactoryInterface, UriInterface}; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Tobias Nyholm <[email protected]> |
12
|
|
|
* @author Martijn van der Ven <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
final class Psr17Factory implements RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface, UriFactoryInterface |
15
|
|
|
{ |
16
|
7 |
|
public function createRequest(string $method, $uri): RequestInterface |
17
|
|
|
{ |
18
|
7 |
|
return new Request($method, $uri); |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
4 |
|
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
22
|
|
|
{ |
23
|
4 |
|
return new Response($code, [], null, '1.1', $reasonPhrase); |
24
|
|
|
} |
25
|
|
|
|
26
|
6 |
|
public function createStream(string $content = ''): StreamInterface |
27
|
|
|
{ |
28
|
6 |
|
return Stream::create($content); |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
32
|
|
|
{ |
33
|
1 |
|
$resource = @\fopen($filename, $mode); |
34
|
1 |
|
if (false === $resource) { |
35
|
|
|
throw new \RuntimeException('The file '.$filename.'cannot be opened.'); |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
return Stream::create($resource); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
public function createStreamFromResource($resource): StreamInterface |
42
|
|
|
{ |
43
|
1 |
|
return Stream::create($resource); |
44
|
|
|
} |
45
|
|
|
|
46
|
12 |
|
public function createUploadedFile(StreamInterface $stream, int $size = null, int $error = \UPLOAD_ERR_OK, string $clientFilename = null, string $clientMediaType = null): UploadedFileInterface |
47
|
|
|
{ |
48
|
12 |
|
if (null === $size) { |
49
|
12 |
|
$size = $stream->getSize(); |
50
|
|
|
} |
51
|
|
|
|
52
|
12 |
|
return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType); |
53
|
|
|
} |
54
|
|
|
|
55
|
13 |
|
public function createUri(string $uri = ''): UriInterface |
56
|
|
|
{ |
57
|
13 |
|
return new Uri($uri); |
58
|
|
|
} |
59
|
|
|
|
60
|
23 |
|
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface |
61
|
|
|
{ |
62
|
23 |
|
return new ServerRequest($method, $uri, [], null, '1.1', $serverParams); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.