|
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\Form\DataTransformer\DataWithIdToNodeTransformer; |
|
20
|
|
|
use Ynlo\GraphQLBundle\Model\ConstraintViolation; |
|
21
|
|
|
use Ynlo\GraphQLBundle\Resolver\AbstractResolver; |
|
22
|
|
|
use Ynlo\GraphQLBundle\Validator\ConstraintViolationList; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Base class for mutations |
|
26
|
|
|
* Implement the method "process()" and "returnPayload()" is enough in many scenarios |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract class AbstractMutationResolver extends AbstractResolver implements EventSubscriberInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @param array $input |
|
32
|
|
|
* |
|
33
|
|
|
* @return mixed |
|
34
|
|
|
*/ |
|
35
|
8 |
|
public function __invoke($input) |
|
36
|
|
|
{ |
|
37
|
8 |
|
$formBuilder = $this->createDefinitionForm($input); |
|
38
|
|
|
|
|
39
|
8 |
|
$form = null; |
|
40
|
8 |
|
if ($formBuilder) { |
|
41
|
8 |
|
$formBuilder->addEventSubscriber($this); |
|
42
|
|
|
|
|
43
|
|
|
$extensionExecutor = function ($method) { |
|
44
|
8 |
|
return function (FormEvent $event) use ($method) { |
|
45
|
8 |
|
foreach ($this->extensions as $extension) { |
|
46
|
4 |
|
return call_user_func_array([$extension, $method], [$event]); |
|
47
|
|
|
} |
|
48
|
8 |
|
}; |
|
49
|
8 |
|
}; |
|
50
|
|
|
|
|
51
|
8 |
|
foreach (self::getSubscribedEvents() as $event => $method) { |
|
52
|
8 |
|
$formBuilder->addEventListener($event, $extensionExecutor($method)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
8 |
|
$form = $formBuilder->getForm(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
8 |
|
if ($form) { |
|
59
|
8 |
|
$form->submit($input, false); |
|
60
|
8 |
|
$data = $form->getData(); |
|
61
|
|
|
} else { |
|
62
|
|
|
$data = $input; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
8 |
|
$violations = new ConstraintViolationList(); |
|
66
|
8 |
|
if ($form) { |
|
67
|
8 |
|
$this->extractFormErrors($form, $violations); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
8 |
|
$dryRun = $input['dryRun'] ?? false; |
|
71
|
|
|
|
|
72
|
8 |
|
if ($dryRun) { |
|
73
|
|
|
$data = null; |
|
74
|
|
|
} else { |
|
75
|
8 |
|
if ((!$form && !$violations->count()) |
|
76
|
8 |
|
|| ($form->isSubmitted() && $form->isValid() && !$violations->count()) |
|
77
|
|
|
) { |
|
78
|
7 |
|
$this->process($data); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
8 |
|
return $this->returnPayload($data, $violations, $input); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
8 |
|
public static function getSubscribedEvents() |
|
89
|
|
|
{ |
|
90
|
|
|
return [ |
|
91
|
8 |
|
FormEvents::PRE_SET_DATA => 'preSetData', |
|
92
|
8 |
|
FormEvents::POST_SET_DATA => 'postSetData', |
|
93
|
8 |
|
FormEvents::PRE_SUBMIT => 'preSubmit', |
|
94
|
8 |
|
FormEvents::SUBMIT => 'onSubmit', |
|
95
|
8 |
|
FormEvents::POST_SUBMIT => 'postSubmit', |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html |
|
101
|
|
|
* |
|
102
|
|
|
* @param FormEvent $event |
|
103
|
|
|
*/ |
|
104
|
8 |
|
public function preSetData(FormEvent $event) |
|
105
|
|
|
{ |
|
106
|
8 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html |
|
110
|
|
|
* |
|
111
|
|
|
* @param FormEvent $event |
|
112
|
|
|
*/ |
|
113
|
8 |
|
public function postSetData(FormEvent $event) |
|
114
|
|
|
{ |
|
115
|
8 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html |
|
119
|
|
|
* |
|
120
|
|
|
* @param FormEvent $event |
|
121
|
|
|
*/ |
|
122
|
8 |
|
public function preSubmit(FormEvent $event) |
|
123
|
|
|
{ |
|
124
|
8 |
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html |
|
128
|
|
|
* |
|
129
|
|
|
* @param FormEvent $event |
|
130
|
|
|
*/ |
|
131
|
4 |
|
public function onSubmit(FormEvent $event) |
|
132
|
|
|
{ |
|
133
|
4 |
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html |
|
137
|
|
|
* |
|
138
|
|
|
* @param FormEvent $event |
|
139
|
|
|
*/ |
|
140
|
8 |
|
public function postSubmit(FormEvent $event) |
|
141
|
|
|
{ |
|
142
|
8 |
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Actions to process |
|
146
|
|
|
* the result processed data is given to payload |
|
147
|
|
|
* |
|
148
|
|
|
* @param mixed $data |
|
149
|
|
|
*/ |
|
150
|
|
|
abstract public function process(&$data); |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* The payload object or array matching the GraphQL definition |
|
154
|
|
|
* |
|
155
|
|
|
* @param mixed $data normalized data, its the input data processed by the form |
|
156
|
|
|
* @param ConstraintViolationList $violations violations returned by the form validation process |
|
157
|
|
|
* @param array $inputSource the original submitted data in array |
|
158
|
|
|
* |
|
159
|
|
|
* @return mixed |
|
160
|
|
|
*/ |
|
161
|
|
|
abstract public function returnPayload($data, ConstraintViolationList $violations, $inputSource); |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param mixed $data |
|
165
|
|
|
* |
|
166
|
|
|
* @return FormBuilderInterface|null |
|
167
|
|
|
*/ |
|
168
|
8 |
|
public function createDefinitionForm($data): ?FormBuilderInterface |
|
169
|
|
|
{ |
|
170
|
8 |
|
if (!$this->context->getDefinition()->hasMeta('form')) { |
|
171
|
|
|
return null; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
8 |
|
$formConfig = $this->context->getDefinition()->getMeta('form') ?? []; |
|
175
|
8 |
|
$formType = $formConfig['type'] ?? null; |
|
176
|
8 |
|
if (!$formConfig || !$formType) { |
|
177
|
|
|
throw new \RuntimeException(sprintf('Can`t find a valid form for %s', $this->context->getDefinition()->getName())); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
$options = [ |
|
181
|
8 |
|
'allow_extra_fields' => true, |
|
182
|
|
|
]; |
|
183
|
|
|
|
|
184
|
8 |
|
if ($this->container->hasParameter('form.type_extension.csrf.enabled') |
|
185
|
8 |
|
&& $this->container->getParameter('form.type_extension.csrf.enabled')) { |
|
186
|
8 |
|
$options['csrf_protection'] = false; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
8 |
|
$options = array_merge($options, $formConfig['options'] ?? []); |
|
190
|
|
|
|
|
191
|
8 |
|
$form = $this->createFormBuilder($formType, $data, $options); |
|
192
|
8 |
|
$viewTransformer = new DataWithIdToNodeTransformer($this->getManager(), $this->context->getEndpoint()); |
|
193
|
8 |
|
$form->addViewTransformer($viewTransformer); |
|
194
|
|
|
|
|
195
|
8 |
|
return $form; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param FormInterface $form |
|
200
|
|
|
* @param ConstraintViolationList $violations |
|
201
|
|
|
* @param null|string $parentName |
|
202
|
|
|
*/ |
|
203
|
8 |
|
public function extractFormErrors(FormInterface $form, ConstraintViolationList $violations, ?string $parentName = null) |
|
204
|
|
|
{ |
|
205
|
8 |
|
$errors = $form->getErrors(true); |
|
206
|
8 |
|
foreach ($errors as $error) { |
|
207
|
1 |
|
$violation = new ConstraintViolation(); |
|
208
|
1 |
|
$violation->setMessage($error->getMessage()); |
|
|
|
|
|
|
209
|
1 |
|
$violation->setMessageTemplate($error->getMessageTemplate()); |
|
|
|
|
|
|
210
|
1 |
|
foreach ($error->getMessageParameters() as $key => $value) { |
|
|
|
|
|
|
211
|
1 |
|
$violation->addParameter($key, $value); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
1 |
|
$cause = $error->getCause(); |
|
|
|
|
|
|
215
|
1 |
|
if ($cause instanceof SymfonyConstraintViolation) { |
|
216
|
1 |
|
$violation->setCode($cause->getCode()); |
|
217
|
1 |
|
$violation->setInvalidValue($cause->getInvalidValue()); |
|
218
|
1 |
|
$violation->setPlural($cause->getPlural()); |
|
219
|
|
|
|
|
220
|
1 |
|
$path = $this->publicPropertyPath($form, $cause->getPropertyPath()); |
|
221
|
1 |
|
if ($path) { |
|
222
|
1 |
|
$violation->setPropertyPath($path); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
1 |
|
$violations->addViolation($violation); |
|
227
|
|
|
} |
|
228
|
8 |
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Convert internal validation property path to the public one, |
|
232
|
|
|
* required when use `property_path` in the form |
|
233
|
|
|
* |
|
234
|
|
|
* @param FormInterface $form |
|
235
|
|
|
* @param string $path |
|
236
|
|
|
* |
|
237
|
|
|
* @return string |
|
238
|
|
|
*/ |
|
239
|
1 |
|
private function publicPropertyPath(FormInterface $form, $path) |
|
240
|
|
|
{ |
|
241
|
1 |
|
if (strpos($path, '.') !== false) { |
|
242
|
1 |
|
$pathArray = explode('.', $path); |
|
243
|
|
|
} else { |
|
244
|
|
|
$pathArray = [$path]; |
|
245
|
|
|
} |
|
246
|
1 |
|
if ($pathArray[0] === 'data') { |
|
247
|
1 |
|
array_shift($pathArray); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
1 |
|
$contextForm = $form; |
|
251
|
1 |
|
foreach ($pathArray as &$propName) { |
|
252
|
1 |
|
$index = null; |
|
253
|
1 |
|
if (preg_match('/(\w+)(\[\d+\])$/', $propName, $matches)) { |
|
254
|
|
|
list(, $propName, $index) = $matches; |
|
255
|
|
|
} |
|
256
|
1 |
|
if (!$contextForm->has($propName)) { |
|
257
|
1 |
|
foreach ($contextForm->all() as $child) { |
|
258
|
1 |
|
if ($child->getConfig()->getOption('property_path') === $propName) { |
|
259
|
1 |
|
$propName = $child->getName(); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
1 |
|
if ($index) { |
|
264
|
1 |
|
$propName = sprintf('%s%s', $propName, $index); |
|
|
|
|
|
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
1 |
|
unset($propName); |
|
268
|
|
|
|
|
269
|
1 |
|
return implode('.', $pathArray); |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.