1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OneGuard DynamicConfigurationBundle. |
5
|
|
|
* |
6
|
|
|
* (c) OneGuard <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OneGuard\Bundle\DynamicConfigurationBundle\Form; |
13
|
|
|
|
14
|
|
|
use OneGuard\Bundle\DynamicConfigurationBundle\DefinitionRegistry; |
15
|
|
|
use OneGuard\Bundle\DynamicConfigurationBundle\Entity\ConfigurationValue; |
16
|
|
|
use OneGuard\Bundle\DynamicConfigurationBundle\EntityDefinition; |
17
|
|
|
use Symfony\Component\Form\AbstractType; |
18
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
19
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
20
|
|
|
use Symfony\Component\Form\FormEvent; |
21
|
|
|
use Symfony\Component\Form\FormEvents; |
22
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
23
|
|
|
|
24
|
|
|
class ConfigurationValueType extends AbstractType { |
25
|
|
|
/** |
26
|
|
|
* @var DefinitionRegistry |
27
|
|
|
*/ |
28
|
|
|
private $registry; |
29
|
|
|
|
30
|
|
|
private $translationDomain; |
31
|
|
|
private $translationPrefix; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
DefinitionRegistry $registry, |
35
|
|
|
string $translationDomain, |
36
|
|
|
string $translationPrefix |
37
|
|
|
) { |
38
|
|
|
$this->registry = $registry; |
39
|
|
|
$this->translationDomain = $translationDomain; |
40
|
|
|
$this->translationPrefix = $translationPrefix; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) { |
44
|
|
|
$builder |
45
|
|
|
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { |
46
|
|
|
/* @var $configurationValue ConfigurationValue */ |
47
|
|
|
$configurationValue = $event->getData(); |
48
|
|
|
$definition = $this->registry->get($configurationValue->getKey()); |
49
|
|
|
switch (get_class($definition)) { |
50
|
|
|
case EntityDefinition::class: |
51
|
|
|
/* @var $definition EntityDefinition */ |
52
|
|
|
$form = $event->getForm(); |
53
|
|
|
$form->add('value', EntityConfigurationValueType::class, [ |
54
|
|
|
'class' => $definition->getClass(), |
55
|
|
|
'choice_label' => $definition->getChoiceLabel(), |
56
|
|
|
'label' => $this->translationPrefix . $configurationValue->getKey(), |
57
|
|
|
'translation_domain' => $this->translationDomain, |
58
|
|
|
'placeholder' => 'Please choose', |
59
|
|
|
'empty_data' => null, |
60
|
|
|
'required' => false |
61
|
|
|
]); |
62
|
|
|
break; |
63
|
|
|
default: // assume StringDefinition |
64
|
|
|
$event->getForm()->add('value', TextType::class, [ |
65
|
|
|
'label' => $this->translationPrefix . $configurationValue->getKey(), |
66
|
|
|
'translation_domain' => $this->translationDomain, |
67
|
|
|
'required' => false |
68
|
|
|
]); |
69
|
|
|
} |
70
|
|
|
}); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function configureOptions(OptionsResolver $resolver) { |
74
|
|
|
$resolver->setDefault('data_class', ConfigurationValue::class); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|