1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Discovery; |
4
|
|
|
|
5
|
|
|
use Http\Discovery\Exception\DiscoveryFailedException; |
6
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
7
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
8
|
|
|
use Psr\Http\Message\ServerRequestFactoryInterface; |
9
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
10
|
|
|
use Psr\Http\Message\UploadedFileFactoryInterface; |
11
|
|
|
use Psr\Http\Message\UriFactoryInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Finds PSR-17 factories. |
15
|
|
|
* |
16
|
|
|
* @author Tobias Nyholm <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
final class Psr17FactoryDiscovery extends ClassDiscovery |
19
|
|
|
{ |
20
|
|
|
private static function createException($type, Exception $e) |
21
|
|
|
{ |
22
|
|
|
new \Http\Discovery\Exception\NotFoundException( |
23
|
|
|
'No psr-17 '.$type.' found. Install a package from this list: https://packagist.org/providers/psr/http-factory-implementation', |
24
|
|
|
0, |
25
|
|
|
$e |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return RequestFactoryInterface |
31
|
|
|
* |
32
|
|
|
* @throws Exception\NotFoundException |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public static function findRequestFactory() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
try { |
37
|
|
|
$messageFactory = static::findOneByType(RequestFactoryInterface::class); |
38
|
|
|
} catch (DiscoveryFailedException $e) { |
39
|
|
|
throw self::createException('request factory', $e); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return static::instantiateClass($messageFactory); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return RequestFactoryInterface |
47
|
|
|
* |
48
|
|
|
* @throws Exception\NotFoundException |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public static function findResponseFactory() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
try { |
53
|
|
|
$messageFactory = static::findOneByType(ResponseFactoryInterface::class); |
54
|
|
|
} catch (DiscoveryFailedException $e) { |
55
|
|
|
throw self::createException('response factory', $e); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return static::instantiateClass($messageFactory); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return ServerRequestFactoryInterface |
63
|
|
|
* |
64
|
|
|
* @throws Exception\NotFoundException |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public static function findServerRequestFactory() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
try { |
69
|
|
|
$messageFactory = static::findOneByType(ServerRequestFactoryInterface::class); |
70
|
|
|
} catch (DiscoveryFailedException $e) { |
71
|
|
|
throw self::createException('server request factory', $e); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return static::instantiateClass($messageFactory); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return StreamFactoryInterface |
79
|
|
|
* |
80
|
|
|
* @throws Exception\NotFoundException |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public static function findStreamFactory() |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
try { |
85
|
|
|
$messageFactory = static::findOneByType(StreamFactoryInterface::class); |
86
|
|
|
} catch (DiscoveryFailedException $e) { |
87
|
|
|
throw self::createException('stream factory', $e); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return static::instantiateClass($messageFactory); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return UploadedFileFactoryInterface |
95
|
|
|
* |
96
|
|
|
* @throws Exception\NotFoundException |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public static function findUploadedFileFactory() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
try { |
101
|
|
|
$messageFactory = static::findOneByType(UploadedFileFactoryInterface::class); |
102
|
|
|
} catch (DiscoveryFailedException $e) { |
103
|
|
|
throw self::createException('uploaded file factory', $e); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return static::instantiateClass($messageFactory); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return UriFactoryInterface |
111
|
|
|
* |
112
|
|
|
* @throws Exception\NotFoundException |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public static function findUrlFactory() |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
try { |
117
|
|
|
$messageFactory = static::findOneByType(UriFactoryInterface::class); |
118
|
|
|
} catch (DiscoveryFailedException $e) { |
119
|
|
|
throw self::createException('url factory', $e); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return static::instantiateClass($messageFactory); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.