2019-05-23 17:04:48 +02:00
|
|
|
<?php
|
|
|
|
|
2020-07-25 17:40:08 +02:00
|
|
|
namespace Ardent\ParameterBundle\Service;
|
2019-05-23 17:04:48 +02:00
|
|
|
|
2020-07-27 01:02:18 +02:00
|
|
|
use Ardent\ParameterBundle\DependencyInjection\Configuration;
|
2020-07-25 16:07:01 +02:00
|
|
|
use Ardent\ParameterBundle\Entity\Parameter;
|
2019-05-23 17:04:48 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
2022-01-30 01:11:39 +01:00
|
|
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
2020-07-27 01:02:18 +02:00
|
|
|
use Symfony\Component\Form\Extension\Core\Type\NumberType;
|
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
2019-05-23 17:04:48 +02:00
|
|
|
|
|
|
|
class ParameterService
|
|
|
|
{
|
|
|
|
/** @var EntityManagerInterface */
|
|
|
|
private $em;
|
|
|
|
|
|
|
|
/** @var ParameterBagInterface */
|
|
|
|
private $parameter;
|
2020-07-27 00:26:57 +02:00
|
|
|
private $config;
|
2019-05-23 17:04:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ParameterService constructor.
|
|
|
|
*
|
|
|
|
* @param EntityManagerInterface $em
|
2020-07-27 00:26:57 +02:00
|
|
|
* @param ParameterBagInterface $parameter
|
|
|
|
* @param $config
|
2019-05-23 17:04:48 +02:00
|
|
|
*/
|
2020-07-27 00:26:57 +02:00
|
|
|
public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameter, $config)
|
2019-05-23 17:04:48 +02:00
|
|
|
{
|
|
|
|
$this->em = $em;
|
|
|
|
$this->parameter = $parameter;
|
2020-07-27 01:02:18 +02:00
|
|
|
$this->config = $this->supplementConfig($config);
|
2019-05-23 17:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-07-27 01:23:13 +02:00
|
|
|
* @param string $name The name of the parameter in the format <group>_<parameter>
|
|
|
|
* @param bool $parse Whether to parse embedded %parameter%'s
|
2019-05-23 17:04:48 +02:00
|
|
|
*
|
2020-07-27 01:23:13 +02:00
|
|
|
* @return mixed
|
2019-05-23 17:04:48 +02:00
|
|
|
*/
|
|
|
|
public function get($name, $parse = true)
|
|
|
|
{
|
|
|
|
$paramRepo = $this->em->getRepository(Parameter::class);
|
|
|
|
$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 $value;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-07-27 01:23:13 +02:00
|
|
|
* @param string $name The name of the parameter in the format <group>_<parameter>
|
|
|
|
* @param mixed $value
|
2019-05-23 17:04:48 +02:00
|
|
|
*/
|
|
|
|
public function set($name, $value)
|
|
|
|
{
|
|
|
|
$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();
|
|
|
|
}
|
2020-07-27 00:26:57 +02:00
|
|
|
|
2020-07-27 01:02:18 +02:00
|
|
|
/**
|
|
|
|
* Replaces the types in the config by their classes
|
|
|
|
*
|
|
|
|
* @param $config
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function supplementConfig($config)
|
|
|
|
{
|
|
|
|
foreach ($config as $groupName => &$group) {
|
|
|
|
foreach ($group as &$value) {
|
|
|
|
switch ($value['type']) {
|
|
|
|
default:
|
|
|
|
case Configuration::TYPE_TEXT:
|
|
|
|
$class = TextType::class;
|
|
|
|
break;
|
|
|
|
case Configuration::TYPE_NUMBER:
|
|
|
|
$class = NumberType::class;
|
|
|
|
break;
|
2022-01-30 01:11:39 +01:00
|
|
|
case Configuration::TYPE_BOOL:
|
|
|
|
$class = CheckboxType::class;
|
|
|
|
break;
|
2020-07-27 01:02:18 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 01:23:13 +02:00
|
|
|
// Add the group name in front of the parameter name
|
2020-07-27 01:02:18 +02:00
|
|
|
$value['name'] = sprintf('%s_%s', $groupName, $value['name']);
|
|
|
|
$value['type'] = $class;
|
|
|
|
unset($value);
|
|
|
|
}
|
|
|
|
unset($group);
|
|
|
|
}
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
2020-07-27 00:26:57 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getConfig()
|
|
|
|
{
|
|
|
|
return $this->config;
|
|
|
|
}
|
2019-05-23 17:04:48 +02:00
|
|
|
}
|