Skip to content

Commit

Permalink
Added method for retrieving packages from composer api
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxwilko committed Jan 6, 2025
1 parent 6b03b16 commit 43f0339
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/Packager/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Illuminate\Support\Facades\Cache;
use Winter\Packager\Composer as PackagerComposer;
use Winter\Storm\Exception\ApplicationException;
use Winter\Storm\Foundation\Extension\WinterExtension;
use Winter\Storm\Network\Http;
use Winter\Storm\Packager\Commands\InfoCommand;
use Winter\Storm\Packager\Commands\RemoveCommand;
use Winter\Storm\Packager\Commands\RequireCommand;
Expand Down Expand Up @@ -168,4 +170,31 @@ protected static function remember(string $key, callable $callable, ?int $expire

return $result;
}

public static function listPackages(string $type): array
{
return Cache::remember(static::COMPOSER_CACHE_KEY . '.packages.' . $type, 60 * 60 * 24, function () use ($type) {
$page = 0;
$packages = [];
do {
$result = Http::get('https://packagist.org/search.json', function (Http $http) use (&$page, $type) {
$http->data([
'q' => '',
'page' => ++$page,
'type' => $type
]);
});

if ($result->code != '200') {
throw new ApplicationException('Unable to retrieve packages, failed with code: ' . $result->code);
}

$data = json_decode($result->body, JSON_OBJECT_AS_ARRAY);

$packages = array_merge($packages, $data['results']);
} while (isset($data['next']));

return $packages;
});
}
}

0 comments on commit 43f0339

Please sign in to comment.