1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Tebe\HttpFactory; |
5
|
|
|
|
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 RuntimeException; |
12
|
|
|
use Tebe\HttpFactory\Factory\DiactorosFactory; |
13
|
|
|
use Tebe\HttpFactory\Factory\FactoryInterface; |
14
|
|
|
use Tebe\HttpFactory\Factory\GuzzleFactory; |
15
|
|
|
use Tebe\HttpFactory\Factory\NyholmFactory; |
16
|
|
|
use Tebe\HttpFactory\Factory\SlimFactory; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Simple class to create instances of PSR-7 classes. |
20
|
|
|
*/ |
21
|
|
|
class HttpFactory implements FactoryInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var FactoryInterface |
26
|
|
|
*/ |
27
|
|
|
private $factory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private static $strategies = [ |
33
|
|
|
DiactorosFactory::class, |
34
|
|
|
GuzzleFactory::class, |
35
|
|
|
SlimFactory::class, |
36
|
|
|
NyholmFactory::class |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param FactoryInterface $factory |
41
|
|
|
* @return HttpFactory |
42
|
|
|
*/ |
43
|
|
|
public function setFactory(FactoryInterface $factory) |
44
|
|
|
{ |
45
|
|
|
$this->factory = $factory; |
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return FactoryInterface |
51
|
|
|
*/ |
52
|
|
|
public function getFactory() |
53
|
|
|
{ |
54
|
|
|
if ($this->factory !== null) { |
55
|
|
|
return $this->factory; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
foreach (self::$strategies as $className) { |
59
|
|
|
if (!class_exists($className)) { |
60
|
|
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (strpos($className, __NAMESPACE__) === 0 && !$className::isInstalled()) { |
64
|
|
|
continue; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->factory = new $className(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
throw new RuntimeException('No PSR-7 library detected'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
|
|
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
77
|
|
|
{ |
78
|
|
|
return $this->getFactory()->createResponse($code, $reasonPhrase); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritdoc |
83
|
|
|
*/ |
84
|
|
|
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface |
85
|
|
|
{ |
86
|
|
|
return $this->getFactory()->createServerRequest($method, $uri, $serverParams); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
*/ |
92
|
|
|
public function createServerRequestFromGlobals() : ServerRequestInterface |
93
|
|
|
{ |
94
|
|
|
return $this->getFactory()->createServerRequestFromGlobals(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritdoc |
99
|
|
|
*/ |
100
|
|
|
public function createStream(string $content = ''): StreamInterface |
101
|
|
|
{ |
102
|
|
|
return $this->getFactory()->createStream($content); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritdoc |
107
|
|
|
*/ |
108
|
|
|
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
109
|
|
|
{ |
110
|
|
|
return $this->getFactory()->createStreamFromFile($filename, $mode); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @inheritdoc |
115
|
|
|
*/ |
116
|
|
|
public function createStreamFromResource($resource): StreamInterface |
117
|
|
|
{ |
118
|
|
|
return $this->getFactory()->createStreamFromResource($resource); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @inheritdoc |
123
|
|
|
*/ |
124
|
|
|
public function createUri(string $uri = ''): UriInterface |
125
|
|
|
{ |
126
|
|
|
return $this->getFactory()->createUri($uri); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritdoc |
131
|
|
|
*/ |
132
|
|
|
public function createUploadedFile( |
133
|
|
|
StreamInterface $stream, |
134
|
|
|
int $size = null, |
135
|
|
|
int $error = \UPLOAD_ERR_OK, |
136
|
|
|
string $clientFilename = null, |
137
|
|
|
string $clientMediaType = null |
138
|
|
|
): UploadedFileInterface { |
139
|
|
|
return $this->getFactory()->createUploadedFile($stream, $size, $error, $clientFilename, $clientMediaType); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return static |
144
|
|
|
*/ |
145
|
|
|
public static function instance() |
146
|
|
|
{ |
147
|
|
|
static $instance; |
148
|
|
|
if (is_null($instance)) { |
149
|
|
|
$instance = new static(); |
150
|
|
|
} |
151
|
|
|
return $instance; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param array $strategies |
156
|
|
|
*/ |
157
|
|
|
public static function setStrategies(array $strategies) |
158
|
|
|
{ |
159
|
|
|
static::$strategies = $strategies; |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
|
|
public static function getStrategies() |
166
|
|
|
{ |
167
|
|
|
return static::$strategies; |
|
|
|
|
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|