15
15
namespace League \OAuth2 \Client \Provider ;
16
16
17
17
use GuzzleHttp \Client as HttpClient ;
18
- use GuzzleHttp \ClientInterface as HttpClientInterface ;
19
18
use GuzzleHttp \Exception \BadResponseException ;
20
19
use InvalidArgumentException ;
21
20
use League \OAuth2 \Client \Grant \AbstractGrant ;
28
27
use League \OAuth2 \Client \Tool \ArrayAccessorTrait ;
29
28
use League \OAuth2 \Client \Tool \GuardedPropertyTrait ;
30
29
use League \OAuth2 \Client \Tool \QueryBuilderTrait ;
31
- use League \OAuth2 \Client \Tool \RequestFactory ;
30
+ use Psr \Http \Client \ClientInterface ;
31
+ use Psr \Http \Message \RequestFactoryInterface ;
32
32
use Psr \Http \Message \RequestInterface ;
33
33
use Psr \Http \Message \ResponseInterface ;
34
+ use Psr \Http \Message \StreamFactoryInterface ;
34
35
use UnexpectedValueException ;
35
36
36
37
/**
@@ -103,12 +104,17 @@ abstract class AbstractProvider
103
104
protected $ grantFactory ;
104
105
105
106
/**
106
- * @var RequestFactory
107
+ * @var RequestFactoryInterface
107
108
*/
108
109
protected $ requestFactory ;
109
110
110
111
/**
111
- * @var HttpClientInterface
112
+ * @var StreamFactoryInterface
113
+ */
114
+ protected $ streamFactory ;
115
+
116
+ /**
117
+ * @var ClientInterface
112
118
*/
113
119
protected $ httpClient ;
114
120
@@ -140,16 +146,17 @@ public function __construct(array $options = [], array $collaborators = [])
140
146
$ this ->setGrantFactory ($ collaborators ['grantFactory ' ]);
141
147
142
148
if (empty ($ collaborators ['requestFactory ' ])) {
143
- $ collaborators [ ' requestFactory ' ] = new RequestFactory ( );
149
+ throw new InvalidArgumentException ( ' No request factory set ' );
144
150
}
145
151
$ this ->setRequestFactory ($ collaborators ['requestFactory ' ]);
146
152
147
- if (empty ($ collaborators ['httpClient ' ])) {
148
- $ client_options = $ this ->getAllowedClientOptions ($ options );
153
+ if (empty ($ collaborators ['streamFactory ' ])) {
154
+ throw new InvalidArgumentException ('No stream factory set ' );
155
+ }
156
+ $ this ->setStreamFactory ($ collaborators ['streamFactory ' ]);
149
157
150
- $ collaborators ['httpClient ' ] = new HttpClient (
151
- array_intersect_key ($ options , array_flip ($ client_options ))
152
- );
158
+ if (empty ($ collaborators ['httpClient ' ])) {
159
+ throw new InvalidArgumentException ('No http client set ' );
153
160
}
154
161
$ this ->setHttpClient ($ collaborators ['httpClient ' ]);
155
162
@@ -205,10 +212,10 @@ public function getGrantFactory()
205
212
/**
206
213
* Sets the request factory instance.
207
214
*
208
- * @param RequestFactory $factory
215
+ * @param RequestFactoryInterface $factory
209
216
* @return self
210
217
*/
211
- public function setRequestFactory (RequestFactory $ factory )
218
+ public function setRequestFactory (RequestFactoryInterface $ factory )
212
219
{
213
220
$ this ->requestFactory = $ factory ;
214
221
@@ -218,20 +225,43 @@ public function setRequestFactory(RequestFactory $factory)
218
225
/**
219
226
* Returns the request factory instance.
220
227
*
221
- * @return RequestFactory
228
+ * @return RequestFactoryInterface
222
229
*/
223
230
public function getRequestFactory ()
224
231
{
225
232
return $ this ->requestFactory ;
226
233
}
227
234
235
+ /**
236
+ * Sets the stream factory instance.
237
+ *
238
+ * @param StreamFactoryInterface $factory
239
+ * @return self
240
+ */
241
+ public function setStreamFactory (StreamFactoryInterface $ factory )
242
+ {
243
+ $ this ->streamFactory = $ factory ;
244
+
245
+ return $ this ;
246
+ }
247
+
248
+ /**
249
+ * Returns the stream factory instance.
250
+ *
251
+ * @return StreamFactoryInterface
252
+ */
253
+ public function getStreamFactory ()
254
+ {
255
+ return $ this ->streamFactory ;
256
+ }
257
+
228
258
/**
229
259
* Sets the HTTP client instance.
230
260
*
231
- * @param HttpClientInterface $client
261
+ * @param ClientInterface $client
232
262
* @return self
233
263
*/
234
- public function setHttpClient (HttpClientInterface $ client )
264
+ public function setHttpClient (ClientInterface $ client )
235
265
{
236
266
$ this ->httpClient = $ client ;
237
267
@@ -241,7 +271,7 @@ public function setHttpClient(HttpClientInterface $client)
241
271
/**
242
272
* Returns the HTTP client instance.
243
273
*
244
- * @return HttpClientInterface
274
+ * @return ClientInterface
245
275
*/
246
276
public function getHttpClient ()
247
277
{
@@ -696,9 +726,23 @@ protected function createRequest($method, $url, $token, array $options)
696
726
];
697
727
698
728
$ options = array_merge_recursive ($ defaults , $ options );
699
- $ factory = $ this ->getRequestFactory ();
729
+ $ requestFactory = $ this ->getRequestFactory ();
730
+ $ streamFactory = $ this ->getStreamFactory ();
731
+
732
+ $ request = $ requestFactory ->createRequest ($ method , $ url );
733
+ foreach ($ options ['headers ' ] as $ name => $ value ) {
734
+ $ request = $ request ->withAddedHeader ($ name , $ value );
735
+ }
736
+
737
+ $ request = $ request ->withProtocolVersion ($ options ['version ' ] ?? '1.1 ' );
738
+
739
+ if (!empty ($ options ['body ' ])) {
740
+ $ request = $ request ->withBody (
741
+ $ streamFactory ->createStream ($ options ['body ' ] ?? null )
742
+ );
743
+ }
700
744
701
- return $ factory -> getRequestWithOptions ( $ method , $ url , $ options ) ;
745
+ return $ request ;
702
746
}
703
747
704
748
/**
@@ -712,7 +756,7 @@ protected function createRequest($method, $url, $token, array $options)
712
756
*/
713
757
public function getResponse (RequestInterface $ request )
714
758
{
715
- return $ this ->getHttpClient ()->send ($ request );
759
+ return $ this ->getHttpClient ()->sendRequest ($ request );
716
760
}
717
761
718
762
/**
0 commit comments