113 lines
3.6 KiB
PHP
113 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace Ardent\ParameterBundle\Service;
|
|
|
|
use Ardent\ParameterBundle\DependencyInjection\Configuration;
|
|
use Ardent\ParameterBundle\Entity\Parameter;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
|
use Symfony\Component\Form\Extension\Core\Type\NumberType;
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
|
|
|
class ParameterService
|
|
{
|
|
private array $config;
|
|
|
|
public function __construct(
|
|
private EntityManagerInterface $em,
|
|
private ParameterBagInterface $parameter,
|
|
array $config
|
|
)
|
|
{
|
|
$this->config = $this->supplementConfig($config);
|
|
}
|
|
|
|
public function get(string $name, bool $parse = true): mixed
|
|
{
|
|
$split = explode('_', $name);
|
|
$category = array_shift($split);
|
|
|
|
$paramRepo = $this->em->getRepository(Parameter::class);
|
|
/** @var Parameter $parameter */
|
|
$parameter = $paramRepo->findOneBy(['name' => $name]);
|
|
if ($parameter) {
|
|
if ($parse) {
|
|
// find and replace all %parameter% with their value
|
|
$value = preg_replace_callback('/%([^%\s]+)%/', function ($match) {
|
|
$key = $match[1];
|
|
// first try locally
|
|
if ($value = $this->get($key)) {
|
|
return $value;
|
|
}
|
|
// then try with parameter bag
|
|
return $this->parameter->get($key);
|
|
},
|
|
$parameter->getValue()
|
|
);
|
|
} else {
|
|
$value = $parameter->getValue();
|
|
}
|
|
|
|
return Parameter::parseValue($value, $this->config[$category][$name]['type']);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param string $name The name of the parameter in the format <group>_<parameter>
|
|
*/
|
|
public function set(string $name, mixed $value): void
|
|
{
|
|
$paramRepo = $this->em->getRepository(Parameter::class);
|
|
/* @var Parameter $parameter */
|
|
$parameter = $paramRepo->findOneBy(['name' => $name]);
|
|
if (!$parameter) {
|
|
$parameter = (new Parameter())->setName($name);
|
|
}
|
|
$parameter->setValue($value);
|
|
$this->em->persist($parameter);
|
|
$this->em->flush();
|
|
}
|
|
|
|
private function supplementConfig(array $rawConfig): array
|
|
{
|
|
$config = [];
|
|
foreach ($rawConfig as $groupName => $rawGroup) {
|
|
$group = [];
|
|
foreach ($rawGroup as &$value) {
|
|
switch ($value['type']) {
|
|
default:
|
|
case Configuration::TYPE_TEXT:
|
|
$class = TextType::class;
|
|
break;
|
|
case Configuration::TYPE_NUMBER:
|
|
$class = NumberType::class;
|
|
break;
|
|
case Configuration::TYPE_BOOL:
|
|
$class = CheckboxType::class;
|
|
break;
|
|
}
|
|
|
|
// Add the group name in front of the parameter name
|
|
$value['name'] = sprintf('%s_%s', $groupName, $value['name']);
|
|
$value['type'] = $class;
|
|
|
|
$group[$value['name']] = $value;
|
|
unset($value);
|
|
}
|
|
$config[$groupName] = $group;
|
|
}
|
|
return $config;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function getConfig()
|
|
{
|
|
return $this->config;
|
|
}
|
|
}
|