Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
dacf3d6687 | |||
cd3c5a0a52 | |||
da8a1e37ca | |||
d20f7401c1 | |||
69abbcaf4b | |||
6c43ee8fd1 | |||
db970b4c69 | |||
a2c17d4be3 | |||
28b21b8e12 |
@ -40,6 +40,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]);
|
||||||
}
|
}
|
||||||
@ -59,10 +60,8 @@ abstract class BaseController extends AbstractController
|
|||||||
switch ($type) {
|
switch ($type) {
|
||||||
case CheckboxType::class:
|
case CheckboxType::class:
|
||||||
return boolval($value);
|
return boolval($value);
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
return $value;
|
return $value;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
41
Controller/ParameterController.php
Normal file
41
Controller/ParameterController.php
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace Ardent\ParameterBundle\Controller;
|
||||||
|
|
||||||
|
|
||||||
|
use Ardent\ParameterBundle\Service\ParameterService;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\Routing\Annotation\Route;
|
||||||
|
|
||||||
|
class ParameterController extends BaseController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @Route("/{category}", name="parameter_bundle_parameters")
|
||||||
|
*/
|
||||||
|
public function parameters($category = 'categories',
|
||||||
|
ParameterService $param,
|
||||||
|
Request $request)
|
||||||
|
{
|
||||||
|
$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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace Ardent\ParameterBundle\DependencyInjection;
|
namespace Ardent\ParameterBundle\DependencyInjection;
|
||||||
|
|
||||||
use Ardent\ParameterBundle\Entity\Parameter;
|
|
||||||
use Symfony\Component\Config\FileLocator;
|
use Symfony\Component\Config\FileLocator;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
use Symfony\Component\DependencyInjection\Extension\Extension;
|
use Symfony\Component\DependencyInjection\Extension\Extension;
|
||||||
@ -14,8 +13,14 @@ class ArdentParameterExtension extends Extension
|
|||||||
{
|
{
|
||||||
$loader = new YamlFileLoader(
|
$loader = new YamlFileLoader(
|
||||||
$container,
|
$container,
|
||||||
new FileLocator(__DIR__.'/../Resources/config')
|
new FileLocator(dirname(__DIR__).'/Resources/config')
|
||||||
);
|
);
|
||||||
$loader->load('services.yaml');
|
$loader->load('services.yaml');
|
||||||
|
|
||||||
|
$configuration = new Configuration();
|
||||||
|
$config = $this->processConfiguration($configuration, $configs);
|
||||||
|
|
||||||
|
$def = $container->findDefinition('ardent.parameter');
|
||||||
|
$def->setArgument('$config', $config['parameters']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
39
DependencyInjection/Configuration.php
Normal file
39
DependencyInjection/Configuration.php
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace Ardent\ParameterBundle\DependencyInjection;
|
||||||
|
|
||||||
|
|
||||||
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
||||||
|
use Symfony\Component\Config\Definition\ConfigurationInterface;
|
||||||
|
|
||||||
|
class Configuration implements ConfigurationInterface
|
||||||
|
{
|
||||||
|
public const TYPE_TEXT = 'text';
|
||||||
|
public const TYPE_NUMBER = 'number';
|
||||||
|
public const TYPE_BOOL = 'bool';
|
||||||
|
|
||||||
|
public function getConfigTreeBuilder()
|
||||||
|
{
|
||||||
|
$treeBuilder = new TreeBuilder('ardent_parameter');
|
||||||
|
|
||||||
|
$treeBuilder->getRootNode()
|
||||||
|
->children()
|
||||||
|
->arrayNode('parameters')
|
||||||
|
->arrayPrototype()
|
||||||
|
->arrayPrototype()
|
||||||
|
->children()
|
||||||
|
->scalarNode('name')->end()
|
||||||
|
->enumNode('type')
|
||||||
|
->values([self::TYPE_TEXT, self::TYPE_NUMBER, self::TYPE_BOOL])
|
||||||
|
->defaultValue(self::TYPE_TEXT)
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
|
->end();
|
||||||
|
|
||||||
|
return $treeBuilder;
|
||||||
|
}
|
||||||
|
}
|
@ -4,4 +4,9 @@ services:
|
|||||||
|
|
||||||
Ardent\ParameterBundle\Service\ParameterService:
|
Ardent\ParameterBundle\Service\ParameterService:
|
||||||
public: true
|
public: true
|
||||||
|
autowire: true
|
||||||
|
|
||||||
|
Ardent\ParameterBundle\Controller\:
|
||||||
|
resource: '../../Controller'
|
||||||
|
tags: ['controller.service_arguments']
|
||||||
autowire: true
|
autowire: true
|
9
Resources/views/categories.html.twig
Normal file
9
Resources/views/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 %}
|
@ -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 %}
|
@ -2,9 +2,13 @@
|
|||||||
|
|
||||||
namespace Ardent\ParameterBundle\Service;
|
namespace Ardent\ParameterBundle\Service;
|
||||||
|
|
||||||
|
use Ardent\ParameterBundle\DependencyInjection\Configuration;
|
||||||
use Ardent\ParameterBundle\Entity\Parameter;
|
use Ardent\ParameterBundle\Entity\Parameter;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\NumberType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||||
|
|
||||||
class ParameterService
|
class ParameterService
|
||||||
{
|
{
|
||||||
@ -13,23 +17,27 @@ class ParameterService
|
|||||||
|
|
||||||
/** @var ParameterBagInterface */
|
/** @var ParameterBagInterface */
|
||||||
private $parameter;
|
private $parameter;
|
||||||
|
private $config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ParameterService constructor.
|
* ParameterService constructor.
|
||||||
*
|
*
|
||||||
* @param EntityManagerInterface $em
|
* @param EntityManagerInterface $em
|
||||||
|
* @param ParameterBagInterface $parameter
|
||||||
|
* @param $config
|
||||||
*/
|
*/
|
||||||
public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameter)
|
public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameter, $config)
|
||||||
{
|
{
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
$this->parameter = $parameter;
|
$this->parameter = $parameter;
|
||||||
|
$this->config = $this->supplementConfig($config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name The name of the parameter in the format <group>_<parameter>
|
||||||
* @param bool $parse
|
* @param bool $parse Whether to parse embedded %parameter%'s
|
||||||
*
|
*
|
||||||
* @return string
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function get($name, $parse = true)
|
public function get($name, $parse = true)
|
||||||
{
|
{
|
||||||
@ -60,8 +68,8 @@ class ParameterService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param string $name The name of the parameter in the format <group>_<parameter>
|
||||||
* @param $value
|
* @param mixed $value
|
||||||
*/
|
*/
|
||||||
public function set($name, $value)
|
public function set($name, $value)
|
||||||
{
|
{
|
||||||
@ -75,4 +83,45 @@ class ParameterService
|
|||||||
$this->em->persist($parameter);
|
$this->em->persist($parameter);
|
||||||
$this->em->flush();
|
$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;
|
||||||
|
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;
|
||||||
|
unset($value);
|
||||||
|
}
|
||||||
|
unset($group);
|
||||||
|
}
|
||||||
|
return $config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getConfig()
|
||||||
|
{
|
||||||
|
return $this->config;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"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",
|
"doctrine/orm": "^2.0",
|
||||||
"symfony/form": "^5.0",
|
"symfony/form": "^5.0|^6.0",
|
||||||
"symfony/twig-bundle": "^5.0"
|
"symfony/twig-bundle": "^5.0|^6.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
Reference in New Issue
Block a user