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 _ */ 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; } }