deblan.tv/vendor/trinity/src/Trinity/Bundle/VarsEditorBundle/Service/Config.php

73 lines
2 KiB
PHP

<?php
namespace Trinity\Bundle\VarsEditorBundle\Service;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;
class Config
{
protected $filesystem;
protected $config;
protected $file;
public function __construct(Container $container)
{
$this->filesystem = new Filesystem();
$this->file = $container->getParameter('trinity_vars_editor.file');
}
protected function getConfig()
{
if (!empty($this->config)) {
return $this->config;
}
return $this->config = Yaml::parse($this->file);
}
public function get($key, $default = null, $locale = null)
{
if (empty($this->file)) {
return $default;
}
if (!$this->filesystem->exists($this->file)) {
return $default;
}
$config = $this->getConfig();
if (empty($config['fieldsets']) || (!empty($config['fieldsets']) && !is_array($config['fieldsets']))) {
return $default;
}
foreach ($config['fieldsets'] as $fieldset) {
if (!is_null($locale) && isset($fieldset['tabs'])) {
if (isset($fieldset['tabs'][$locale])) {
foreach ($fieldset['tabs'][$locale]['vars'] as $var) {
if (isset($var['key'], $var['value'])) {
if ($var['key'] === $key) {
return $var['value'];
}
}
}
}
} elseif (!empty($fieldset['vars'])) {
foreach ($fieldset['vars'] as $var) {
if (isset($var['key'], $var['value'])) {
if ($var['key'] === $key) {
return $var['value'];
}
}
}
}
}
return $default;
}
}