Refactor to new bundle style (Symfony v6)

This commit is contained in:
Tim 2023-02-21 00:44:28 +01:00
parent a2fe3584be
commit 7040fd5ea7
14 changed files with 76 additions and 107 deletions

View File

@ -1,13 +0,0 @@
<?php
namespace Ardent\ParameterBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* Bundle.
*/
class ArdentParameterBundle extends Bundle
{
}

View File

@ -1,26 +0,0 @@
<?php
namespace Ardent\ParameterBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class ArdentParameterExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader(
$container,
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

@ -1,39 +0,0 @@
<?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 const TYPE_BOOL = 'bool';
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, self::TYPE_BOOL])
->defaultValue(self::TYPE_TEXT)
->end()
->end()
->end()
->end()
->end()
->end();
return $treeBuilder;
}
}

View File

@ -10,7 +10,7 @@
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"Ardent\\ParameterBundle\\": "" "Ardent\\ParameterBundle\\": "src/"
} }
}, },
"license": "GPL-3.0-only" "license": "GPL-3.0-only"

View File

@ -7,6 +7,6 @@ services:
autowire: true autowire: true
Ardent\ParameterBundle\Controller\: Ardent\ParameterBundle\Controller\:
resource: '../../Controller' resource: '../src/Controller'
tags: ['controller.service_arguments'] tags: ['controller.service_arguments', 'container.service_subscriber']
autowire: true autowire: true

View File

@ -0,0 +1,48 @@
<?php
namespace Ardent\ParameterBundle;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
class ArdentParameterBundle extends AbstractBundle
{
public const TYPE_TEXT = 'text';
public const TYPE_NUMBER = 'number';
public const TYPE_BOOL = 'bool';
public function configure(DefinitionConfigurator $definition): void
{
// $treeBuilder = new TreeBuilder('ardent_parameter');
$definition->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']);
}
}

View File

@ -9,15 +9,11 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
abstract class BaseController extends AbstractController abstract class BaseController extends AbstractController
{ {
/** public function baseIndex(ParameterService $param, Request $request, array $configuration): Response
* @param ParameterService $param
* @param Request $request
* @param [] $configuration
*/
public function baseIndex(ParameterService $param, Request $request, $configuration)
{ {
// gather the values // gather the values
$data = []; $data = [];
@ -51,24 +47,26 @@ abstract class BaseController extends AbstractController
} }
return $this->render('@ArdentParameter/form.html.twig', [ return $this->render('@ArdentParameter/form.html.twig', [
'parameter_form' => $form->createView(), 'parameter_form' => $form->createView(),
]); ]);
} }
/** /**
* @param $config * @param $config
* @param FormBuilderInterface $formBuilder * @param FormBuilderInterface $formBuilder
*/ */
private function configParser($config, &$formBuilder) private function configParser($config, FormBuilderInterface $formBuilder): void
{ {
$type = $config['type']; $type = $config['type'];
$options = []; $options = [];
switch ($type) { switch ($type) {
case ChoiceType::class: case ChoiceType::class:
if (is_callable($config['choices'])) { $choices = $config['choices'];
$options['choices'] = $config['choices']();
if (is_callable($choices)) {
$options['choices'] = $choices();
} else { } else {
$options['choices'] = $config['choices']; $options['choices'] = $choices;
} }
break; break;
case CheckboxType::class: case CheckboxType::class:

View File

@ -6,16 +6,17 @@ namespace Ardent\ParameterBundle\Controller;
use Ardent\ParameterBundle\Service\ParameterService; use Ardent\ParameterBundle\Service\ParameterService;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
class ParameterController extends BaseController class ParameterController extends BaseController
{ {
/** #[Route('/{category}', name: 'parameter_bundle_parameters')]
* @Route("/{category}", name="parameter_bundle_parameters") public function parameters(
*/ ParameterService $param,
public function parameters($category = 'categories', Request $request,
ParameterService $param, ?string $category = 'categories',
Request $request) ): Response
{ {
$configs = $param->getConfig(); $configs = $param->getConfig();

View File

@ -2,7 +2,7 @@
namespace Ardent\ParameterBundle\Service; namespace Ardent\ParameterBundle\Service;
use Ardent\ParameterBundle\DependencyInjection\Configuration; use Ardent\ParameterBundle\ArdentParameterBundle;
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;
@ -15,9 +15,9 @@ class ParameterService
private array $config; private array $config;
public function __construct( public function __construct(
private EntityManagerInterface $em, private readonly EntityManagerInterface $em,
private ParameterBagInterface $parameter, private readonly ParameterBagInterface $parameter,
array $config array $config
) )
{ {
$this->config = $this->supplementConfig($config); $this->config = $this->supplementConfig($config);
@ -79,13 +79,13 @@ class ParameterService
foreach ($rawGroup as &$value) { foreach ($rawGroup as &$value) {
switch ($value['type']) { switch ($value['type']) {
default: default:
case Configuration::TYPE_TEXT: case ArdentParameterBundle::TYPE_TEXT:
$class = TextType::class; $class = TextType::class;
break; break;
case Configuration::TYPE_NUMBER: case ArdentParameterBundle::TYPE_NUMBER:
$class = NumberType::class; $class = NumberType::class;
break; break;
case Configuration::TYPE_BOOL: case ArdentParameterBundle::TYPE_BOOL:
$class = CheckboxType::class; $class = CheckboxType::class;
break; break;
} }