From 301862589b32204a18be9293da4471947f04daff Mon Sep 17 00:00:00 2001 From: ardent Date: Thu, 23 May 2019 17:04:48 +0200 Subject: [PATCH] first commit --- ArdentParameterBundle.php | 9 +++ Controller/BaseController.php | 73 +++++++++++++++++ .../ArdentParameterExtension.php | 20 +++++ Entity/Parameter.php | 75 ++++++++++++++++++ Resources/config/services.yaml | 3 + Resources/views/form.html.twig | 6 ++ Resources/views/layout.html.twig | 2 + Service/ParameterService.php | 78 +++++++++++++++++++ 8 files changed, 266 insertions(+) create mode 100644 ArdentParameterBundle.php create mode 100644 Controller/BaseController.php create mode 100644 DependencyInjection/ArdentParameterExtension.php create mode 100644 Entity/Parameter.php create mode 100644 Resources/config/services.yaml create mode 100644 Resources/views/form.html.twig create mode 100644 Resources/views/layout.html.twig create mode 100644 Service/ParameterService.php diff --git a/ArdentParameterBundle.php b/ArdentParameterBundle.php new file mode 100644 index 0000000..0a350d8 --- /dev/null +++ b/ArdentParameterBundle.php @@ -0,0 +1,9 @@ +get($name, false); + } + + // build the form + $formBuilder = $this->createFormBuilder($data); + foreach ($configuration as $config) { + $this->configParser($config, $formBuilder); + } + $form = $formBuilder->add('Update', SubmitType::class)->getForm(); + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $result = $form->getData(); + + foreach ($configuration as $config) { + $name = $config['name']; + if ($result[$name]) { + $param->set($name, $result[$name]); + } + } + $this->addFlash('success', 'Updating parameters successful'); + + return $this->redirect($request->getRequestUri()); + } + + return $this->render('@ArdentParameter/form.html.twig', [ + 'parameter_form' => $form->createView(), + ]); + } + + /** + * @param $config + * @param FormBuilderInterface $formBuilder + */ + private function configParser($config, &$formBuilder) + { + $type = $config['type']; + $options = []; + switch ($type) { + case ChoiceType::class: + $options['choices'] = $config['choices']; + break; + default: + break; + } + $formBuilder->add($config['name'], $type, $options); + } +} diff --git a/DependencyInjection/ArdentParameterExtension.php b/DependencyInjection/ArdentParameterExtension.php new file mode 100644 index 0000000..7b82c5c --- /dev/null +++ b/DependencyInjection/ArdentParameterExtension.php @@ -0,0 +1,20 @@ +load('services.yaml'); + } +} diff --git a/Entity/Parameter.php b/Entity/Parameter.php new file mode 100644 index 0000000..6d73f21 --- /dev/null +++ b/Entity/Parameter.php @@ -0,0 +1,75 @@ +id; + } + + /** + * @return mixed + */ + public function getName() + { + return $this->name; + } + + /** + * @param mixed $name + * @return Parameter + */ + public function setName($name) + { + $this->name = $name; + return $this; + } + + /** + * @return mixed + */ + public function getValue() + { + return $this->value; + } + + /** + * @param mixed $value + * @return Parameter + */ + public function setValue($value) + { + $this->value = $value; + return $this; + } + + +} diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml new file mode 100644 index 0000000..637261b --- /dev/null +++ b/Resources/config/services.yaml @@ -0,0 +1,3 @@ +services: + App\Ardent\ParameterBundle\Service\ParameterService: + alias: 'ardent.parameter' \ No newline at end of file diff --git a/Resources/views/form.html.twig b/Resources/views/form.html.twig new file mode 100644 index 0000000..e7abebd --- /dev/null +++ b/Resources/views/form.html.twig @@ -0,0 +1,6 @@ +{% extends '@ArdentParameter/layout.html.twig' %} + +{% block par_user_content %} + {{ form(parameter_form) }} + Back +{% endblock %} \ No newline at end of file diff --git a/Resources/views/layout.html.twig b/Resources/views/layout.html.twig new file mode 100644 index 0000000..3f09e5a --- /dev/null +++ b/Resources/views/layout.html.twig @@ -0,0 +1,2 @@ +{% block par_user_content %} +{% endblock %} \ No newline at end of file diff --git a/Service/ParameterService.php b/Service/ParameterService.php new file mode 100644 index 0000000..bf2355d --- /dev/null +++ b/Service/ParameterService.php @@ -0,0 +1,78 @@ +em = $em; + $this->parameter = $parameter; + } + + /** + * @param string $name + * @param bool $parse + * + * @return string + */ + 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 $name + * @param $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(); + } +}