1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Simply\Application\HttpFactory; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\RequestInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
use Psr\Http\Message\StreamInterface; |
9
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
10
|
|
|
use Psr\Http\Message\UriInterface; |
11
|
|
|
use Zend\Diactoros\RequestFactory; |
12
|
|
|
use Zend\Diactoros\ResponseFactory; |
13
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
14
|
|
|
use Zend\Diactoros\StreamFactory; |
15
|
|
|
use Zend\Diactoros\UploadedFileFactory; |
16
|
|
|
use Zend\Diactoros\UriFactory; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* A http factory that uses Zend's Diactoros library to implement the requests and responses. |
20
|
|
|
* @author Riikka Kalliomäki <[email protected]> |
21
|
|
|
* @copyright Copyright (c) 2018 Riikka Kalliomäki |
22
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
23
|
|
|
*/ |
24
|
|
|
class DiactorosHttpFactory implements HttpFactoryInterface |
25
|
|
|
{ |
26
|
|
|
private $requestFactory; |
27
|
|
|
private $responseFactory; |
28
|
|
|
private $serverRequestFactory; |
29
|
|
|
private $streamFactory; |
30
|
|
|
private $uploadedFileFactory; |
31
|
|
|
private $uriFactory; |
32
|
|
|
|
33
|
20 |
|
public function __construct() |
34
|
|
|
{ |
35
|
20 |
|
$this->requestFactory = new RequestFactory(); |
36
|
20 |
|
$this->responseFactory = new ResponseFactory(); |
37
|
20 |
|
$this->serverRequestFactory = new ServerRequestFactory(); |
38
|
20 |
|
$this->streamFactory = new StreamFactory(); |
39
|
20 |
|
$this->uploadedFileFactory = new UploadedFileFactory(); |
40
|
20 |
|
$this->uriFactory = new UriFactory(); |
41
|
20 |
|
} |
42
|
|
|
|
43
|
|
|
/** {@inheritdoc} */ |
44
|
1 |
|
public function createRequest(string $method, $uri): RequestInterface |
45
|
|
|
{ |
46
|
1 |
|
return $this->requestFactory->createRequest($method, $uri); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** {@inheritdoc} */ |
50
|
11 |
|
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
51
|
|
|
{ |
52
|
11 |
|
return $this->responseFactory->createResponse($code, $reasonPhrase); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** {@inheritdoc} */ |
56
|
10 |
|
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface |
57
|
|
|
{ |
58
|
10 |
|
return $this->serverRequestFactory->createServerRequest($method, $uri, $serverParams); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** {@inheritdoc} */ |
62
|
1 |
|
public function createServerRequestFromGlobals(): ServerRequestInterface |
63
|
|
|
{ |
64
|
1 |
|
return ServerRequestFactory::fromGlobals(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** {@inheritdoc} */ |
68
|
10 |
|
public function createStream(string $content = ''): StreamInterface |
69
|
|
|
{ |
70
|
10 |
|
return $this->streamFactory->createStream($content); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** {@inheritdoc} */ |
74
|
2 |
|
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
75
|
|
|
{ |
76
|
2 |
|
return $this->streamFactory->createStreamFromFile($filename, $mode); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** {@inheritdoc} */ |
80
|
2 |
|
public function createStreamFromResource($resource): StreamInterface |
81
|
|
|
{ |
82
|
2 |
|
return $this->streamFactory->createStreamFromResource($resource); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** {@inheritdoc} */ |
86
|
1 |
|
public function createUploadedFile( |
87
|
|
|
StreamInterface $stream, |
88
|
|
|
int $size = null, |
89
|
|
|
int $error = \UPLOAD_ERR_OK, |
90
|
|
|
string $clientFilename = null, |
91
|
|
|
string $clientMediaType = null |
92
|
|
|
): UploadedFileInterface { |
93
|
1 |
|
return $this->uploadedFileFactory |
94
|
1 |
|
->createUploadedFile($stream, $size, $error, $clientFilename, $clientMediaType); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** {@inheritdoc} */ |
98
|
1 |
|
public function createUri(string $uri = ''): UriInterface |
99
|
|
|
{ |
100
|
1 |
|
return $this->uriFactory->createUri($uri); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|