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-28 11:54:21 -05:00
|
|
|
private $configFile;
|
2020-05-03 16:41:59 -05:00
|
|
|
|
2020-05-28 11:54:21 -05:00
|
|
|
public function __construct($configFile) {
|
|
|
|
$this->configFile = $configFile;
|
2020-05-03 16:41:59 -05:00
|
|
|
$this->processor = new Processor();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add($config) {
|
|
|
|
$this->configs[] = $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getConfigTreeBuilder() {
|
2020-05-12 23:56:22 -05:00
|
|
|
$treeBuilder = new TreeBuilder();
|
|
|
|
$rootNode = $treeBuilder->root('configs');
|
2020-05-28 11:54:21 -05:00
|
|
|
$ref = $rootNode->children()
|
|
|
|
->arrayNode('logger')
|
|
|
|
->canBeEnabled()
|
|
|
|
->children()
|
2020-05-29 11:45:53 -05:00
|
|
|
->scalarNode('file')->end()
|
2020-05-28 11:54:21 -05:00
|
|
|
->scalarNode('level')
|
|
|
|
->defaultValue('ERROR')
|
|
|
|
->validate()
|
|
|
|
->IfNotInArray(['DEBUG', 'INFO', 'NOTICE', 'WARNING', 'ERROR', 'CRITICAL', 'ALERT', 'EMERGENCY'])
|
|
|
|
->thenInvalid('Invalid log level %s')
|
2020-05-29 11:45:53 -05:00
|
|
|
->end()
|
|
|
|
->end()
|
|
|
|
->end()
|
|
|
|
->end();
|
2020-05-28 11:54:21 -05:00
|
|
|
|
2020-05-03 16:41:59 -05:00
|
|
|
foreach ($this->configs as $config) {
|
|
|
|
$ref = $ref->append($config->get());
|
|
|
|
}
|
|
|
|
$ref->end();
|
|
|
|
return $treeBuilder;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function readContent() {
|
2020-05-28 11:54:21 -05:00
|
|
|
return file_get_contents($this->configFile);
|
2020-05-03 16:41:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function loadYaml() {
|
|
|
|
$contents = $this->readContent();
|
|
|
|
$parseContents = Yaml::parse($contents);
|
|
|
|
return $this->processor->processConfiguration($this, [$parseContents]);
|
|
|
|
}
|
|
|
|
}
|