Cleanup config and fix rendering

This commit is contained in:
Tim 2020-07-27 01:02:18 +02:00
parent db970b4c69
commit 6c43ee8fd1
3 changed files with 45 additions and 41 deletions

View File

@ -5,42 +5,19 @@ namespace Ardent\ParameterBundle\Controller;
use Ardent\ParameterBundle\Service\ParameterService; 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\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
class ParameterController extends BaseController class ParameterController extends BaseController
{ {
/** /**
* @Route("/{name}", name="parameter_bundle_parameters") * @Route("/{category}", name="parameter_bundle_parameters")
*/ */
public function parameters($name, public function parameters($category,
ParameterService $param, ParameterService $param,
Request $request) Request $request)
{ {
$config = $param->getConfig(); $configs = $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 // Handle all special cases for the name
/*if ('list' === $name) { // List all the categories /*if ('list' === $name) { // List all the categories
@ -50,7 +27,7 @@ class ParameterController extends BaseController
} }
return $this->render('list.routes.twig', ['routes' => $routes]); return $this->render('list.routes.twig', ['routes' => $routes]);
} else*/if ('all' === $name) { // Show all parameters from all categories } else*/if ('all' === $category) { // Show all parameters from all categories
$allConfigs = []; $allConfigs = [];
foreach ($configs as $config) { foreach ($configs as $config) {
$allConfigs = array_merge($allConfigs, $config); $allConfigs = array_merge($allConfigs, $config);
@ -58,7 +35,7 @@ class ParameterController extends BaseController
return parent::baseIndex($param, $request, $allConfigs); return parent::baseIndex($param, $request, $allConfigs);
} else { // Show the parameters from one category } else { // Show the parameters from one category
return parent::baseIndex($param, $request, $configs[$name]); return parent::baseIndex($param, $request, $configs[$category]);
} }
} }
} }

View File

@ -20,17 +20,12 @@ class Configuration implements ConfigurationInterface
->children() ->children()
->arrayNode('parameters') ->arrayNode('parameters')
->arrayPrototype() ->arrayPrototype()
->children() ->arrayPrototype()
->scalarNode('group')->defaultValue('misc')->end() ->children()
->arrayNode('values') ->scalarNode('name')->end()
->arrayPrototype() ->enumNode('type')
->children() ->values([self::TYPE_TEXT, self::TYPE_NUMBER])
->scalarNode('name')->end() ->defaultValue(self::TYPE_TEXT)
->enumNode('type')
->values([self::TYPE_TEXT, self::TYPE_NUMBER])
->defaultValue(self::TYPE_TEXT)
->end()
->end()
->end() ->end()
->end() ->end()
->end() ->end()

View File

@ -2,9 +2,12 @@
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\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class ParameterService class ParameterService
{ {
@ -26,12 +29,12 @@ class ParameterService
{ {
$this->em = $em; $this->em = $em;
$this->parameter = $parameter; $this->parameter = $parameter;
$this->config = $config; $this->config = $this->supplementConfig($config);
} }
/** /**
* @param string $name * @param string $name
* @param bool $parse * @param bool $parse
* *
* @return string * @return string
*/ */
@ -80,6 +83,35 @@ class ParameterService
$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;
}
$value['name'] = sprintf('%s_%s', $groupName, $value['name']);
$value['type'] = $class;
unset($value);
}
unset($group);
}
return $config;
}
/** /**
* @return array * @return array
*/ */