fresh/src/Configuration/ComposeConfiguration.php

51 lines
1.2 KiB
PHP

<?php
namespace NoccyLabs\FreshDocker\Configuration;
use Symfony\Component\Yaml\Yaml;
class ComposeConfiguration
{
private array $checks = [];
private array $services = [];
public function __construct(string $configfile)
{
$this->loadComposeFile($configfile);
}
private function loadComposeFile($configfile)
{
$config = Yaml::parseFile($configfile);
$services = $config['services']??[];
foreach ($services as $name=>$service) {
$image = $service['image']??null;
if ($image && !in_array($image,$this->checks)) {
$this->checks[] = $image;
}
if (!array_key_exists($image, $this->services)) {
$this->services[$image] = [];
}
$this->services[$image][] = $name;
}
}
public function getChecks(): array
{
return $this->checks;
}
public function getServicesForImages(array $images)
{
$ret = [];
foreach ($images as $ref) {
$image = (string)$ref->ref;
if (array_key_exists($image, $this->services)) {
$ret = array_merge($ret, $this->services[$image]);
}
}
return array_unique($ret);
}
}