1212
1313namespace App \DownloadHandler ;
1414
15+ use App \Exception \BoxException ;
1516use App \Exception \NotSupportVersionsException ;
17+ use App \PkgDefinition \Definition ;
18+ use GuzzleHttp \Client ;
1619use SplFileInfo ;
1720
1821class 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}
0 commit comments