17
17
use Geocoder \Exception \QuotaExceeded ;
18
18
use Geocoder \Provider \AbstractProvider ;
19
19
use Http \Message \MessageFactory ;
20
- use Http \Discovery \MessageFactoryDiscovery ;
20
+ use Http \Discovery \Psr17Factory ;
21
21
use Psr \Http \Client \ClientInterface ;
22
+ use Psr \Http \Message \RequestFactoryInterface ;
22
23
use Psr \Http \Message \RequestInterface ;
24
+ use Psr \Http \Message \ResponseFactoryInterface ;
25
+ use Psr \Http \Message \ResponseInterface ;
26
+ use Psr \Http \Message \StreamFactoryInterface ;
27
+ use Psr \Http \Message \StreamInterface ;
23
28
24
29
/**
25
30
* @author William Durand <[email protected] >
@@ -33,18 +38,14 @@ abstract class AbstractHttpProvider extends AbstractProvider
33
38
private $ client ;
34
39
35
40
/**
36
- * @var MessageFactory
41
+ * @var RequestFactoryInterface&StreamFactoryInterface
37
42
*/
38
43
private $ messageFactory ;
39
44
40
- /**
41
- * @param ClientInterface $client
42
- * @param MessageFactory|null $factory
43
- */
44
- public function __construct (ClientInterface $ client , MessageFactory $ factory = null )
45
+ public function __construct (ClientInterface $ client )
45
46
{
46
47
$ this ->client = $ client ;
47
- $ this ->messageFactory = $ factory ?: MessageFactoryDiscovery:: find ();
48
+ $ this ->messageFactory = $ client instanceof RequestFactoryInterface && $ client instanceof StreamFactoryInterface ? $ client : new Psr17Factory ();
48
49
}
49
50
50
51
/**
@@ -70,7 +71,28 @@ protected function getUrlContents(string $url): string
70
71
*/
71
72
protected function getRequest (string $ url ): RequestInterface
72
73
{
73
- return $ this ->getMessageFactory ()->createRequest ('GET ' , $ url );
74
+ return $ this ->createRequest ('GET ' , $ url );
75
+ }
76
+
77
+ protected function createRequest (string $ method , string $ uri , array $ headers = [], string $ body = null ): RequestInterface
78
+ {
79
+ $ request = $ this ->messageFactory ->createRequest ($ method , $ uri );
80
+
81
+ foreach ($ headers as $ name => $ value ) {
82
+ $ request = $ request ->withAddedHeader ($ name , $ value );
83
+ }
84
+
85
+ if (null === $ body ) {
86
+ return $ request ;
87
+ }
88
+
89
+ $ stream = $ this ->messageFactory ->createStream ($ body );
90
+
91
+ if ($ stream ->isSeekable ()) {
92
+ $ stream ->seek (0 );
93
+ }
94
+
95
+ return $ request ->withBody ($ stream );
74
96
}
75
97
76
98
/**
@@ -114,10 +136,70 @@ protected function getHttpClient(): ClientInterface
114
136
}
115
137
116
138
/**
117
- * @return MessageFactory
139
+ * @deprecated Use createRequest instead
118
140
*/
119
141
protected function getMessageFactory (): MessageFactory
120
142
{
121
- return $ this ->messageFactory ;
143
+ $ factory = $ this ->messageFactory instanceof ResponseFactoryInterface ? $ this ->messageFactory : new Psr17Factory ();
144
+
145
+ return new class ($ factory ) implements MessageFactory {
146
+ private $ factory ;
147
+
148
+ public function __construct ($ factory )
149
+ {
150
+ $ this ->factory = $ factory ;
151
+ }
152
+
153
+ public function createRequest ($ method , $ uri , array $ headers = [], $ body = null , $ protocolVersion = '1.1 ' ): RequestInterface
154
+ {
155
+ $ request = $ this ->factory ->createRequest ($ method , $ uri );
156
+
157
+ foreach ($ headers as $ name => $ value ) {
158
+ $ request = $ request ->withAddedHeader ($ name , $ value );
159
+ }
160
+
161
+ if (null !== $ body ) {
162
+ $ request = $ request ->withBody ($ this ->createStream ($ body ));
163
+ }
164
+
165
+ return $ request ->withProtocolVersion ($ protocolVersion );
166
+ }
167
+
168
+ public function createResponse ($ statusCode = 200 , $ reasonPhrase = null , array $ headers = [], $ body = null , $ protocolVersion = '1.1 ' ): ResponseInterface
169
+ {
170
+ $ response = $ this ->factory ->createResponse ($ statusCode , $ reasonPhrase );
171
+
172
+ foreach ($ headers as $ name => $ value ) {
173
+ $ response = $ response ->withAddedHeader ($ name , $ value );
174
+ }
175
+
176
+ if (null !== $ body ) {
177
+ $ response = $ response ->withBody ($ this ->createStream ($ body ));
178
+ }
179
+
180
+ return $ response ->withProtocolVersion ($ protocolVersion );
181
+ }
182
+
183
+ private function createStream ($ content = '' ): StreamInterface
184
+ {
185
+ if ($ content instanceof StreamInterface) {
186
+ return $ content ;
187
+ }
188
+
189
+ if (\is_string ($ content ?? '' )) {
190
+ $ stream = $ this ->factory ->createStream ($ content ?? '' );
191
+ } elseif (\is_resource ($ content )) {
192
+ $ stream = $ this ->factory ->createStreamFromResource ($ content );
193
+ } else {
194
+ throw new \InvalidArgumentException (sprintf ('"%s()" expects string, resource or StreamInterface, "%s" given. ' , __METHOD__ , get_debug_type ($ content )));
195
+ }
196
+
197
+ if ($ stream ->isSeekable ()) {
198
+ $ stream ->seek (0 );
199
+ }
200
+
201
+ return $ stream ;
202
+ }
203
+ };
122
204
}
123
205
}
0 commit comments