1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Mutation; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
14
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
15
|
|
|
use Symfony\Component\Form\FormEvent; |
16
|
|
|
use Symfony\Component\Form\FormEvents; |
17
|
|
|
use Symfony\Component\Form\FormInterface; |
18
|
|
|
use Symfony\Component\Validator\ConstraintViolation as SymfonyConstraintViolation; |
19
|
|
|
use Ynlo\GraphQLBundle\Events\GraphQLEvents; |
20
|
|
|
use Ynlo\GraphQLBundle\Events\GraphQLMutationEvent; |
21
|
|
|
use Ynlo\GraphQLBundle\Model\ConstraintViolation; |
22
|
|
|
use Ynlo\GraphQLBundle\Model\ID; |
23
|
|
|
use Ynlo\GraphQLBundle\Resolver\AbstractResolver; |
24
|
|
|
use Ynlo\GraphQLBundle\Validator\ConstraintViolationList; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Base class for mutations |
28
|
|
|
* Implement the method "process()" and "returnPayload()" is enough in many scenarios |
29
|
|
|
*/ |
30
|
|
|
abstract class AbstractMutationResolver extends AbstractResolver implements EventSubscriberInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @param array $input |
34
|
|
|
* |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
8 |
|
public function __invoke($input) |
38
|
|
|
{ |
39
|
8 |
|
$formBuilder = $this->createDefinitionForm($this->initialFormData($input)); |
40
|
8 |
|
$mutationEvent = null; |
41
|
|
|
|
42
|
8 |
|
$form = null; |
43
|
8 |
|
if ($formBuilder) { |
44
|
8 |
|
$formBuilder->addEventListener( |
45
|
8 |
|
FormEvents::SUBMIT, |
46
|
8 |
|
function (FormEvent $event) use (&$mutationEvent) { |
47
|
8 |
|
if ($this->eventDispatcher) { |
48
|
8 |
|
$mutationEvent = new GraphQLMutationEvent($this->context, $event); |
49
|
8 |
|
$this->eventDispatcher->dispatch(GraphQLEvents::MUTATION_SUBMITTED, $mutationEvent); |
50
|
|
|
} |
51
|
8 |
|
} |
52
|
|
|
); |
53
|
|
|
|
54
|
8 |
|
$formBuilder->addEventSubscriber($this); |
55
|
|
|
|
56
|
|
|
$extensionExecutor = function ($method) { |
57
|
8 |
|
return function (FormEvent $event) use ($method) { |
58
|
8 |
|
foreach ($this->extensions as $extension) { |
59
|
4 |
|
return call_user_func_array([$extension, $method], [$event]); |
60
|
|
|
} |
61
|
8 |
|
}; |
62
|
8 |
|
}; |
63
|
|
|
|
64
|
8 |
|
foreach (self::getSubscribedEvents() as $event => $method) { |
65
|
8 |
|
$formBuilder->addEventListener($event, $extensionExecutor($method)); |
66
|
|
|
} |
67
|
|
|
|
68
|
8 |
|
$form = $formBuilder->getForm(); |
69
|
|
|
} |
70
|
|
|
|
71
|
8 |
|
if ($form) { |
72
|
8 |
|
$form->submit($input, false); |
73
|
8 |
|
$data = $form->getData(); |
74
|
|
|
} else { |
75
|
|
|
$data = $input; |
76
|
|
|
} |
77
|
|
|
|
78
|
8 |
|
$violations = new ConstraintViolationList(); |
79
|
8 |
|
if ($form) { |
80
|
8 |
|
$this->extractFormErrors($form, $violations); |
81
|
|
|
} |
82
|
|
|
|
83
|
8 |
|
$dryRun = $input['dryRun'] ?? false; |
84
|
|
|
|
85
|
8 |
|
if ($dryRun) { |
86
|
|
|
$data = null; |
87
|
|
|
} else { |
88
|
8 |
|
if ((!$form && !$violations->count()) |
89
|
8 |
|
|| ($form->isSubmitted() && $form->isValid() && !$violations->count()) |
90
|
|
|
) { |
91
|
7 |
|
$this->process($data); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
8 |
|
$payload = $this->returnPayload($data, $violations, $input); |
96
|
|
|
|
97
|
8 |
|
if ($mutationEvent instanceof GraphQLMutationEvent) { |
98
|
8 |
|
$mutationEvent->setPayload($payload); |
99
|
8 |
|
$this->eventDispatcher->dispatch(GraphQLEvents::MUTATION_COMPLETED, $mutationEvent); |
100
|
8 |
|
$payload = $mutationEvent->getPayload(); |
101
|
|
|
} |
102
|
|
|
|
103
|
8 |
|
return $payload; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
8 |
|
public static function getSubscribedEvents() |
110
|
|
|
{ |
111
|
|
|
return [ |
112
|
8 |
|
FormEvents::PRE_SET_DATA => 'preSetData', |
113
|
8 |
|
FormEvents::POST_SET_DATA => 'postSetData', |
114
|
8 |
|
FormEvents::PRE_SUBMIT => 'preSubmit', |
115
|
8 |
|
FormEvents::SUBMIT => 'onSubmit', |
116
|
8 |
|
FormEvents::POST_SUBMIT => 'postSubmit', |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html |
122
|
|
|
* |
123
|
|
|
* @param FormEvent $event |
124
|
|
|
*/ |
125
|
8 |
|
public function preSetData(FormEvent $event) |
126
|
|
|
{ |
127
|
8 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html |
131
|
|
|
* |
132
|
|
|
* @param FormEvent $event |
133
|
|
|
*/ |
134
|
8 |
|
public function postSetData(FormEvent $event) |
135
|
|
|
{ |
136
|
8 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html |
140
|
|
|
* |
141
|
|
|
* @param FormEvent $event |
142
|
|
|
*/ |
143
|
8 |
|
public function preSubmit(FormEvent $event) |
144
|
|
|
{ |
145
|
8 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html |
149
|
|
|
* |
150
|
|
|
* @param FormEvent $event |
151
|
|
|
*/ |
152
|
4 |
|
public function onSubmit(FormEvent $event) |
153
|
|
|
{ |
154
|
4 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html |
158
|
|
|
* |
159
|
|
|
* @param FormEvent $event |
160
|
|
|
*/ |
161
|
8 |
|
public function postSubmit(FormEvent $event) |
162
|
|
|
{ |
163
|
8 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Actions to process |
167
|
|
|
* the result processed data is given to payload |
168
|
|
|
* |
169
|
|
|
* @param mixed $data |
170
|
|
|
*/ |
171
|
|
|
abstract public function process(&$data); |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* The payload object or array matching the GraphQL definition |
175
|
|
|
* |
176
|
|
|
* @param mixed $data normalized data, its the input data processed by the form |
177
|
|
|
* @param ConstraintViolationList $violations violations returned by the form validation process |
178
|
|
|
* @param array $inputSource the original submitted data in array |
179
|
|
|
* |
180
|
|
|
* @return mixed |
181
|
|
|
*/ |
182
|
|
|
abstract public function returnPayload($data, ConstraintViolationList $violations, $inputSource); |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return null|string |
186
|
|
|
*/ |
187
|
8 |
|
protected function getPayloadClass(): string |
188
|
|
|
{ |
189
|
8 |
|
$type = $this->getContext()->getDefinition()->getType(); |
190
|
|
|
|
191
|
8 |
|
if (class_exists($type)) { |
192
|
3 |
|
return $type; |
193
|
|
|
} |
194
|
|
|
|
195
|
5 |
|
if ($this->context->getEndpoint()->hasType($type)) { |
196
|
5 |
|
$class = $this->context->getEndpoint()->getClassForType($type); |
197
|
5 |
|
if (class_exists($class)) { |
198
|
5 |
|
return $class; |
|
|
|
|
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
throw new \RuntimeException( |
203
|
|
|
sprintf( |
204
|
|
|
'Can\'t find a valid payload class for "%s".', |
205
|
|
|
$this->getContext()->getDefinition()->getName() |
206
|
|
|
) |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param array $input |
212
|
|
|
* |
213
|
|
|
* @return mixed |
214
|
|
|
*/ |
215
|
8 |
|
public function initialFormData($input) |
216
|
|
|
{ |
217
|
8 |
|
if (is_array($input) && isset($input['id'])) { |
218
|
3 |
|
$id = ID::createFromString($input['id']); |
219
|
3 |
|
if ($this->context->getEndpoint()->hasType($id->getNodeType())) { |
220
|
3 |
|
$class = $this->context->getEndpoint()->getClassForType($id->getNodeType()); |
221
|
3 |
|
if ($class) { |
222
|
3 |
|
return $this->getManager()->getRepository($class)->find($id->getDatabaseId()); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
6 |
|
return null; |
228
|
|
|
} |
229
|
|
|
/** |
230
|
|
|
* @param mixed|null $data |
231
|
|
|
* |
232
|
|
|
* @return FormBuilderInterface|null |
233
|
|
|
*/ |
234
|
8 |
|
public function createDefinitionForm($data): ?FormBuilderInterface |
235
|
|
|
{ |
236
|
8 |
|
if (!$this->context->getDefinition()->hasMeta('form')) { |
237
|
|
|
return null; |
238
|
|
|
} |
239
|
|
|
|
240
|
8 |
|
$formConfig = $this->context->getDefinition()->getMeta('form') ?? []; |
241
|
8 |
|
$formType = $formConfig['type'] ?? null; |
242
|
8 |
|
if (!$formConfig || !$formType) { |
243
|
|
|
throw new \RuntimeException(sprintf('Can`t find a valid form for %s', $this->context->getDefinition()->getName())); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
$options = [ |
247
|
8 |
|
'allow_extra_fields' => true, |
248
|
|
|
]; |
249
|
|
|
|
250
|
8 |
|
if ($this->container->hasParameter('form.type_extension.csrf.enabled') |
251
|
8 |
|
&& $this->container->getParameter('form.type_extension.csrf.enabled')) { |
252
|
8 |
|
$options['csrf_protection'] = false; |
253
|
|
|
} |
254
|
|
|
|
255
|
8 |
|
$options = array_merge($options, $formConfig['options'] ?? []); |
256
|
|
|
|
257
|
8 |
|
return $this->createFormBuilder($formType, $data, $options); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param FormInterface $form |
262
|
|
|
* @param ConstraintViolationList $violations |
263
|
|
|
* @param null|string $parentName |
264
|
|
|
*/ |
265
|
8 |
|
public function extractFormErrors(FormInterface $form, ConstraintViolationList $violations, ?string $parentName = null) |
266
|
|
|
{ |
267
|
8 |
|
$errors = $form->getErrors(true); |
268
|
8 |
|
foreach ($errors as $error) { |
269
|
1 |
|
$violation = new ConstraintViolation(); |
270
|
1 |
|
$violation->setMessage($error->getMessage()); |
|
|
|
|
271
|
1 |
|
$violation->setMessageTemplate($error->getMessageTemplate() ?? $error->getMessage()); |
|
|
|
|
272
|
1 |
|
foreach ($error->getMessageParameters() as $key => $value) { |
|
|
|
|
273
|
1 |
|
$violation->addParameter($key, $value); |
274
|
|
|
} |
275
|
|
|
|
276
|
1 |
|
$cause = $error->getCause(); |
|
|
|
|
277
|
1 |
|
if ($cause instanceof SymfonyConstraintViolation) { |
278
|
1 |
|
$violation->setCode($cause->getCode() ?? md5($violation->getMessageTemplate())); |
279
|
1 |
|
$violation->setInvalidValue($cause->getInvalidValue()); |
280
|
1 |
|
$violation->setPlural($cause->getPlural()); |
281
|
|
|
|
282
|
1 |
|
$path = $this->publicPropertyPath($form, $cause->getPropertyPath()); |
283
|
1 |
|
if ($path) { |
284
|
1 |
|
$violation->setPropertyPath($path); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
1 |
|
$violations->addViolation($violation); |
289
|
|
|
} |
290
|
8 |
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Convert internal validation property path to the public one, |
294
|
|
|
* required when use `property_path` in the form |
295
|
|
|
* |
296
|
|
|
* @param FormInterface $form |
297
|
|
|
* @param string $path |
298
|
|
|
* |
299
|
|
|
* @return string |
300
|
|
|
*/ |
301
|
1 |
|
private function publicPropertyPath(FormInterface $form, $path) |
302
|
|
|
{ |
303
|
1 |
|
$pathArray = [$path]; |
304
|
|
|
|
305
|
1 |
|
if (strpos($path, '.') !== false) { // object.child.property |
306
|
1 |
|
$pathArray = explode('.', $path); |
307
|
|
|
} |
308
|
|
|
|
309
|
1 |
|
if (strpos($path, '[') !== false) { //[array][child][property] |
310
|
1 |
|
$path = str_replace(']', null, $path); |
311
|
1 |
|
$pathArray = explode('[', $path); |
312
|
|
|
} |
313
|
|
|
|
314
|
1 |
|
if (in_array($pathArray[0], ['data', 'children'])) { |
315
|
1 |
|
array_shift($pathArray); |
316
|
|
|
} |
317
|
|
|
|
318
|
1 |
|
$contextForm = $form; |
319
|
1 |
|
foreach ($pathArray as &$propName) { |
320
|
|
|
//for some reason some inputs are resolved as "inputName.data" |
321
|
|
|
//because the original form property is children[inputName].data |
322
|
|
|
//this is the case of DEMO AddUserInput form the login field is validated as path children[login].data |
323
|
|
|
//the following statements remove the trailing ".data" |
324
|
1 |
|
if (preg_match('/\.data$/', $propName)) { |
325
|
1 |
|
$propName = preg_replace('/\.data$/', null, $propName); |
326
|
|
|
} |
327
|
|
|
|
328
|
1 |
|
$index = null; |
329
|
1 |
|
if (preg_match('/(\w+)(\[\d+\])$/', $propName, $matches)) { |
330
|
|
|
list(, $propName, $index) = $matches; |
331
|
|
|
} |
332
|
1 |
|
if (!$contextForm->has($propName)) { |
333
|
|
|
foreach ($contextForm->all() as $child) { |
334
|
|
|
if ($child->getConfig()->getOption('property_path') === $propName) { |
335
|
|
|
$propName = $child->getName(); |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
} |
339
|
1 |
|
if ($index) { |
340
|
1 |
|
$propName = sprintf('%s%s', $propName, $index); |
|
|
|
|
341
|
|
|
} |
342
|
|
|
} |
343
|
1 |
|
unset($propName); |
344
|
|
|
|
345
|
1 |
|
return implode('.', $pathArray); |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|