Skip to content

Commit 9f19946

Browse files
committed
Replace Guzzle with generic PSR18 discovery
1 parent 4634b39 commit 9f19946

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

composer.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@
1313
],
1414
"require": {
1515
"php": "^7.4 || ^8.0",
16-
"guzzlehttp/guzzle": "^6.0 || ^7.0",
1716
"doctrine/inflector": "^1.0 || ^2.0",
1817
"ext-json": "*",
1918
"composer/metadata-minifier": "^1.0",
20-
"composer/semver": "^1.0|^2.0|^3.0"
19+
"composer/semver": "^1.0|^2.0|^3.0",
20+
"php-http/discovery": "^1.12",
21+
"php-http/client-common": "^2.3",
22+
"psr/http-client-implementation": "^1.0",
23+
"psr/http-factory-implementation": "^1.0"
2124
},
2225
"require-dev": {
2326
"phpspec/phpspec": "^6.0 || ^7.0",
24-
"squizlabs/php_codesniffer": "^3.0"
27+
"squizlabs/php_codesniffer": "^3.0",
28+
"guzzlehttp/guzzle": "^6.0 || ^7.0"
2529
},
2630
"autoload": {
2731
"psr-4": {
@@ -41,5 +45,10 @@
4145
"scripts": {
4246
"lint": "vendor/bin/phpcs --standard=PSR12 src/",
4347
"test": "vendor/bin/phpspec run -f pretty"
48+
},
49+
"config": {
50+
"allow-plugins": {
51+
"php-http/discovery": true
52+
}
4453
}
4554
}

src/Packagist/Api/Client.php

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
namespace Packagist\Api;
66

77
use Composer\Semver\Semver;
8-
use GuzzleHttp\Client as HttpClient;
9-
use GuzzleHttp\ClientInterface;
10-
use GuzzleHttp\Exception\GuzzleException;
8+
use Exception;
9+
use Http\Client\Common\HttpMethodsClient;
10+
use Http\Client\Common\HttpMethodsClientInterface;
11+
use Http\Client\Common\PluginClientFactory;
12+
use Http\Discovery\Psr17FactoryDiscovery;
13+
use Http\Discovery\Psr18ClientDiscovery;
1114
use Packagist\Api\Result\Advisory;
1215
use Packagist\Api\Result\Factory;
1316
use Packagist\Api\Result\Package;
17+
use Psr\Http\Client\ClientInterface;
18+
use Psr\Http\Message\StreamInterface;
1419

1520
/**
1621
* Packagist Api
@@ -19,7 +24,7 @@
1924
*/
2025
class Client
2126
{
22-
protected ClientInterface $httpClient;
27+
protected HttpMethodsClientInterface $httpClient;
2328

2429
protected Factory $resultFactory;
2530

@@ -36,14 +41,18 @@ public function __construct(
3641
string $packagistUrl = 'https://packagist.org'
3742
) {
3843
if (null === $httpClient) {
39-
$httpClient = new HttpClient();
44+
$httpClient = (new PluginClientFactory())->createClient(Psr18ClientDiscovery::find());
4045
}
4146

4247
if (null === $resultFactory) {
4348
$resultFactory = new Factory();
4449
}
4550

46-
$this->httpClient = $httpClient;
51+
$this->httpClient = new HttpMethodsClient(
52+
$httpClient,
53+
Psr17FactoryDiscovery::findRequestFactory(),
54+
Psr17FactoryDiscovery::findStreamFactory()
55+
);
4756
$this->resultFactory = $resultFactory;
4857
$this->packagistUrl = $packagistUrl;
4958
}
@@ -222,9 +231,11 @@ public function advisories(array $packages = [], ?int $updatedSince = null, bool
222231
if ($updatedSince !== null) {
223232
$query['updatedSince'] = $updatedSince;
224233
}
225-
$options = [
234+
$queryString = http_build_query([
226235
'query' => array_filter($query),
227-
];
236+
]);
237+
$headers = [];
238+
$body = null;
228239

229240
// Add packages if appropriate
230241
if (count($packages) > 0) {
@@ -235,13 +246,13 @@ public function advisories(array $packages = [], ?int $updatedSince = null, bool
235246
}
236247
$content['packages'][] = $package;
237248
}
238-
$options['headers']['Content-type'] = 'application/x-www-form-urlencoded';
239-
$options['body'] = http_build_query($content);
249+
$headers['Content-type'] = 'application/x-www-form-urlencoded';
250+
$body = http_build_query($content);
240251
}
241252

242253
// Get advisories from API
243254
/** @var Advisory[] $advisories */
244-
$advisories = $this->respondPost($this->url('/api/security-advisories/'), $options);
255+
$advisories = $this->respondPost($this->url('/api/security-advisories/?' . $queryString), $headers, $body);
245256

246257
// Filter advisories if necessary
247258
if (count($advisories) > 0 && $filterByVersion) {
@@ -308,12 +319,13 @@ protected function respond(string $url)
308319
* Execute the POST request and parse the response
309320
*
310321
* @param string $url
311-
* @param array $option
322+
* @param array $headers
323+
* @param string|StreamInterface|null $body
312324
* @return array|Package
313325
*/
314-
protected function respondPost(string $url, array $options)
326+
protected function respondPost(string $url, array $headers = [], string|StreamInterface|null $body = null)
315327
{
316-
$response = $this->postRequest($url, $options);
328+
$response = $this->postRequest($url, $headers, $body);
317329
$response = $this->parse($response);
318330

319331
return $this->create($response);
@@ -352,14 +364,15 @@ protected function multiRespond(string $url1, string $url2)
352364
* Execute the POST request
353365
*
354366
* @param string $url
355-
* @param array $options
367+
* @param array $headers
368+
* @param string|StreamInterface|null $body
356369
* @return string
357-
* @throws GuzzleException
370+
* @throws Exception
358371
*/
359-
protected function postRequest(string $url, array $options): string
372+
protected function postRequest(string $url, array $headers = [], string|StreamInterface|null $body = null): string
360373
{
361374
return $this->httpClient
362-
->request('POST', $url, $options)
375+
->post($url, $headers, $body)
363376
->getBody()
364377
->getContents();
365378
}
@@ -370,16 +383,16 @@ protected function postRequest(string $url, array $options): string
370383
* @param string $url
371384
* @return string
372385
* @throws PackageNotFoundException
373-
* @throws GuzzleException
386+
* @throws Exception
374387
*/
375388
protected function request(string $url): string
376389
{
377390
try {
378391
return $this->httpClient
379-
->request('GET', $url)
392+
->get($url)
380393
->getBody()
381394
->getContents();
382-
} catch (GuzzleException $e) {
395+
} catch (Exception $e) {
383396
if ($e->getCode() === 404) {
384397
throw new PackageNotFoundException('The requested package was not found.', 404);
385398
}

0 commit comments

Comments
 (0)