Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
61b8a68705 | |||
7040fd5ea7 | |||
a2fe3584be | |||
dacf3d6687 | |||
cd3c5a0a52 | |||
da8a1e37ca | |||
d20f7401c1 | |||
69abbcaf4b | |||
6c43ee8fd1 | |||
db970b4c69 | |||
a2c17d4be3 | |||
28b21b8e12 |
@ -1,13 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace Ardent\ParameterBundle;
|
|
||||||
|
|
||||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Bundle.
|
|
||||||
*/
|
|
||||||
class ArdentParameterBundle extends Bundle
|
|
||||||
{
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Ardent\ParameterBundle\DependencyInjection;
|
|
||||||
|
|
||||||
use Ardent\ParameterBundle\Entity\Parameter;
|
|
||||||
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');
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,75 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
services:
|
|
||||||
ardent.parameter:
|
|
||||||
alias: Ardent\ParameterBundle\Service\ParameterService
|
|
||||||
|
|
||||||
Ardent\ParameterBundle\Service\ParameterService:
|
|
||||||
public: true
|
|
||||||
autowire: true
|
|
@ -1,78 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Ardent\ParameterBundle\Service;
|
|
||||||
|
|
||||||
use 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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +1,16 @@
|
|||||||
{
|
{
|
||||||
"name": "ardent/parameter-bundle",
|
"name": "ardent/parameter-bundle",
|
||||||
"description": "Simple bundle for storing parameters in the database",
|
"description": "Simple bundle for storing parameters in a database",
|
||||||
"type": "symfony-bundle",
|
"type": "symfony-bundle",
|
||||||
"require": {
|
"require": {
|
||||||
"doctrine/orm": "^2.7",
|
"php": "^8.0",
|
||||||
"symfony/form": "^5.0",
|
"doctrine/orm": "^2.0",
|
||||||
"symfony/twig-bundle": "^5.0"
|
"symfony/form": "^5.0|^6.0",
|
||||||
|
"symfony/twig-bundle": "^5.0|^6.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Ardent\\ParameterBundle\\": ""
|
"Ardent\\ParameterBundle\\": "src/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"license": "GPL-3.0-only"
|
"license": "GPL-3.0-only"
|
||||||
|
12
config/services.yaml
Normal file
12
config/services.yaml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
services:
|
||||||
|
ardent.parameter:
|
||||||
|
alias: Ardent\ParameterBundle\Service\ParameterService
|
||||||
|
|
||||||
|
Ardent\ParameterBundle\Service\ParameterService:
|
||||||
|
public: true
|
||||||
|
autowire: true
|
||||||
|
|
||||||
|
Ardent\ParameterBundle\Controller\:
|
||||||
|
resource: '../src/Controller'
|
||||||
|
tags: ['controller.service_arguments', 'container.service_subscriber']
|
||||||
|
autowire: true
|
52
src/ArdentParameterBundle.php
Normal file
52
src/ArdentParameterBundle.php
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace Ardent\ParameterBundle;
|
||||||
|
|
||||||
|
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
|
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||||
|
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
|
||||||
|
|
||||||
|
class ArdentParameterBundle extends AbstractBundle
|
||||||
|
{
|
||||||
|
|
||||||
|
public const TYPE_TEXT = 'text';
|
||||||
|
public const TYPE_NUMBER = 'number';
|
||||||
|
public const TYPE_BOOL = 'bool';
|
||||||
|
public const TYPE_CHOICE = 'choice';
|
||||||
|
|
||||||
|
public function configure(DefinitionConfigurator $definition): void
|
||||||
|
{
|
||||||
|
// $treeBuilder = new TreeBuilder('ardent_parameter');
|
||||||
|
|
||||||
|
$definition->rootNode()
|
||||||
|
->children()
|
||||||
|
->arrayNode('parameters')
|
||||||
|
->arrayPrototype()
|
||||||
|
->arrayPrototype()
|
||||||
|
->children()
|
||||||
|
->scalarNode('name')->end()
|
||||||
|
->enumNode('type')
|
||||||
|
->values([self::TYPE_TEXT, self::TYPE_NUMBER, self::TYPE_BOOL, self::TYPE_CHOICE])
|
||||||
|
->defaultValue(self::TYPE_TEXT)
|
||||||
|
->end()
|
||||||
|
->arrayNode('choices')
|
||||||
|
->scalarPrototype()->end()
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
|
->end();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
|
||||||
|
{
|
||||||
|
$container->import('../config/services.yaml');
|
||||||
|
|
||||||
|
// Retrieves the parameters from the config and injects them info the ardent.parameter service
|
||||||
|
$def = $builder->findDefinition('ardent.parameter');
|
||||||
|
$def->setArgument('$config', $config['parameters']);
|
||||||
|
}
|
||||||
|
}
|
@ -9,21 +9,17 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
|||||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||||
use Symfony\Component\Form\FormBuilderInterface;
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
abstract class BaseController extends AbstractController
|
abstract class BaseController extends AbstractController
|
||||||
{
|
{
|
||||||
/**
|
public function baseIndex(ParameterService $param, Request $request, array $configuration): Response
|
||||||
* @param ParameterService $param
|
|
||||||
* @param Request $request
|
|
||||||
* @param [] $configuration
|
|
||||||
*/
|
|
||||||
public function baseIndex(ParameterService $param, Request $request, $configuration)
|
|
||||||
{
|
{
|
||||||
// gather the values
|
// gather the values
|
||||||
$data = [];
|
$data = [];
|
||||||
foreach ($configuration as $config) {
|
foreach ($configuration as $config) {
|
||||||
$name = $config['name'];
|
$name = $config['name'];
|
||||||
$data[$name] = $this->valueParser($config['type'], $param->get($name, false));
|
$data[$name] = $param->get($name, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// build the form
|
// build the form
|
||||||
@ -40,6 +36,7 @@ abstract class BaseController extends AbstractController
|
|||||||
|
|
||||||
foreach ($configuration as $config) {
|
foreach ($configuration as $config) {
|
||||||
$name = $config['name'];
|
$name = $config['name'];
|
||||||
|
// Excluding the checkbox type from checking, because it will be null if not checked
|
||||||
if (null !== $result[$name] || CheckboxType::class == $config['type']) {
|
if (null !== $result[$name] || CheckboxType::class == $config['type']) {
|
||||||
$param->set($name, $result[$name]);
|
$param->set($name, $result[$name]);
|
||||||
}
|
}
|
||||||
@ -50,36 +47,26 @@ abstract class BaseController extends AbstractController
|
|||||||
}
|
}
|
||||||
|
|
||||||
return $this->render('@ArdentParameter/form.html.twig', [
|
return $this->render('@ArdentParameter/form.html.twig', [
|
||||||
'parameter_form' => $form->createView(),
|
'parameter_form' => $form->createView(),
|
||||||
]);
|
]);
|
||||||
}
|
|
||||||
|
|
||||||
private function valueParser($type, $value)
|
|
||||||
{
|
|
||||||
switch ($type) {
|
|
||||||
case CheckboxType::class:
|
|
||||||
return boolval($value);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return $value;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $config
|
* @param $config
|
||||||
* @param FormBuilderInterface $formBuilder
|
* @param FormBuilderInterface $formBuilder
|
||||||
*/
|
*/
|
||||||
private function configParser($config, &$formBuilder)
|
private function configParser($config, FormBuilderInterface $formBuilder): void
|
||||||
{
|
{
|
||||||
$type = $config['type'];
|
$type = $config['type'];
|
||||||
$options = [];
|
$options = [];
|
||||||
switch ($type) {
|
switch ($type) {
|
||||||
case ChoiceType::class:
|
case ChoiceType::class:
|
||||||
if (is_callable($config['choices'])) {
|
$choices = array_combine($config['choices'], $config['choices']);
|
||||||
$options['choices'] = $config['choices']();
|
|
||||||
|
if (is_callable($choices)) {
|
||||||
|
$options['choices'] = $choices();
|
||||||
} else {
|
} else {
|
||||||
$options['choices'] = $config['choices'];
|
$options['choices'] = $choices;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case CheckboxType::class:
|
case CheckboxType::class:
|
42
src/Controller/ParameterController.php
Normal file
42
src/Controller/ParameterController.php
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace Ardent\ParameterBundle\Controller;
|
||||||
|
|
||||||
|
|
||||||
|
use Ardent\ParameterBundle\Service\ParameterService;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\Routing\Annotation\Route;
|
||||||
|
|
||||||
|
class ParameterController extends BaseController
|
||||||
|
{
|
||||||
|
#[Route('/{category}', name: 'parameter_bundle_parameters')]
|
||||||
|
public function parameters(
|
||||||
|
ParameterService $param,
|
||||||
|
Request $request,
|
||||||
|
?string $category = 'categories',
|
||||||
|
): Response
|
||||||
|
{
|
||||||
|
$configs = $param->getConfig();
|
||||||
|
|
||||||
|
// Handle all special cases for the name
|
||||||
|
if ('categories' === $category) { // List all the categories
|
||||||
|
$categories = [];
|
||||||
|
foreach ($configs as $key => $config) {
|
||||||
|
$categories[] = $key;
|
||||||
|
}
|
||||||
|
$categories[] = 'all';
|
||||||
|
return $this->render('@ArdentParameter/categories.html.twig', ['categories' => $categories]);
|
||||||
|
} elseif ('all' === $category) { // Show all parameters from all categories
|
||||||
|
$allConfigs = [];
|
||||||
|
foreach ($configs as $config) {
|
||||||
|
$allConfigs = array_merge($allConfigs, $config);
|
||||||
|
}
|
||||||
|
|
||||||
|
return parent::baseIndex($param, $request, $allConfigs);
|
||||||
|
} else { // Show the parameters from one category
|
||||||
|
return parent::baseIndex($param, $request, $configs[$category]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
70
src/Entity/Parameter.php
Normal file
70
src/Entity/Parameter.php
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ardent\ParameterBundle\Entity;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\Types\TextType;
|
||||||
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\NumberType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Entity
|
||||||
|
* @ORM\Table(name="parameter_bundle_parameters")
|
||||||
|
*/
|
||||||
|
class Parameter
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @ORM\Id
|
||||||
|
* @ORM\GeneratedValue(strategy="AUTO")
|
||||||
|
* @ORM\Column(type="integer")
|
||||||
|
*/
|
||||||
|
private ?int $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="string")
|
||||||
|
*/
|
||||||
|
private ?string $name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="string")
|
||||||
|
*/
|
||||||
|
private mixed $value;
|
||||||
|
|
||||||
|
public function getId(): int
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getName(): string
|
||||||
|
{
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setName(string $name): self
|
||||||
|
{
|
||||||
|
$this->name = $name;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getValue(): mixed
|
||||||
|
{
|
||||||
|
return $this->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
static function parseValue(mixed $value, string $type): mixed
|
||||||
|
{
|
||||||
|
return match ($type) {
|
||||||
|
NumberType::class => intval($value),
|
||||||
|
CheckboxType::class => boolval($value),
|
||||||
|
default => $value,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setValue(mixed $value): self
|
||||||
|
{
|
||||||
|
$this->value = $value;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
119
src/Service/ParameterService.php
Normal file
119
src/Service/ParameterService.php
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ardent\ParameterBundle\Service;
|
||||||
|
|
||||||
|
use Ardent\ParameterBundle\ArdentParameterBundle;
|
||||||
|
use Ardent\ParameterBundle\Entity\Parameter;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\NumberType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||||
|
|
||||||
|
class ParameterService
|
||||||
|
{
|
||||||
|
private array $config;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private readonly EntityManagerInterface $em,
|
||||||
|
private readonly ParameterBagInterface $parameter,
|
||||||
|
array $config
|
||||||
|
)
|
||||||
|
{
|
||||||
|
$this->config = $this->supplementConfig($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name The name of the parameter in the format <group>_<parameter>
|
||||||
|
*/
|
||||||
|
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 <group>_<parameter>
|
||||||
|
*/
|
||||||
|
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 ArdentParameterBundle::TYPE_TEXT:
|
||||||
|
$class = TextType::class;
|
||||||
|
break;
|
||||||
|
case ArdentParameterBundle::TYPE_NUMBER:
|
||||||
|
$class = NumberType::class;
|
||||||
|
break;
|
||||||
|
case ArdentParameterBundle::TYPE_BOOL:
|
||||||
|
$class = CheckboxType::class;
|
||||||
|
break;
|
||||||
|
case ArdentParameterBundle::TYPE_CHOICE:
|
||||||
|
$class = ChoiceType::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;
|
||||||
|
}
|
||||||
|
}
|
9
templates/categories.html.twig
Normal file
9
templates/categories.html.twig
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{% extends '@ArdentParameter/layout.html.twig' %}
|
||||||
|
|
||||||
|
|
||||||
|
{% block par_user_content %}
|
||||||
|
<h2>Parameter categories</h2><br>
|
||||||
|
{% for category in categories %}
|
||||||
|
<a class="btn-primary btn" href="{{ path('parameter_bundle_parameters', {category:category}) }}">{{ category }}</a><br><br>
|
||||||
|
{% endfor %}
|
||||||
|
{% endblock %}
|
1
templates/form.extra.html.twig
Normal file
1
templates/form.extra.html.twig
Normal file
@ -0,0 +1 @@
|
|||||||
|
{# Add extra content on the bottom of the form #}
|
@ -1,8 +1,8 @@
|
|||||||
{% extends '@ArdentParameter/layout.html.twig' %}
|
{% extends '@ArdentParameter/layout.html.twig' %}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
{% block par_user_content %}
|
{% block par_user_content %}
|
||||||
|
<a class="btn-primary btn" href="{{ path('parameter_bundle_parameters', {category:'categories'}) }}">Back</a> <br><br>
|
||||||
{{ form(parameter_form) }}
|
{{ form(parameter_form) }}
|
||||||
{% include '@ArdentParameter/form.extra.html.twig' %}
|
{% include '@ArdentParameter/form.extra.html.twig' %}
|
||||||
{% endblock %}
|
{% endblock %}
|
Reference in New Issue
Block a user