diff --git a/composer.json b/composer.json index bb87705..ca5eb36 100644 --- a/composer.json +++ b/composer.json @@ -13,15 +13,19 @@ ], "require": { "php": "^7.4 || ^8.0", - "guzzlehttp/guzzle": "^6.0 || ^7.0", "doctrine/inflector": "^1.0 || ^2.0", "ext-json": "*", "composer/metadata-minifier": "^1.0", - "composer/semver": "^1.0|^2.0|^3.0" + "composer/semver": "^1.0|^2.0|^3.0", + "php-http/discovery": "^1.12", + "php-http/client-common": "^2.3", + "psr/http-client-implementation": "^1.0", + "psr/http-factory-implementation": "^1.0" }, "require-dev": { "phpspec/phpspec": "^6.0 || ^7.0", - "squizlabs/php_codesniffer": "^3.0" + "squizlabs/php_codesniffer": "^3.0", + "guzzlehttp/guzzle": "^6.0 || ^7.0" }, "autoload": { "psr-4": { @@ -41,5 +45,10 @@ "scripts": { "lint": "vendor/bin/phpcs --standard=PSR12 src/", "test": "vendor/bin/phpspec run -f pretty" + }, + "config": { + "allow-plugins": { + "php-http/discovery": true + } } } diff --git a/src/Packagist/Api/Client.php b/src/Packagist/Api/Client.php index 79b046b..aefd35e 100644 --- a/src/Packagist/Api/Client.php +++ b/src/Packagist/Api/Client.php @@ -5,12 +5,17 @@ namespace Packagist\Api; use Composer\Semver\Semver; -use GuzzleHttp\Client as HttpClient; -use GuzzleHttp\ClientInterface; -use GuzzleHttp\Exception\GuzzleException; +use Exception; +use Http\Client\Common\HttpMethodsClient; +use Http\Client\Common\HttpMethodsClientInterface; +use Http\Client\Common\PluginClientFactory; +use Http\Discovery\Psr17FactoryDiscovery; +use Http\Discovery\Psr18ClientDiscovery; use Packagist\Api\Result\Advisory; use Packagist\Api\Result\Factory; use Packagist\Api\Result\Package; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\StreamInterface; /** * Packagist Api @@ -19,7 +24,7 @@ */ class Client { - protected ClientInterface $httpClient; + protected HttpMethodsClientInterface $httpClient; protected Factory $resultFactory; @@ -36,14 +41,18 @@ public function __construct( string $packagistUrl = 'https://packagist.org' ) { if (null === $httpClient) { - $httpClient = new HttpClient(); + $httpClient = (new PluginClientFactory())->createClient(Psr18ClientDiscovery::find()); } if (null === $resultFactory) { $resultFactory = new Factory(); } - $this->httpClient = $httpClient; + $this->httpClient = new HttpMethodsClient( + $httpClient, + Psr17FactoryDiscovery::findRequestFactory(), + Psr17FactoryDiscovery::findStreamFactory() + ); $this->resultFactory = $resultFactory; $this->packagistUrl = $packagistUrl; } @@ -222,9 +231,11 @@ public function advisories(array $packages = [], ?int $updatedSince = null, bool if ($updatedSince !== null) { $query['updatedSince'] = $updatedSince; } - $options = [ + $queryString = http_build_query([ 'query' => array_filter($query), - ]; + ]); + $headers = []; + $body = null; // Add packages if appropriate if (count($packages) > 0) { @@ -235,13 +246,13 @@ public function advisories(array $packages = [], ?int $updatedSince = null, bool } $content['packages'][] = $package; } - $options['headers']['Content-type'] = 'application/x-www-form-urlencoded'; - $options['body'] = http_build_query($content); + $headers['Content-type'] = 'application/x-www-form-urlencoded'; + $body = http_build_query($content); } // Get advisories from API /** @var Advisory[] $advisories */ - $advisories = $this->respondPost($this->url('/api/security-advisories/'), $options); + $advisories = $this->respondPost($this->url('/api/security-advisories/?' . $queryString), $headers, $body); // Filter advisories if necessary if (count($advisories) > 0 && $filterByVersion) { @@ -308,12 +319,13 @@ protected function respond(string $url) * Execute the POST request and parse the response * * @param string $url - * @param array $option + * @param array $headers + * @param string|StreamInterface|null $body * @return array|Package */ - protected function respondPost(string $url, array $options) + protected function respondPost(string $url, array $headers = [], string|StreamInterface|null $body = null) { - $response = $this->postRequest($url, $options); + $response = $this->postRequest($url, $headers, $body); $response = $this->parse($response); return $this->create($response); @@ -352,14 +364,15 @@ protected function multiRespond(string $url1, string $url2) * Execute the POST request * * @param string $url - * @param array $options + * @param array $headers + * @param string|StreamInterface|null $body * @return string - * @throws GuzzleException + * @throws Exception */ - protected function postRequest(string $url, array $options): string + protected function postRequest(string $url, array $headers = [], string|StreamInterface|null $body = null): string { return $this->httpClient - ->request('POST', $url, $options) + ->post($url, $headers, $body) ->getBody() ->getContents(); } @@ -370,16 +383,16 @@ protected function postRequest(string $url, array $options): string * @param string $url * @return string * @throws PackageNotFoundException - * @throws GuzzleException + * @throws Exception */ protected function request(string $url): string { try { return $this->httpClient - ->request('GET', $url) + ->get($url) ->getBody() ->getContents(); - } catch (GuzzleException $e) { + } catch (Exception $e) { if ($e->getCode() === 404) { throw new PackageNotFoundException('The requested package was not found.', 404); }