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