55 lines
1.6 KiB
PHP
55 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Registries;
|
|
|
|
use App\Enums\RegistryType;
|
|
use App\Models\Environment;
|
|
use App\Models\Registry;
|
|
|
|
class ImageReference
|
|
{
|
|
public function tagFor(Environment $environment, string $commitSha, ?Registry $registry = null): string
|
|
{
|
|
$tag = substr($commitSha, 0, 12);
|
|
|
|
if ($this->registryType($registry) === RegistryType::MANAGED) {
|
|
$namespace = trim((string) config('keystone.managed_registry.namespace', 'keystone'), '/');
|
|
$applicationId = $environment->application->uuid ?: 'app-'.$environment->application_id;
|
|
$environmentId = $environment->uuid ?: 'env-'.$environment->id;
|
|
$path = $namespace.'/'.$applicationId.'/'.$environmentId;
|
|
|
|
return $path.':'.$tag;
|
|
}
|
|
|
|
return str($environment->application->name)
|
|
->slug()
|
|
->append(':'.$tag)
|
|
->value();
|
|
}
|
|
|
|
public function registryReference(Registry $registry, string $imageTag): string
|
|
{
|
|
return rtrim($this->registryHost((string) $registry->url), '/').'/'.ltrim($imageTag, '/');
|
|
}
|
|
|
|
private function registryHost(string $url): string
|
|
{
|
|
$host = preg_replace('#^https?://#', '', trim($url));
|
|
|
|
return $host === null ? trim($url) : $host;
|
|
}
|
|
|
|
private function registryType(?Registry $registry): ?RegistryType
|
|
{
|
|
if (! $registry instanceof Registry) {
|
|
return null;
|
|
}
|
|
|
|
if ($registry->type instanceof RegistryType) {
|
|
return $registry->type;
|
|
}
|
|
|
|
return RegistryType::tryFrom((string) $registry->type);
|
|
}
|
|
}
|