Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle field types with strategy #4

Merged
merged 1 commit into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions DependencyInjection/Compiler/FieldTypePass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Sherlockode\ConfigurationBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class FieldTypePass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$definition = $container->findDefinition('sherlockode_configuration.field_manager');
$taggedServices = $container->findTaggedServiceIds('sherlockode_configuration.field');

foreach ($taggedServices as $id => $tags) {
$definition->addMethodCall('addFieldType', [new Reference($id)]);
}
}
}
1 change: 1 addition & 0 deletions DependencyInjection/SherlockodeConfigurationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ public function load(array $configs, ContainerBuilder $container)
$fileLocator = new FileLocator(__DIR__ . '/../Resources/config');
$loader = new YamlFileLoader($container, $fileLocator);
$loader->load('services.yml');
$loader->load('field_types.yml');
}
}
21 changes: 21 additions & 0 deletions FieldType/FieldTypeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Sherlockode\ConfigurationBundle\FieldType;

interface FieldTypeInterface
{
/**
* @return string
*/
public function getFormType();

/**
* @return array
*/
public function getFormOptions();

/**
* @return string
*/
public function getName();
}
26 changes: 26 additions & 0 deletions FieldType/TextField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Sherlockode\ConfigurationBundle\FieldType;

use Symfony\Component\Form\Extension\Core\Type\TextType;

class TextField implements FieldTypeInterface
{
/**
* @return string
*/
public function getFormType()
{
return TextType::class;
}

public function getFormOptions()
{
return [];
}

public function getName()
{
return 'text';
}
}
30 changes: 17 additions & 13 deletions Form/Type/ParameterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Sherlockode\ConfigurationBundle\Form\Type;

use Sherlockode\ConfigurationBundle\Manager\FieldTypeManager;
use Sherlockode\ConfigurationBundle\Model\ParameterInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand All @@ -19,11 +20,18 @@ class ParameterType extends AbstractType
private $class;

/**
* @param string $class
* @var FieldTypeManager
*/
public function __construct($class)
private $fieldTypeManager;

/**
* @param string $class
* @param FieldTypeManager $fieldTypeManager
*/
public function __construct($class, FieldTypeManager $fieldTypeManager)
{
$this->class = $class;
$this->fieldTypeManager = $fieldTypeManager;
}

public function buildForm(FormBuilderInterface $builder, array $options)
Expand All @@ -39,6 +47,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder->addViewTransformer(new CallbackTransformer(function ($data) {
return $data;
}, function ($data) use ($builder) {
/** @var ParameterInterface $data */
$data->setPath($builder->getName());

return $data;
Expand All @@ -54,19 +63,14 @@ public function configureOptions(OptionsResolver $resolver)
/**
* @param string $type
*
* @return string
* @throws \Exception
* @return array
*/
private function getFormConfiguration($type)
{
$availableTypes = [
'text' => ['type' => TextType::class, 'options' => []],
$field = $this->fieldTypeManager->getField($type);
return [
'type' => $field->getFormType(),
'options' => $field->getFormOptions(),
];

if (!isset($availableTypes[$type])) {
throw new \Exception(sprintf('Unknown parameter type "%s"', $type));
}

return $availableTypes[$type];
}
}
45 changes: 45 additions & 0 deletions Manager/FieldTypeManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Sherlockode\ConfigurationBundle\Manager;

use Sherlockode\ConfigurationBundle\FieldType\FieldTypeInterface;

class FieldTypeManager
{
/**
* @var FieldTypeInterface[]
*/
private $fieldTypes;

public function __construct()
{
$this->fieldTypes = [];
}

/**
* @param FieldTypeInterface $fieldType
*
* @return $this
*/
public function addFieldType(FieldTypeInterface $fieldType)
{
$this->fieldTypes[$fieldType->getName()] = $fieldType;

return $this;
}

/**
* @param string $type
*
* @return FieldTypeInterface
* @throws \Exception
*/
public function getField($type)
{
if (!isset($this->fieldTypes[$type])) {
throw new \Exception(sprintf('Unknown parameter type "%s"', $type));
}

return $this->fieldTypes[$type];
}
}
5 changes: 5 additions & 0 deletions Resources/config/field_types.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
sherlockode_configuration.field.text:
class: Sherlockode\ConfigurationBundle\FieldType\TextField
tags:
- {name: sherlockode_configuration.field }
4 changes: 4 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ services:
- '@doctrine.orm.entity_manager'
- '%sherlockode_configuration.parameter_class%'
- '%sherlockode_configuration.parameters%'
sherlockode_configuration.field_manager:
class: Sherlockode\ConfigurationBundle\Manager\FieldTypeManager
sherlockode_configuration.form.parameters:
class: Sherlockode\ConfigurationBundle\Form\Type\ParametersType
arguments:
Expand All @@ -21,7 +23,9 @@ services:
class: Sherlockode\ConfigurationBundle\Form\Type\ParameterType
arguments:
- '%sherlockode_configuration.parameter_class%'
- '@sherlockode_configuration.field_manager'
tags:
- {name: form.type }

Sherlockode\ConfigurationBundle\Manager\ParameterManager: '@sherlockode_configuration.parameter_manager'
Sherlockode\ConfigurationBundle\Manager\FieldTypeManager: '@sherlockode_configuration.field_manager'
7 changes: 6 additions & 1 deletion SherlockodeConfigurationBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

namespace Sherlockode\ConfigurationBundle;

use Sherlockode\ConfigurationBundle\DependencyInjection\Compiler\FieldTypePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class SherlockodeConfigurationBundle extends Bundle
{

public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new FieldTypePass());
}
}