diff --git a/src/Package_Command.php b/src/Package_Command.php index 82fc3508..32a6284f 100644 --- a/src/Package_Command.php +++ b/src/Package_Command.php @@ -834,7 +834,7 @@ private function get_package_by_shortened_identifier( $package_name, $insecure = $options = [ 'insecure' => $insecure ]; // Check if the package exists on Packagist. - $url = "https://repo.packagist.org/p/{$package_name}.json"; + $url = $this->get_packagist_url( $package_name, $options ); $response = Utils\http_request( 'GET', $url, null, [], $options ); if ( 20 === (int) substr( $response->status_code, 0, 2 ) ) { return $package_name; @@ -862,6 +862,20 @@ private function get_package_by_shortened_identifier( $package_name, $insecure = return false; } + /** + * Gets the correct packagist URL. + */ + private function get_packagist_url( $package_name, $options ) { + $url = "https://repo.packagist.org/p2/{$package_name}.json"; + $response = Utils\http_request( 'GET', $url, null, [], $options ); + + if ( 20 === (int) substr( $response->status_code, 0, 2 ) ) { + return $url; + } + + return "https://repo.packagist.org/p/{$package_name}.json"; + } + /** * Gets the installed community packages. */ diff --git a/tests/ComposerJsonTest.php b/tests/ComposerJsonTest.php index 82ef7d46..697a5174 100644 --- a/tests/ComposerJsonTest.php +++ b/tests/ComposerJsonTest.php @@ -180,6 +180,25 @@ public function test_get_composer_json_path_backup_decoded() { putenv( false === $env_wp_cli_packages_dir ? 'WP_CLI_PACKAGES_DIR' : "WP_CLI_PACKAGES_DIR=$env_wp_cli_packages_dir" ); } + public function test_get_packagist_url() { + $get_packagist_url = new \ReflectionMethod( 'Package_Command', 'get_packagist_url' ); + $get_packagist_url->setAccessible( true ); + + $package = new Package_Command(); + $options = [ 'insecure' => false ]; + + // Test with a valid package that should exist on packagist v1 + $result = $get_packagist_url->invoke( $package, 'wp-cli/wp-cli', $options ); + $this->assertStringContainsString( 'repo.packagist.org/p/', $result ); + $this->assertStringContainsString( 'wp-cli/wp-cli.json', $result ); + + // The method should return either v1 or v2 URL based on availability + $this->assertTrue( + strpos( $result, 'https://repo.packagist.org/p/wp-cli/wp-cli.json' ) === 0 || + strpos( $result, 'https://repo.packagist.org/p2/wp-cli/wp-cli.json' ) === 0 + ); + } + private function mac_safe_path( $path ) { return preg_replace( '#^/private/(var|tmp)/#i', '/$1/', $path ); }