Skip to content

Commit 122583d

Browse files
committed
Client: require composer 2 and extract api client from InstalledVersions
1 parent 1530ed8 commit 122583d

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"php-http/httplug": "^1.1 || ^2.0",
1717
"php-http/discovery": "^1.0",
1818
"php-http/client-implementation": "^1.0",
19-
"php-http/client-common": "^1.9 || ^2.0"
19+
"php-http/client-common": "^1.9 || ^2.0",
20+
"composer-runtime-api": "^2.0"
2021
},
2122
"require-dev": {
2223
"phpunit/phpunit": "^5.5 || ^7.0",

src/Client.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
class Client
2121
{
22-
const API_CLIENT_VERSION = '1.21.0';
23-
2422
/** @var HttpPluginClientBuilder */
2523
private $httpClientBuilder;
2624
/** @var ResponseMediator */
@@ -31,15 +29,18 @@ public function __construct(HttpPluginClientBuilder $httpClientBuilder = null, $
3129
{
3230
$this->httpClientBuilder = $builder = $httpClientBuilder ?: new HttpPluginClientBuilder();
3331
$privatePackagistUrl = $privatePackagistUrl ? : 'https://packagist.com';
34-
$this->responseMediator = $responseMediator ? $responseMediator : new ResponseMediator();
32+
$this->responseMediator = $responseMediator ? : new ResponseMediator();
3533

3634
$builder->addPlugin(new Plugin\AddHostPlugin(UriFactoryDiscovery::find()->createUri($privatePackagistUrl)));
3735
$builder->addPlugin(new PathPrepend('/api'));
3836
$builder->addPlugin(new Plugin\RedirectPlugin());
39-
$builder->addPlugin(new Plugin\HeaderDefaultsPlugin([
37+
$headers = [
4038
'User-Agent' => 'php-private-packagist-api (https://github.com/packagist/private-packagist-api-client)',
41-
'X-API-CLIENT-VERSION' => self::API_CLIENT_VERSION,
42-
]));
39+
];
40+
if ($apiClientVersion = $this->getApiClientVersion()) {
41+
$headers['API-CLIENT-VERSION'] = $apiClientVersion;
42+
}
43+
$builder->addPlugin(new Plugin\HeaderDefaultsPlugin($headers));
4344
$builder->addPlugin(new ExceptionThrower($this->responseMediator));
4445
}
4546

@@ -121,4 +122,17 @@ protected function getHttpClientBuilder()
121122
{
122123
return $this->httpClientBuilder;
123124
}
125+
126+
private function getApiClientVersion()
127+
{
128+
try {
129+
if ($version = \Composer\InstalledVersions::getVersion('private-packagist/api-client')) {
130+
return $version;
131+
}
132+
} catch (\OutOfBoundsException $exception) {
133+
return null;
134+
}
135+
136+
return null;
137+
}
124138
}

0 commit comments

Comments
 (0)