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