16
16
use Geocoder \Exception \InvalidServerResponse ;
17
17
use Geocoder \Exception \QuotaExceeded ;
18
18
use Geocoder \Provider \AbstractProvider ;
19
- use Http \Discovery \MessageFactoryDiscovery ;
20
19
use Http \Message \MessageFactory ;
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,14 +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
- public function __construct (ClientInterface $ client, MessageFactory $ factory = null )
45
+ public function __construct (ClientInterface $ client )
41
46
{
42
47
$ this ->client = $ client ;
43
- $ this ->messageFactory = $ factory ?: MessageFactoryDiscovery:: find ();
48
+ $ this ->messageFactory = $ client instanceof RequestFactoryInterface && $ client instanceof StreamFactoryInterface ? $ client : new Psr17Factory ();
44
49
}
45
50
46
51
/**
@@ -57,7 +62,28 @@ protected function getUrlContents(string $url): string
57
62
58
63
protected function getRequest (string $ url ): RequestInterface
59
64
{
60
- return $ this ->getMessageFactory ()->createRequest ('GET ' , $ url );
65
+ return $ this ->createRequest ('GET ' , $ url );
66
+ }
67
+
68
+ protected function createRequest (string $ method , string $ uri , array $ headers = [], string $ body = null ): RequestInterface
69
+ {
70
+ $ request = $ this ->messageFactory ->createRequest ($ method , $ uri );
71
+
72
+ foreach ($ headers as $ name => $ value ) {
73
+ $ request = $ request ->withAddedHeader ($ name , $ value );
74
+ }
75
+
76
+ if (null === $ body ) {
77
+ return $ request ;
78
+ }
79
+
80
+ $ stream = $ this ->messageFactory ->createStream ($ body );
81
+
82
+ if ($ stream ->isSeekable ()) {
83
+ $ stream ->seek (0 );
84
+ }
85
+
86
+ return $ request ->withBody ($ stream );
61
87
}
62
88
63
89
/**
@@ -94,8 +120,71 @@ protected function getHttpClient(): ClientInterface
94
120
return $ this ->client ;
95
121
}
96
122
123
+ /**
124
+ * @deprecated Use createRequest instead
125
+ */
97
126
protected function getMessageFactory (): MessageFactory
98
127
{
99
- return $ this ->messageFactory ;
128
+ $ factory = $ this ->messageFactory instanceof ResponseFactoryInterface ? $ this ->messageFactory : new Psr17Factory ();
129
+
130
+ return new class ($ factory ) implements MessageFactory {
131
+ private $ factory ;
132
+
133
+ public function __construct ($ factory )
134
+ {
135
+ $ this ->factory = $ factory ;
136
+ }
137
+
138
+ public function createRequest ($ method , $ uri , array $ headers = [], $ body = null , $ protocolVersion = '1.1 ' ): RequestInterface
139
+ {
140
+ $ request = $ this ->factory ->createRequest ($ method , $ uri );
141
+
142
+ foreach ($ headers as $ name => $ value ) {
143
+ $ request = $ request ->withAddedHeader ($ name , $ value );
144
+ }
145
+
146
+ if (null !== $ body ) {
147
+ $ request = $ request ->withBody ($ this ->createStream ($ body ));
148
+ }
149
+
150
+ return $ request ->withProtocolVersion ($ protocolVersion );
151
+ }
152
+
153
+ public function createResponse ($ statusCode = 200 , $ reasonPhrase = null , array $ headers = [], $ body = null , $ protocolVersion = '1.1 ' ): ResponseInterface
154
+ {
155
+ $ response = $ this ->factory ->createResponse ($ statusCode , $ reasonPhrase );
156
+
157
+ foreach ($ headers as $ name => $ value ) {
158
+ $ response = $ response ->withAddedHeader ($ name , $ value );
159
+ }
160
+
161
+ if (null !== $ body ) {
162
+ $ response = $ response ->withBody ($ this ->createStream ($ body ));
163
+ }
164
+
165
+ return $ response ->withProtocolVersion ($ protocolVersion );
166
+ }
167
+
168
+ private function createStream ($ content = '' ): StreamInterface
169
+ {
170
+ if ($ content instanceof StreamInterface) {
171
+ return $ content ;
172
+ }
173
+
174
+ if (\is_string ($ content ?? '' )) {
175
+ $ stream = $ this ->factory ->createStream ($ content ?? '' );
176
+ } elseif (\is_resource ($ content )) {
177
+ $ stream = $ this ->factory ->createStreamFromResource ($ content );
178
+ } else {
179
+ throw new \InvalidArgumentException (sprintf ('"%s()" expects string, resource or StreamInterface, "%s" given. ' , __METHOD__ , get_debug_type ($ content )));
180
+ }
181
+
182
+ if ($ stream ->isSeekable ()) {
183
+ $ stream ->seek (0 );
184
+ }
185
+
186
+ return $ stream ;
187
+ }
188
+ };
100
189
}
101
190
}
0 commit comments