em = $em; $this->parameter = $parameter; $this->config = $this->supplementConfig($config); } /** * @param string $name The name of the parameter in the format _ * @param bool $parse Whether to parse embedded %parameter%'s * * @return mixed */ 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; } } /** * @param string $name The name of the parameter in the format _ * @param mixed $value */ 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(); } /** * 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; } // Add the group name in front of the parameter name $value['name'] = sprintf('%s_%s', $groupName, $value['name']); $value['type'] = $class; unset($value); } unset($group); } return $config; } /** * @return array */ public function getConfig() { return $this->config; } }