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": {
"psr-4": {
"Ardent\\ParameterBundle\\": ""
"Ardent\\ParameterBundle\\": "src/"
}
},
"license": "GPL-3.0-only"

View File

@ -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

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\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:

View File

@ -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();

View File

@ -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;
}