Skip to content

Commit d1c383f

Browse files
authored
Merge pull request #38 from huangzhhui/phpunit-latest
Support for fetch versions from packagist.org
2 parents 9d571ce + 6d13b54 commit d1c383f

4 files changed

Lines changed: 71 additions & 8 deletions

File tree

pkgs.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
},
66
"phpunit": {
77
"url": "https://phar.phpunit.de/phpunit-${{version}}.phar",
8-
"latest": "9",
9-
"bin": "phpunit.phar"
8+
"bin": "phpunit.phar",
9+
"composer_name": "phpunit/phpunit",
10+
"latest_fetch_type": "packagist"
1011
},
1112
"php": {
1213
"repo": "dixyes/lwmbs",

src/app/DownloadHandler/AbstractDownloadHandler.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,28 @@ protected function fetchVersionsFromGithubRelease(string $fullRepo, ?string $ass
9292
return $versions;
9393
}
9494

95+
protected function fetchVersionsFromPackagist(string $pkgName, string $composerName): array
96+
{
97+
$client = new Client();
98+
$response = $client->get(sprintf('https://repo.packagist.org/p2/%s.json', $composerName));
99+
$body = json_decode($response->getBody()->getContents(), true);
100+
if (! isset($body['packages'][$composerName])) {
101+
throw new NotSupportVersionsException($pkgName);
102+
}
103+
$composerJsons = $body['packages'][$composerName];
104+
$versions = [];
105+
foreach ($composerJsons as $composerJson) {
106+
if (isset($composerJson['version'])) {
107+
$versions[] = $composerJson['version'];
108+
}
109+
}
110+
// Sort versions by desc
111+
usort($versions, function ($a, $b) {
112+
return version_compare($b, $a);
113+
});
114+
return $versions;
115+
}
116+
95117
protected function download(
96118
?string $url,
97119
string $savePath,

src/app/DownloadHandler/DefaultHandler.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212

1313
namespace App\DownloadHandler;
1414

15+
use App\Exception\BoxException;
1516
use App\Exception\NotSupportVersionsException;
17+
use App\PkgDefinition\Definition;
18+
use GuzzleHttp\Client;
1619
use SplFileInfo;
1720

1821
class DefaultHandler extends AbstractDownloadHandler
@@ -26,10 +29,17 @@ public function handle(string $pkgName, string $version, array $options = []): ?
2629
if ($definition->getRepo()) {
2730
$url = $this->fetchDownloadUrlFromGithubRelease($definition->getBin(), $definition->getRepo(), $version);
2831
} elseif ($definition->getUrl()) {
29-
if ($version === 'latest' && $definition->getLatest()) {
30-
$version = $definition->getLatest();
32+
if ($version === 'latest') {
33+
if ($definition->getLatest() && $definition->getLatest() !== 'latest') {
34+
$specifiedVersion = $definition->getLatest();
35+
} else {
36+
$versions = $this->versions($pkgName);
37+
$specifiedVersion = array_shift($versions);
38+
}
39+
} else {
40+
$specifiedVersion = $version;
3141
}
32-
$url = str_replace('${{version}}', $version, $definition->getUrl());
42+
$url = str_replace('${{version}}', $specifiedVersion, $definition->getUrl());
3343
} else {
3444
throw new \RuntimeException('The definition of package is invalid');
3545
}
@@ -42,9 +52,15 @@ public function versions(string $pkgName, array $options = []): array
4252
if (! $definition) {
4353
throw new \RuntimeException('The package not found');
4454
}
45-
if (! $definition->getRepo()) {
55+
if (! $definition->getRepo() && ! $definition->getComposerName()) {
4656
throw new NotSupportVersionsException($pkgName);
4757
}
48-
return $this->fetchVersionsFromGithubRelease($definition->getRepo(), $definition->getBin());
58+
if ($definition->getLatestFetchType() === 'github') {
59+
return $this->fetchVersionsFromGithubRelease($definition->getRepo(), $definition->getBin());
60+
} elseif ($definition->getLatestFetchType() === 'packagist') {
61+
return $this->fetchVersionsFromPackagist($definition->getPkgName(), $definition->getComposerName());
62+
} else {
63+
throw new BoxException('The definition of package is invalid');
64+
}
4965
}
5066
}

src/app/PkgDefinition/Definition.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Definition
1010
protected ?string $latest = null;
1111
protected ?string $latestFetchType = null;
1212
protected ?string $url = null;
13+
protected ?string $composerName = null;
1314
protected ?Jobs $jobs = null;
1415
protected array $jobArtifactMatchRule = [];
1516
protected array $releaseAssetMatchRule = [];
@@ -19,12 +20,13 @@ class Definition
1920

2021
public function __construct(string $pkgName, array $data = [])
2122
{
22-
$this->pkgName = $pkgName;
23+
$this->setPkgName($pkgName);
2324
isset($data['repo']) && is_string($data['repo']) && $this->setRepo($data['repo']);
2425
isset($data['bin']) && is_string($data['bin']) && $this->setBin($data['bin']);
2526
isset($data['latest']) && is_string($data['latest']) && $this->setLatest($data['latest']);
2627
isset($data['latest_fetch_type']) && is_string($data['latest_fetch_type']) && $this->setLatestFetchType($data['latest_fetch_type']);
2728
isset($data['url']) && is_string($data['url']) && $this->setUrl($data['url']);
29+
isset($data['composer_name']) && is_string($data['composer_name']) && $this->setComposerName($data['composer_name']);
2830
isset($data['jobs']) && is_array($data['jobs']) && $this->setJobs(new Jobs($data['jobs']));
2931
isset($data['job_artifact_match_rule']) && is_array($data['job_artifact_match_rule']) && $this->setJobArtifactMatchRule($data['job_artifact_match_rule']);
3032
isset($data['release_asset_match_rule']) && is_array($data['release_asset_match_rule']) && $this->setReleaseAssetMatchRule($data['release_asset_match_rule']);
@@ -153,4 +155,26 @@ public function setReleaseAssetKeyword(string $releaseAssetKeyword): Definition
153155
$this->releaseAssetKeyword = $releaseAssetKeyword;
154156
return $this;
155157
}
158+
159+
public function getComposerName(): ?string
160+
{
161+
return $this->composerName;
162+
}
163+
164+
public function setComposerName(string $composerName): Definition
165+
{
166+
$this->composerName = $composerName;
167+
return $this;
168+
}
169+
170+
public function getPkgName(): string
171+
{
172+
return $this->pkgName;
173+
}
174+
175+
public function setPkgName(string $pkgName): Definition
176+
{
177+
$this->pkgName = $pkgName;
178+
return $this;
179+
}
156180
}

0 commit comments

Comments
 (0)