1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Tebe\HttpFactory\Factory; |
5
|
|
|
|
6
|
|
|
use Laminas\Diactoros\Response; |
7
|
|
|
use Laminas\Diactoros\ServerRequest; |
8
|
|
|
use Laminas\Diactoros\ServerRequestFactory; |
9
|
|
|
use Laminas\Diactoros\Stream; |
10
|
|
|
use Laminas\Diactoros\UploadedFile; |
11
|
|
|
use Laminas\Diactoros\Uri; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
14
|
|
|
use Psr\Http\Message\StreamInterface; |
15
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
16
|
|
|
use Psr\Http\Message\UriInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Simple class to create response instances of PSR-7 classes. |
20
|
|
|
*/ |
21
|
|
|
class DiactorosFactory implements FactoryInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Check whether Laminas Diactoros is available |
25
|
|
|
*/ |
26
|
|
|
public static function isInstalled(): bool |
27
|
|
|
{ |
28
|
|
|
return class_exists('Laminas\\Diactoros\\Response') |
29
|
|
|
&& class_exists('Laminas\\Diactoros\\ServerRequest') |
30
|
|
|
&& class_exists('Laminas\\Diactoros\\Stream') |
31
|
|
|
&& class_exists('Laminas\\Diactoros\\Uri'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
|
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
38
|
|
|
{ |
39
|
|
|
$response = new Response('php://memory', $code); |
40
|
|
|
|
41
|
|
|
return $reasonPhrase !== '' ? $response->withStatus($code, $reasonPhrase) : $response; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
|
|
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface |
48
|
|
|
{ |
49
|
|
|
return new ServerRequest( |
50
|
|
|
$serverParams, |
51
|
|
|
[], |
52
|
|
|
$uri, |
53
|
|
|
$method, |
54
|
|
|
$this->createStream() |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
61
|
|
|
public function createServerRequestFromGlobals(): ServerRequestInterface |
62
|
|
|
{ |
63
|
|
|
return ServerRequestFactory::fromGlobals(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritdoc |
68
|
|
|
*/ |
69
|
|
|
public function createStream(string $content = ''): StreamInterface |
70
|
|
|
{ |
71
|
|
|
$stream = $this->createStreamFromFile('php://temp', 'r+'); |
72
|
|
|
$stream->write($content); |
73
|
|
|
|
74
|
|
|
return $stream; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritdoc |
79
|
|
|
*/ |
80
|
|
|
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
81
|
|
|
{ |
82
|
|
|
$resource = fopen($filename, $mode); |
83
|
|
|
return $this->createStreamFromResource($resource); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
89
|
|
|
public function createStreamFromResource($resource): StreamInterface |
90
|
|
|
{ |
91
|
|
|
return new Stream($resource); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritdoc |
96
|
|
|
*/ |
97
|
|
|
public function createUri(string $uri = ''): UriInterface |
98
|
|
|
{ |
99
|
|
|
return new Uri($uri); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritdoc |
104
|
|
|
*/ |
105
|
|
|
public function createUploadedFile( |
106
|
|
|
StreamInterface $stream, |
107
|
|
|
int $size = null, |
108
|
|
|
int $error = \UPLOAD_ERR_OK, |
109
|
|
|
string $clientFilename = null, |
110
|
|
|
string $clientMediaType = null |
111
|
|
|
): UploadedFileInterface { |
112
|
|
|
return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|