6 Commits

Author SHA1 Message Date
d20f7401c1 Add categories overview 2020-10-23 23:39:45 +02:00
69abbcaf4b Improve phpdocs of service 2020-07-27 01:23:13 +02:00
6c43ee8fd1 Cleanup config and fix rendering 2020-07-27 01:02:18 +02:00
db970b4c69 Add config and controller to use the config 2020-07-27 00:26:57 +02:00
a2c17d4be3 Fix wording 2020-07-26 03:23:06 +02:00
28b21b8e12 Start on config parser 2020-07-26 03:21:34 +02:00
10 changed files with 155 additions and 10 deletions

View File

@ -40,6 +40,7 @@ abstract class BaseController extends AbstractController
foreach ($configuration as $config) {
$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']) {
$param->set($name, $result[$name]);
}

View 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]);
}
}
}

View File

@ -2,7 +2,6 @@
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;
@ -14,8 +13,14 @@ class ArdentParameterExtension extends Extension
{
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__.'/../Resources/config')
new FileLocator(dirname(__DIR__).'/Resources/config')
);
$loader->load('services.yaml');
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$def = $container->findDefinition('ardent.parameter');
$def->setArgument('$config', $config['parameters']);
}
}

View File

@ -0,0 +1,38 @@
<?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 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])
->defaultValue(self::TYPE_TEXT)
->end()
->end()
->end()
->end()
->end()
->end();
return $treeBuilder;
}
}

View File

@ -4,4 +4,9 @@ services:
Ardent\ParameterBundle\Service\ParameterService:
public: true
autowire: true
Ardent\ParameterBundle\Controller\:
resource: '../../Controller'
tags: ['controller.service_arguments']
autowire: true

View 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 %}

View File

@ -0,0 +1 @@
{# Add extra content on the bottom of the form #}

View File

@ -1,8 +1,8 @@
{% extends '@ArdentParameter/layout.html.twig' %}
{% block par_user_content %}
<a class="btn-primary btn" href="{{ path('parameter_bundle_parameters', {category:'categories'}) }}">Back</a> <br><br>
{{ form(parameter_form) }}
{% include '@ArdentParameter/form.extra.html.twig' %}
{% endblock %}

View File

@ -2,9 +2,12 @@
namespace Ardent\ParameterBundle\Service;
use Ardent\ParameterBundle\DependencyInjection\Configuration;
use Ardent\ParameterBundle\Entity\Parameter;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class ParameterService
{
@ -13,23 +16,27 @@ class ParameterService
/** @var ParameterBagInterface */
private $parameter;
private $config;
/**
* ParameterService constructor.
*
* @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->parameter = $parameter;
$this->config = $this->supplementConfig($config);
}
/**
* @param string $name
* @param bool $parse
* @param string $name The name of the parameter in the format <group>_<parameter>
* @param bool $parse Whether to parse embedded %parameter%'s
*
* @return string
* @return mixed
*/
public function get($name, $parse = true)
{
@ -60,8 +67,8 @@ class ParameterService
}
/**
* @param $name
* @param $value
* @param string $name The name of the parameter in the format <group>_<parameter>
* @param mixed $value
*/
public function set($name, $value)
{
@ -75,4 +82,42 @@ class ParameterService
$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;
}
}

View File

@ -1,6 +1,6 @@
{
"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",
"require": {
"doctrine/orm": "^2.7",