fresh/src/Registry/RegistryV2Client.php

59 lines
1.5 KiB
PHP

<?php
namespace NoccyLabs\FreshDocker\Registry;
use GuzzleHttp\Client;
class RegistryV2Client
{
private Client $client;
public function __construct(string $registry, ?array $auth)
{
$this->createClient($registry, $auth);
}
private function createClient(string $registry, ?array $auth)
{
$this->client = new Client([
'base_uri' => 'https://' . $registry . '/v2/',
'auth' => $auth,
]);
}
public function getManifest(string $image, string $tag)
{
$response = $this->client->get("{$image}/manifests/{$tag}", [
'headers' => [
//'Accept' => 'application/vnd.docker.distribution.manifest.v2+json',
]
]);
$body = $response->getBody();
return json_decode($body);
}
public function getImageStatus(string $image, string $tag)
{
$manifest = $this->getManifest($image, $tag);
//print_r($manifest);
$fslayers = (array)(($manifest->fsLayers??$manifest->layers)??[]);
$fshashes = array_map(fn($layer) => $layer->blobSum??$layer->digest, $fslayers);
$metahash = hash("sha256", join("|", $fshashes));
if (isset($manifest->history)) {
$history = $manifest->history[0];
$info = json_decode($history->v1Compatibility);
}
return [
'image' => $image,
'tag' => $tag,
'hash' => $metahash,
'created' => $info->created??null,
];
}
}