first commit

This commit is contained in:
Tim 2019-05-23 17:04:48 +02:00 committed by Tim van Veenendaal
commit 301862589b
8 changed files with 266 additions and 0 deletions

View File

@ -0,0 +1,9 @@
<?php
namespace App\Ardent\ParameterBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class ArdentParameterBundle extends Bundle
{
}

View File

@ -0,0 +1,73 @@
<?php
namespace App\Ardent\ParameterBundle\Controller;
use App\Ardent\ParameterBundle\Service\ParameterService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Request;
abstract class BaseController extends AbstractController
{
/**
* @param ParameterService $param
* @param Request $request
* @param [] $configuration
*/
public function baseIndex(ParameterService $param, Request $request, $configuration)
{
// gather the values
$data = [];
foreach ($configuration as $config) {
$name = $config['name'];
$data[$name] = $param->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);
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace App\Ardent\ParameterBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class ArdentParameterExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__.'/../Resources/config')
);
$loader->load('services.yaml');
}
}

75
Entity/Parameter.php Normal file
View File

@ -0,0 +1,75 @@
<?php
namespace App\Ardent\ParameterBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="parameter_bundle_parameters")
*/
class Parameter
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string")
*/
private $name;
/**
* @ORM\Column(type="string")
*/
private $value;
/**
* @return mixed
*/
public function getId()
{
return $this->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;
}
}

View File

@ -0,0 +1,3 @@
services:
App\Ardent\ParameterBundle\Service\ParameterService:
alias: 'ardent.parameter'

View File

@ -0,0 +1,6 @@
{% extends '@ArdentParameter/layout.html.twig' %}
{% block par_user_content %}
{{ form(parameter_form) }}
<a class="btn btn-light" href="{{ path('all_parameters') }}">Back</a>
{% endblock %}

View File

@ -0,0 +1,2 @@
{% block par_user_content %}
{% endblock %}

View File

@ -0,0 +1,78 @@
<?php
namespace App\Ardent\ParameterBundle\Service;
use App\Ardent\ParameterBundle\Entity\Parameter;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class ParameterService
{
/** @var EntityManagerInterface */
private $em;
/** @var ParameterBagInterface */
private $parameter;
/**
* ParameterService constructor.
*
* @param EntityManagerInterface $em
*/
public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameter)
{
$this->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();
}
}