64 lines
1.4 KiB
PHP
64 lines
1.4 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace SparkPlug\Com\Noccy\Docker\Stack;
|
||
|
|
||
|
use Spark\Environment\Environment;
|
||
|
|
||
|
class StackManager
|
||
|
{
|
||
|
|
||
|
private Environment $environment;
|
||
|
|
||
|
private array $stacks = [];
|
||
|
|
||
|
private array $stackObjects = [];
|
||
|
|
||
|
public function __construct(Environment $environment)
|
||
|
{
|
||
|
$this->environment = $environment;
|
||
|
$this->readConfig();
|
||
|
}
|
||
|
|
||
|
private function readConfig()
|
||
|
{
|
||
|
$config = $this->environment->readConfig("com.noccy.docker/stacks.json", true);
|
||
|
if (!$config) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
$this->stacks = $config['stacks'] ?? [];
|
||
|
foreach ($this->stacks as $i=>$stack) {
|
||
|
$this->stackObjects[$i] = new Stack($stack['path'], $stack['options']);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function flushConfig()
|
||
|
{
|
||
|
$config = [
|
||
|
'stacks' => $this->stacks,
|
||
|
];
|
||
|
$this->environment->writeConfig("com.noccy.docker/stacks.json", $config, true);
|
||
|
}
|
||
|
|
||
|
public function registerStack(string $path, array $options=[])
|
||
|
{
|
||
|
$this->stacks[$path] = [
|
||
|
'path' => $path,
|
||
|
'options' => $options
|
||
|
];
|
||
|
$this->flushConfig();
|
||
|
|
||
|
}
|
||
|
|
||
|
public function removeStack(string $path)
|
||
|
{
|
||
|
unset($this->stacks[$path]);
|
||
|
$this->flushConfig();
|
||
|
}
|
||
|
|
||
|
public function getRegisteredStacks(): array
|
||
|
{
|
||
|
return $this->stackObjects;
|
||
|
}
|
||
|
|
||
|
}
|