diff --git a/ArdentParameterBundle.php b/ArdentParameterBundle.php deleted file mode 100644 index b859284..0000000 --- a/ArdentParameterBundle.php +++ /dev/null @@ -1,13 +0,0 @@ -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 deleted file mode 100644 index 04be94a..0000000 --- a/DependencyInjection/Configuration.php +++ /dev/null @@ -1,39 +0,0 @@ -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; - } -} \ No newline at end of file diff --git a/composer.json b/composer.json index 84edeb8..0b1d43b 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ }, "autoload": { "psr-4": { - "Ardent\\ParameterBundle\\": "" + "Ardent\\ParameterBundle\\": "src/" } }, "license": "GPL-3.0-only" diff --git a/Resources/config/services.yaml b/config/services.yaml similarity index 62% rename from Resources/config/services.yaml rename to config/services.yaml index cee520a..ca13a4e 100644 --- a/Resources/config/services.yaml +++ b/config/services.yaml @@ -7,6 +7,6 @@ services: autowire: true Ardent\ParameterBundle\Controller\: - resource: '../../Controller' - tags: ['controller.service_arguments'] + resource: '../src/Controller' + tags: ['controller.service_arguments', 'container.service_subscriber'] autowire: true \ No newline at end of file diff --git a/src/ArdentParameterBundle.php b/src/ArdentParameterBundle.php new file mode 100644 index 0000000..0129490 --- /dev/null +++ b/src/ArdentParameterBundle.php @@ -0,0 +1,48 @@ +rootNode() + ->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(); + } + + public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void + { + $container->import('../config/services.yaml'); + + // Retrieves the parameters from the config and injects them info the ardent.parameter service + $def = $builder->findDefinition('ardent.parameter'); + $def->setArgument('$config', $config['parameters']); + } +} diff --git a/Controller/BaseController.php b/src/Controller/BaseController.php similarity index 81% rename from Controller/BaseController.php rename to src/Controller/BaseController.php index 83c5c61..773e87d 100644 --- a/Controller/BaseController.php +++ b/src/Controller/BaseController.php @@ -9,15 +9,11 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; abstract class BaseController extends AbstractController { - /** - * @param ParameterService $param - * @param Request $request - * @param [] $configuration - */ - public function baseIndex(ParameterService $param, Request $request, $configuration) + public function baseIndex(ParameterService $param, Request $request, array $configuration): Response { // gather the values $data = []; @@ -51,24 +47,26 @@ abstract class BaseController extends AbstractController } return $this->render('@ArdentParameter/form.html.twig', [ - 'parameter_form' => $form->createView(), - ]); + 'parameter_form' => $form->createView(), + ]); } /** - * @param $config + * @param $config * @param FormBuilderInterface $formBuilder */ - private function configParser($config, &$formBuilder) + private function configParser($config, FormBuilderInterface $formBuilder): void { $type = $config['type']; $options = []; switch ($type) { case ChoiceType::class: - if (is_callable($config['choices'])) { - $options['choices'] = $config['choices'](); + $choices = $config['choices']; + + if (is_callable($choices)) { + $options['choices'] = $choices(); } else { - $options['choices'] = $config['choices']; + $options['choices'] = $choices; } break; case CheckboxType::class: diff --git a/Controller/ParameterController.php b/src/Controller/ParameterController.php similarity index 80% rename from Controller/ParameterController.php rename to src/Controller/ParameterController.php index 05b642f..c982627 100644 --- a/Controller/ParameterController.php +++ b/src/Controller/ParameterController.php @@ -6,16 +6,17 @@ namespace Ardent\ParameterBundle\Controller; use Ardent\ParameterBundle\Service\ParameterService; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; 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) + #[Route('/{category}', name: 'parameter_bundle_parameters')] + public function parameters( + ParameterService $param, + Request $request, + ?string $category = 'categories', + ): Response { $configs = $param->getConfig(); diff --git a/Entity/Parameter.php b/src/Entity/Parameter.php similarity index 100% rename from Entity/Parameter.php rename to src/Entity/Parameter.php diff --git a/Service/ParameterService.php b/src/Service/ParameterService.php similarity index 89% rename from Service/ParameterService.php rename to src/Service/ParameterService.php index 99a9b4c..3366bcd 100644 --- a/Service/ParameterService.php +++ b/src/Service/ParameterService.php @@ -2,7 +2,7 @@ namespace Ardent\ParameterBundle\Service; -use Ardent\ParameterBundle\DependencyInjection\Configuration; +use Ardent\ParameterBundle\ArdentParameterBundle; use Ardent\ParameterBundle\Entity\Parameter; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; @@ -15,9 +15,9 @@ class ParameterService private array $config; public function __construct( - private EntityManagerInterface $em, - private ParameterBagInterface $parameter, - array $config + private readonly EntityManagerInterface $em, + private readonly ParameterBagInterface $parameter, + array $config ) { $this->config = $this->supplementConfig($config); @@ -79,13 +79,13 @@ class ParameterService foreach ($rawGroup as &$value) { switch ($value['type']) { default: - case Configuration::TYPE_TEXT: + case ArdentParameterBundle::TYPE_TEXT: $class = TextType::class; break; - case Configuration::TYPE_NUMBER: + case ArdentParameterBundle::TYPE_NUMBER: $class = NumberType::class; break; - case Configuration::TYPE_BOOL: + case ArdentParameterBundle::TYPE_BOOL: $class = CheckboxType::class; break; } diff --git a/Resources/views/categories.html.twig b/templates/categories.html.twig similarity index 100% rename from Resources/views/categories.html.twig rename to templates/categories.html.twig diff --git a/Resources/views/form.extra.html.twig b/templates/form.extra.html.twig similarity index 100% rename from Resources/views/form.extra.html.twig rename to templates/form.extra.html.twig diff --git a/Resources/views/form.html.twig b/templates/form.html.twig similarity index 100% rename from Resources/views/form.html.twig rename to templates/form.html.twig diff --git a/Resources/views/layout.html.twig b/templates/layout.html.twig similarity index 100% rename from Resources/views/layout.html.twig rename to templates/layout.html.twig