Skip to content

Commit 19d2064

Browse files
committed
Replace GuzzleHttp with psr/http-client and psr/http-factory
1 parent 3d5cf8d commit 19d2064

File tree

8 files changed

+195
-236
lines changed

8 files changed

+195
-236
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
"require": {
99
"php": "^7.1 || >=8.0.0 <8.5.0",
1010
"ext-json": "*",
11-
"guzzlehttp/guzzle": "^6.5.8 || ^7.4.5"
11+
"psr/http-client": "^1.0",
12+
"psr/http-factory": "^1.1",
13+
"psr/http-message": "^2.0"
1214
},
1315
"require-dev": {
16+
"guzzlehttp/guzzle": "^6.5.8 || ^7.4.5"
1417
"mockery/mockery": "^1.3.5",
1518
"php-parallel-lint/php-parallel-lint": "^1.4",
1619
"phpunit/phpunit": "^7 || ^8 || ^9 || ^10 || ^11",

src/Provider/AbstractProvider.php

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
namespace League\OAuth2\Client\Provider;
1616

1717
use GuzzleHttp\Client as HttpClient;
18-
use GuzzleHttp\ClientInterface as HttpClientInterface;
1918
use GuzzleHttp\Exception\BadResponseException;
2019
use InvalidArgumentException;
2120
use League\OAuth2\Client\Grant\AbstractGrant;
@@ -28,9 +27,11 @@
2827
use League\OAuth2\Client\Tool\ArrayAccessorTrait;
2928
use League\OAuth2\Client\Tool\GuardedPropertyTrait;
3029
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;
3232
use Psr\Http\Message\RequestInterface;
3333
use Psr\Http\Message\ResponseInterface;
34+
use Psr\Http\Message\StreamFactoryInterface;
3435
use UnexpectedValueException;
3536

3637
/**
@@ -103,12 +104,17 @@ abstract class AbstractProvider
103104
protected $grantFactory;
104105

105106
/**
106-
* @var RequestFactory
107+
* @var RequestFactoryInterface
107108
*/
108109
protected $requestFactory;
109110

110111
/**
111-
* @var HttpClientInterface
112+
* @var StreamFactoryInterface
113+
*/
114+
protected $streamFactory;
115+
116+
/**
117+
* @var ClientInterface
112118
*/
113119
protected $httpClient;
114120

@@ -140,16 +146,17 @@ public function __construct(array $options = [], array $collaborators = [])
140146
$this->setGrantFactory($collaborators['grantFactory']);
141147

142148
if (empty($collaborators['requestFactory'])) {
143-
$collaborators['requestFactory'] = new RequestFactory();
149+
throw new InvalidArgumentException('No request factory set');
144150
}
145151
$this->setRequestFactory($collaborators['requestFactory']);
146152

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']);
149157

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');
153160
}
154161
$this->setHttpClient($collaborators['httpClient']);
155162

@@ -205,10 +212,10 @@ public function getGrantFactory()
205212
/**
206213
* Sets the request factory instance.
207214
*
208-
* @param RequestFactory $factory
215+
* @param RequestFactoryInterface $factory
209216
* @return self
210217
*/
211-
public function setRequestFactory(RequestFactory $factory)
218+
public function setRequestFactory(RequestFactoryInterface $factory)
212219
{
213220
$this->requestFactory = $factory;
214221

@@ -218,20 +225,43 @@ public function setRequestFactory(RequestFactory $factory)
218225
/**
219226
* Returns the request factory instance.
220227
*
221-
* @return RequestFactory
228+
* @return RequestFactoryInterface
222229
*/
223230
public function getRequestFactory()
224231
{
225232
return $this->requestFactory;
226233
}
227234

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+
228258
/**
229259
* Sets the HTTP client instance.
230260
*
231-
* @param HttpClientInterface $client
261+
* @param ClientInterface $client
232262
* @return self
233263
*/
234-
public function setHttpClient(HttpClientInterface $client)
264+
public function setHttpClient(ClientInterface $client)
235265
{
236266
$this->httpClient = $client;
237267

@@ -241,7 +271,7 @@ public function setHttpClient(HttpClientInterface $client)
241271
/**
242272
* Returns the HTTP client instance.
243273
*
244-
* @return HttpClientInterface
274+
* @return ClientInterface
245275
*/
246276
public function getHttpClient()
247277
{
@@ -696,9 +726,23 @@ protected function createRequest($method, $url, $token, array $options)
696726
];
697727

698728
$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+
}
700744

701-
return $factory->getRequestWithOptions($method, $url, $options);
745+
return $request;
702746
}
703747

704748
/**
@@ -712,7 +756,7 @@ protected function createRequest($method, $url, $token, array $options)
712756
*/
713757
public function getResponse(RequestInterface $request)
714758
{
715-
return $this->getHttpClient()->send($request);
759+
return $this->getHttpClient()->sendRequest($request);
716760
}
717761

718762
/**

src/Tool/RequestFactory.php

Lines changed: 0 additions & 87 deletions
This file was deleted.

test/src/Grant/GrantTestCase.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace League\OAuth2\Client\Test\Grant;
44

5-
use GuzzleHttp\ClientInterface;
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Psr7\HttpFactory;
67
use Mockery;
78
use Mockery\MockInterface;
89
use PHPUnit\Framework\Attributes\DataProvider;
910
use PHPUnit\Framework\TestCase;
11+
use Psr\Http\Client\ClientInterface;
1012
use Psr\Http\Message\ResponseInterface;
1113
use Psr\Http\Message\StreamInterface;
1214
use League\OAuth2\Client\Token\AccessTokenInterface;
@@ -20,6 +22,10 @@ protected function getMockProvider()
2022
'clientId' => 'mock_client_id',
2123
'clientSecret' => 'mock_secret',
2224
'redirectUri' => 'none',
25+
],[
26+
'httpClient' => new Client(),
27+
'requestFactory' => new HttpFactory(),
28+
'streamFactory' => new HttpFactory()
2329
]);
2430
}
2531

@@ -72,7 +78,7 @@ public function testGetAccessToken($grant, array $params = [])
7278
/** @var ClientInterface & MockInterface $client */
7379
$client = Mockery::spy(ClientInterface::class)->makePartial();
7480
$client
75-
->shouldReceive('send')
81+
->shouldReceive('sendRequest')
7682
->once()
7783
->withArgs(function ($request) {
7884
parse_str((string) $request->getBody(), $body);

0 commit comments

Comments
 (0)