Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Guzzle with generic PSR18 discovery #99

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
php-versions: ['7.4', '8.0', '8.1', '8.2']
php-versions: ['8.1', '8.2']

name: PHP ${{ matrix.php-versions }}

Expand Down
17 changes: 13 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@
}
],
"require": {
"php": "^7.4 || ^8.0",
"guzzlehttp/guzzle": "^6.0 || ^7.0",
"php": "^8.1",
"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": {
Expand All @@ -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
}
}
}
55 changes: 34 additions & 21 deletions src/Packagist/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,7 +24,7 @@
*/
class Client
{
protected ClientInterface $httpClient;
protected HttpMethodsClientInterface $httpClient;

protected Factory $resultFactory;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch currently still supports PHP 7.4, which doesn't let us use union types. Since PHP 7.4 is EOL, I think we can bump to ^8.1 and keep these types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I've made this change

{
return $this->httpClient
->request('POST', $url, $options)
->post($url, $headers, $body)
->getBody()
->getContents();
}
Expand All @@ -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);
}
Expand Down
Loading