From 36235f45dfa1ede99a3d6430e23625993f551704 Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Sun, 31 May 2026 10:06:31 +0200 Subject: [PATCH] adhere to packagist.org best practice --- src/Command/SyncPackagesCommand.php | 26 +++++++++++++--- .../Command/SyncPackagesCommandTest.php | 30 +++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/Command/SyncPackagesCommand.php b/src/Command/SyncPackagesCommand.php index 207948b8..c50ab432 100644 --- a/src/Command/SyncPackagesCommand.php +++ b/src/Command/SyncPackagesCommand.php @@ -11,7 +11,8 @@ use Cake\Log\Log; use Composer\Semver\Intervals; use Composer\Semver\VersionParser; -use Packagist\Api\Client; +use GuzzleHttp\Client as HttpClient; +use Packagist\Api\Client as PackagistClient; use Packagist\Api\Result\Package\Version; use UnexpectedValueException; @@ -47,7 +48,7 @@ class SyncPackagesCommand extends Command '5' => [0, 1, 2, 3], ]; - private readonly Client $client; + private readonly PackagistClient $client; /** * The name of this command. @@ -71,7 +72,7 @@ public static function defaultName(): string */ public static function getDescription(): string { - return 'Command description here.'; + return 'Sync all packages from packagist.org marked cakephp-plugin'; } /** @@ -81,7 +82,23 @@ public function __construct( ?CommandFactoryInterface $factory = null, ) { parent::__construct($factory); - $this->client = new Client(); + $this->client = new PackagistClient($this->createPackagistHttpClient()); + } + + /** + * @return \GuzzleHttp\Client + */ + private function createPackagistHttpClient(): HttpClient + { + return new HttpClient([ + 'headers' => [ + 'User-Agent' => env( + 'PACKAGIST_USER_AGENT', + 'plugins.cakephp.org (https://plugins.cakephp.org; mailto=security@cakephp.org)', + ), + ], + 'version' => 2.0, + ]); } /** @@ -106,6 +123,7 @@ public function execute(Arguments $args, ConsoleIo $io): void $failed = 0; $i = 0; + /** @var \Cake\Command\Helper\ProgressHelper $progress */ $progress = $io->helper('Progress'); $progress->init(['total' => $total, 'width' => 60]); $io->out('', 0); diff --git a/tests/TestCase/Command/SyncPackagesCommandTest.php b/tests/TestCase/Command/SyncPackagesCommandTest.php index 8202509c..ef1a76da 100644 --- a/tests/TestCase/Command/SyncPackagesCommandTest.php +++ b/tests/TestCase/Command/SyncPackagesCommandTest.php @@ -7,6 +7,7 @@ use Cake\Console\TestSuite\ConsoleIntegrationTestTrait; use Cake\I18n\Date; use Cake\TestSuite\TestCase; +use GuzzleHttp\Client; use Packagist\Api\Result\Package\Version; use ReflectionMethod; @@ -48,6 +49,35 @@ public function testExtractReleaseDate(): void $this->assertNull($method->invoke($command, null)); } + /** + * @return void + */ + public function testCreatePackagistHttpClientUsesApiBestPractices(): void + { + $userAgent = 'plugins.cakephp.org-test (mailto=test@example.com)'; + $previousUserAgent = $_ENV['PACKAGIST_USER_AGENT'] ?? null; + $_ENV['PACKAGIST_USER_AGENT'] = $userAgent; + + try { + $command = new SyncPackagesCommand(); + $method = new ReflectionMethod($command, 'createPackagistHttpClient'); + + /** @var \GuzzleHttp\Client $client */ + $client = $method->invoke($command); + } finally { + if ($previousUserAgent === null) { + unset($_ENV['PACKAGIST_USER_AGENT']); + } else { + $_ENV['PACKAGIST_USER_AGENT'] = $previousUserAgent; + } + } + + $this->assertInstanceOf(Client::class, $client); + $this->assertSame(2.0, $client->getConfig('version')); + $this->assertSame($userAgent, $client->getConfig('headers')['User-Agent']); + $this->assertStringContainsString('mailto=', $client->getConfig('headers')['User-Agent']); + } + /** * Test defaultName method *