2020-05-03 16:41:59 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Aerex\BaikalStorage\Configs;
|
|
|
|
|
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface;
|
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
|
|
|
use Symfony\Component\Config\Definition\Processor;
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
|
|
|
|
|
|
|
class ConfigBuilder implements ConfigurationInterface {
|
|
|
|
private $configs = [];
|
2020-05-05 00:04:31 -05:00
|
|
|
private $configDir;
|
2020-05-03 16:41:59 -05:00
|
|
|
|
2020-05-05 00:04:31 -05:00
|
|
|
public function __construct($configDir = null) {
|
|
|
|
if (!isset($configDir)) {
|
|
|
|
$this->configDir = $this->getHomeDir() . '~/.config/baikal';
|
|
|
|
} else {
|
|
|
|
$this->configDir = $configDir;
|
|
|
|
}
|
2020-05-03 16:41:59 -05:00
|
|
|
$this->processor = new Processor();
|
|
|
|
}
|
|
|
|
|
2020-05-05 00:04:31 -05:00
|
|
|
private function getHomeDir() {
|
|
|
|
if (stristr(PHP_OS, 'WIN')) {
|
|
|
|
return rtrim($_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'], '\\/');
|
|
|
|
} else {
|
|
|
|
return rtrim($_SERVER['HOME'], '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 16:41:59 -05:00
|
|
|
public function add($config) {
|
|
|
|
$this->configs[] = $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getConfigTreeBuilder() {
|
|
|
|
$treeBuilder = new TreeBuilder('configs');
|
|
|
|
$rootNode = $treeBuilder->getRootNode();
|
|
|
|
$ref = $rootNode->children();
|
|
|
|
foreach ($this->configs as $config) {
|
|
|
|
$ref = $ref->append($config->get());
|
|
|
|
}
|
|
|
|
$ref->end();
|
|
|
|
return $treeBuilder;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function readContent() {
|
2020-05-05 00:04:31 -05:00
|
|
|
if (!is_dir($this->configDir)) {
|
|
|
|
mkdir($this->configDir, 0755, true);
|
2020-05-03 16:41:59 -05:00
|
|
|
}
|
2020-05-05 00:04:31 -05:00
|
|
|
$contents = sprintf('%s/storage.yml', $this->configDir);
|
2020-05-03 16:41:59 -05:00
|
|
|
return file_get_contents($contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function loadYaml() {
|
|
|
|
$contents = $this->readContent();
|
|
|
|
$parseContents = Yaml::parse($contents);
|
|
|
|
return $this->processor->processConfiguration($this, [$parseContents]);
|
|
|
|
}
|
|
|
|
}
|