Add config and controller to use the config

This commit is contained in:
Tim 2020-07-27 00:26:57 +02:00
parent a2c17d4be3
commit db970b4c69
6 changed files with 115 additions and 5 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,64 @@
<?php
namespace Ardent\ParameterBundle\Controller;
use Ardent\ParameterBundle\Service\ParameterService;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class ParameterController extends BaseController
{
/**
* @Route("/{name}", name="parameter_bundle_parameters")
*/
public function parameters($name,
ParameterService $param,
Request $request)
{
$config = $param->getConfig();
dump($config);
$configs = [
'transmission' => [
['name' => 'transmission_url', 'type' => TextType::class],
['name' => 'transmission_port', 'type' => NumberType::class],
['name' => 'transmission_username', 'type' => TextType::class],
['name' => 'transmission_password', 'type' => TextType::class],
['name' => 'transmission_path_local', 'type' => TextType::class],
['name' => 'transmission_path_remote', 'type' => TextType::class],
],
'animerss' => [
['name' => 'animerss_apikey', 'type' => TextType::class],
['name' => 'animerss_baseurl', 'type' => TextType::class],
],
'misc' => [
['name' => 'rss_url', 'type' => TextType::class],
['name' => 'torrent_quality', 'type' => TextType::class],
],
'all' => [], // Placeholder, lists all above values, it is captured a bit later
];
// Handle all special cases for the name
/*if ('list' === $name) { // List all the categories
$routes = [];
foreach ($configs as $key => $config) {
$routes[] = ['route' => 'parameters', 'params' => ['name' => $key], 'title' => $key];
}
return $this->render('list.routes.twig', ['routes' => $routes]);
} else*/if ('all' === $name) { // 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[$name]);
}
}
}

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

@ -9,12 +9,35 @@ 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('parameter_bundle');
$treeBuilder = new TreeBuilder('ardent_parameter');
$treeBuilder->getRootNode()
->children()
->end();
->arrayNode('parameters')
->arrayPrototype()
->children()
->scalarNode('group')->defaultValue('misc')->end()
->arrayNode('values')
->arrayPrototype()
->children()
->scalarNode('name')->end()
->enumNode('type')
->values([self::TYPE_TEXT, self::TYPE_NUMBER])
->defaultValue(self::TYPE_TEXT)
->end()
->end()
->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

@ -13,16 +13,20 @@ 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 = $config;
}
/**
@ -75,4 +79,12 @@ class ParameterService
$this->em->persist($parameter);
$this->em->flush();
}
/**
* @return array
*/
public function getConfig()
{
return $this->config;
}
}