1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Http\Factory\Discovery; |
5
|
|
|
|
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
8
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestFactoryInterface; |
10
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
11
|
|
|
use Psr\Http\Message\UploadedFileFactoryInterface; |
12
|
|
|
use Psr\Http\Message\UriFactoryInterface; |
13
|
|
|
use RuntimeException; |
14
|
|
|
|
15
|
|
|
final class HttpFactory |
16
|
|
|
{ |
17
|
|
|
/** @var array */ |
18
|
|
|
private static $factories = []; |
19
|
|
|
|
20
|
|
|
public static function requestFactory(): RequestFactoryInterface |
21
|
|
|
{ |
22
|
|
|
return self::factoryInstance(RequestFactoryInterface::class); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public static function responseFactory(): ResponseFactoryInterface |
26
|
|
|
{ |
27
|
|
|
return self::factoryInstance(ResponseFactoryInterface::class); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function serverRequestFactory(): ServerRequestFactoryInterface |
31
|
|
|
{ |
32
|
|
|
return self::factoryInstance(ServerRequestFactoryInterface::class); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public static function streamFactory(): StreamFactoryInterface |
36
|
|
|
{ |
37
|
|
|
return self::factoryInstance(StreamFactoryInterface::class); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function uploadedFileFactory(): UploadedFileFactoryInterface |
41
|
|
|
{ |
42
|
|
|
return self::factoryInstance(UploadedFileFactoryInterface::class); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public static function uriFactory(): UriFactoryInterface |
46
|
|
|
{ |
47
|
|
|
return self::factoryInstance(UriFactoryInterface::class); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private static function factoryInstance(string $factoryInterface) |
51
|
|
|
{ |
52
|
|
|
if (! isset(self::$factories[$factoryInterface])) { |
53
|
|
|
$factory = FactoryLocator::locate($factoryInterface); |
54
|
|
|
self::$factories[$factoryInterface] = new $factory(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return self::$factories[$factoryInterface]; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|