From db970b4c693abb5cacb763eafa0c169dd2b5fdd8 Mon Sep 17 00:00:00 2001 From: ardent Date: Mon, 27 Jul 2020 00:26:57 +0200 Subject: [PATCH] Add config and controller to use the config --- Controller/BaseController.php | 1 + Controller/ParameterController.php | 64 +++++++++++++++++++ .../ArdentParameterExtension.php | 9 ++- DependencyInjection/Configuration.php | 27 +++++++- Resources/config/services.yaml | 5 ++ Service/ParameterService.php | 14 +++- 6 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 Controller/ParameterController.php diff --git a/Controller/BaseController.php b/Controller/BaseController.php index e0f85db..237ee45 100644 --- a/Controller/BaseController.php +++ b/Controller/BaseController.php @@ -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]); } diff --git a/Controller/ParameterController.php b/Controller/ParameterController.php new file mode 100644 index 0000000..61f4d22 --- /dev/null +++ b/Controller/ParameterController.php @@ -0,0 +1,64 @@ +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]); + } + } +} \ No newline at end of file diff --git a/DependencyInjection/ArdentParameterExtension.php b/DependencyInjection/ArdentParameterExtension.php index 243b0d9..67f7dfa 100644 --- a/DependencyInjection/ArdentParameterExtension.php +++ b/DependencyInjection/ArdentParameterExtension.php @@ -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']); } } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 5605bb6..259dcc0 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -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; } } \ No newline at end of file diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index f3dd062..cee520a 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -4,4 +4,9 @@ services: Ardent\ParameterBundle\Service\ParameterService: public: true + autowire: true + + Ardent\ParameterBundle\Controller\: + resource: '../../Controller' + tags: ['controller.service_arguments'] autowire: true \ No newline at end of file diff --git a/Service/ParameterService.php b/Service/ParameterService.php index 0d4e0ff..a3d257f 100644 --- a/Service/ParameterService.php +++ b/Service/ParameterService.php @@ -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; + } }