php-spark/plugins/com.noccy.docker/Stack/StackManager.php
Christopher Vagnetoft 538383c33d PDO plugin: Reflections
* com.noccy.pdo: Implemented reflection for PDO databases,
  tables and columns. Reflectors for MySQL and Sqlite.
* com.noccy.pdo: Added pdo:inspect command.
* com.noccy.docker: Added basic stack management and commands.
* com.noccy.docker: Moved commands to dedicated namespace.
* Environment: readConfig and writeConfig helper added, with
  a flag to use the global config dir ~/.config/spark.
2021-12-24 01:27:57 +01:00

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;
}
}