1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JD\FormBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use JD\FormBundle\Form\Type\DateBetweenType; |
6
|
|
|
use JD\FormBundle\Form\Type\DateType; |
7
|
|
|
use Symfony\Component\Config\FileLocator; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
10
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
11
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
12
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
final class JDFormExtension extends ConfigurableExtension |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Configures the passed container according to the merged configuration. |
21
|
|
|
* |
22
|
|
|
* @param array $mergedConfig |
23
|
|
|
* @param ContainerBuilder $container |
24
|
|
|
*/ |
25
|
|
|
protected function loadInternal(array $mergedConfig, ContainerBuilder $container) |
26
|
|
|
{ |
27
|
|
|
if (false === $mergedConfig['enabled']) { |
28
|
|
|
return; |
29
|
|
|
} |
30
|
|
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
31
|
|
|
$loader->load('forms.yml'); |
32
|
|
|
if (true === $mergedConfig['form']['enabled']) { |
33
|
|
|
$this->loadFormTypes($mergedConfig['form'], $container); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $config |
39
|
|
|
* @param ContainerBuilder $container |
40
|
|
|
*/ |
41
|
|
|
private function loadFormTypes(array $config, ContainerBuilder $container) |
42
|
|
|
{ |
43
|
|
|
if (true === $config['array']['enabled']) { |
44
|
|
|
$this->loadArrayFormType($container, $config['array']); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
foreach ($this->getLoaders($config) as $configName => $options) { |
48
|
|
|
if (true === $config[$configName]['enabled']) { |
49
|
|
|
$this->loadFormType($container, $options['class'], $options['arguments'], $options['alias']); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $config |
56
|
|
|
* |
57
|
|
|
* @return string[] |
|
|
|
|
58
|
|
|
*/ |
59
|
|
|
private function getLoaders(array $config) |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
|
|
'date' => [ |
63
|
|
|
'class' => DateType::class, |
64
|
|
|
'arguments' => [ |
65
|
|
|
$config['date']['widget'], |
66
|
|
|
$config['date']['format'], |
67
|
|
|
], |
68
|
|
|
'alias' => 'jd_date', |
69
|
|
|
], |
70
|
|
|
'date_between' => [ |
71
|
|
|
'class' => DateBetweenType::class, |
72
|
|
|
'arguments' => [ |
73
|
|
|
$config['date_between']['from'], |
74
|
|
|
$config['date_between']['to'], |
75
|
|
|
], |
76
|
|
|
'alias' => 'date_between', |
77
|
|
|
], |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param ContainerBuilder $container |
83
|
|
|
* @param string $class |
84
|
|
|
* @param string[] $arguments |
85
|
|
|
* @param string $tagAlias |
86
|
|
|
*/ |
87
|
|
|
private function loadFormType(ContainerBuilder $container, $class, array $arguments, $tagAlias) |
88
|
|
|
{ |
89
|
|
|
$typeDef = new Definition($class); |
90
|
|
|
$typeDef |
91
|
|
|
->setArguments($arguments) |
92
|
|
|
->addTag('form.type', ['alias' => $tagAlias]); |
93
|
|
|
$container->setDefinition('jd_form.form_type.'.$tagAlias, $typeDef); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param array $config |
98
|
|
|
* @param ContainerBuilder $container |
99
|
|
|
*/ |
100
|
|
|
private function loadArrayFormType(ContainerBuilder $container, array $config) |
101
|
|
|
{ |
102
|
|
|
$serviceId = 'jd_form.form_type.array'; |
103
|
|
|
$types = ['hidden', 'text']; |
104
|
|
|
foreach ($types as $type) { |
105
|
|
|
$typeDef = new DefinitionDecorator($serviceId); |
106
|
|
|
$typeDef |
107
|
|
|
->setArguments([$type, $config['delimiter']]) |
108
|
|
|
->addTag('form.type', ['alias' => 'array_'.$type]); |
109
|
|
|
$container->setDefinition($serviceId.'_'.$type, $typeDef); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.