|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Admin\ActionInterface; |
|
6
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
|
7
|
|
|
use LAG\AdminBundle\Configuration\AdminConfiguration; |
|
8
|
|
|
use LAG\AdminBundle\DataProvider\DataProviderInterface; |
|
9
|
|
|
use LAG\AdminBundle\Event\Events; |
|
10
|
|
|
use LAG\AdminBundle\Event\Events\FormEvent; |
|
11
|
|
|
use LAG\AdminBundle\Exception\Exception; |
|
12
|
|
|
use LAG\AdminBundle\Field\Definition\FieldDefinitionInterface; |
|
13
|
|
|
use LAG\AdminBundle\Utils\FormUtils; |
|
14
|
|
|
use Symfony\Component\Form\CallbackTransformer; |
|
15
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
|
16
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
17
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
18
|
|
|
use Symfony\Component\Form\FormInterface; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
21
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
|
22
|
|
|
|
|
23
|
|
|
class FormFactory implements \LAG\AdminBundle\Factory\FormFactoryInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var DataProviderFactory |
|
27
|
|
|
*/ |
|
28
|
|
|
private $dataProviderFactory; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var FormFactoryInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $formFactory; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var EventDispatcherInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $eventDispatcher; |
|
39
|
|
|
|
|
40
|
6 |
|
public function __construct( |
|
41
|
|
|
DataProviderFactory $dataProviderFactory, |
|
42
|
|
|
FormFactoryInterface $formFactory, |
|
43
|
|
|
EventDispatcherInterface $eventDispatcher |
|
44
|
|
|
) { |
|
45
|
6 |
|
$this->dataProviderFactory = $dataProviderFactory; |
|
46
|
6 |
|
$this->formFactory = $formFactory; |
|
47
|
6 |
|
$this->eventDispatcher = $eventDispatcher; |
|
48
|
6 |
|
} |
|
49
|
|
|
|
|
50
|
4 |
|
public function createEntityForm(AdminInterface $admin, Request $request, $entity = null): FormInterface |
|
51
|
|
|
{ |
|
52
|
4 |
|
$dataProvider = $this->getDataProvider($admin->getConfiguration()); |
|
53
|
|
|
|
|
54
|
4 |
|
if (null === $entity) { |
|
55
|
4 |
|
$entity = $dataProvider->create($admin); |
|
56
|
|
|
} |
|
57
|
4 |
|
$formType = $admin->getConfiguration()->get('form'); |
|
58
|
|
|
|
|
59
|
4 |
|
if (null === $formType) { |
|
60
|
|
|
$builder = $this |
|
61
|
2 |
|
->formFactory |
|
62
|
2 |
|
->createBuilder(FormType::class, $entity, [ |
|
63
|
2 |
|
'label' => false, |
|
64
|
|
|
]) |
|
65
|
|
|
; |
|
66
|
2 |
|
$event = new FormEvent($admin, $request); |
|
67
|
2 |
|
$this->eventDispatcher->dispatch($event, Events::FORM_PRE_CREATE_ENTITY_FORM); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
2 |
|
foreach ($event->getFieldDefinitions() as $field => $definition) { |
|
70
|
|
|
if (!$definition instanceof FieldDefinitionInterface) { |
|
71
|
|
|
throw new Exception('The field definition should implements "'.FieldDefinitionInterface::class.'", got "'.gettype($definition)); |
|
72
|
|
|
} |
|
73
|
|
|
// Usually we do not want to edit those values in a Form |
|
74
|
|
|
if (in_array($field, [ |
|
75
|
|
|
'createdAt', |
|
76
|
|
|
'updatedAt', |
|
77
|
|
|
]) && 'datetime' === $definition->getType()) { |
|
78
|
|
|
continue; |
|
79
|
|
|
} |
|
80
|
|
|
$formType = FormUtils::convertShortFormType($definition->getType()); |
|
81
|
|
|
$formOptions = array_merge( |
|
82
|
|
|
FormUtils::getFormTypeOptions($definition->getType()), |
|
83
|
|
|
$definition->getFormOptions() |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
$builder->add($field, $formType, $formOptions); |
|
87
|
|
|
$this->addFormTransformers($builder, $field, $definition->getType()); |
|
88
|
|
|
} |
|
89
|
2 |
|
$form = $builder->getForm(); |
|
90
|
|
|
} else { |
|
91
|
|
|
$form = $this |
|
92
|
2 |
|
->formFactory |
|
93
|
2 |
|
->create($formType, $entity) |
|
94
|
|
|
; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
4 |
|
return $form; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
public function createDeleteForm(ActionInterface $action, Request $request, $entity): FormInterface |
|
101
|
|
|
{ |
|
102
|
|
|
$form = $this |
|
103
|
2 |
|
->formFactory |
|
104
|
2 |
|
->create($action->getConfiguration()->get('form'), $entity) |
|
105
|
|
|
; |
|
106
|
|
|
|
|
107
|
2 |
|
return $form; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
4 |
|
private function getDataProvider(AdminConfiguration $configuration): DataProviderInterface |
|
111
|
|
|
{ |
|
112
|
|
|
return $this |
|
113
|
4 |
|
->dataProviderFactory |
|
114
|
4 |
|
->get($configuration->get('data_provider')) |
|
115
|
|
|
; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
private function addFormTransformers(FormBuilderInterface $builder, string $field, ?string $type): void |
|
119
|
|
|
{ |
|
120
|
|
|
if ('array' === $type) { |
|
121
|
|
|
$builder |
|
122
|
|
|
->get($field) |
|
123
|
|
|
->addModelTransformer(new CallbackTransformer(function (?array $value = null) { |
|
124
|
|
|
if (null === $value) { |
|
125
|
|
|
$value = []; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return Yaml::dump($value); |
|
129
|
|
|
}, function ($value) { |
|
130
|
|
|
if (null === $value) { |
|
131
|
|
|
return []; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return Yaml::parse($value); |
|
135
|
|
|
})) |
|
136
|
|
|
; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if ('simple_array' === $type) { |
|
140
|
|
|
$builder |
|
141
|
|
|
->get($field) |
|
142
|
|
|
->addModelTransformer(new CallbackTransformer(function (?array $value = null) { |
|
143
|
|
|
if (null === $value) { |
|
144
|
|
|
$value = []; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return implode(',', $value); |
|
148
|
|
|
}, function ($value) { |
|
149
|
|
|
if (null === $value) { |
|
150
|
|
|
return []; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return explode(',', $value); |
|
154
|
|
|
})) |
|
155
|
|
|
; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.