1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2018 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* |
8
|
|
|
* @license GPL-3.0 |
9
|
|
|
* |
10
|
|
|
* @author Gerrit Addiks <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Addiks\SymfonyGenerics\Services; |
14
|
|
|
|
15
|
|
|
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface; |
16
|
|
|
use Psr\Container\ContainerInterface; |
17
|
|
|
use ReflectionParameter; |
18
|
|
|
use ReflectionType; |
19
|
|
|
use ReflectionMethod; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Webmozart\Assert\Assert; |
22
|
|
|
use ReflectionClass; |
23
|
|
|
use ReflectionFunctionAbstract; |
24
|
|
|
use InvalidArgumentException; |
25
|
|
|
use ReflectionException; |
26
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
27
|
|
|
use ValueObjects\ValueObjectInterface; |
28
|
|
|
use Symfony\Component\HttpFoundation\FileBag; |
29
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
30
|
|
|
|
31
|
|
|
final class ArgumentCompiler implements ArgumentCompilerInterface |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ContainerInterface |
36
|
|
|
*/ |
37
|
|
|
private $container; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var EntityManagerInterface |
41
|
|
|
*/ |
42
|
|
|
private $entityManager; |
43
|
|
|
|
44
|
12 |
|
public function __construct( |
45
|
|
|
ContainerInterface $container, |
46
|
|
|
EntityManagerInterface $entityManager |
47
|
|
|
) { |
48
|
12 |
|
$this->container = $container; |
49
|
12 |
|
$this->entityManager = $entityManager; |
50
|
12 |
|
} |
51
|
|
|
|
52
|
9 |
|
public function buildArguments( |
53
|
|
|
array $argumentsConfiguration, |
54
|
|
|
Request $request, |
55
|
|
|
array $additionalData = array() |
56
|
|
|
): array { |
57
|
|
|
/** @var array<int, mixed> $routeArguments */ |
58
|
9 |
|
$routeArguments = array(); |
59
|
|
|
|
60
|
9 |
|
foreach ($argumentsConfiguration as $key => $argumentConfiguration) { |
61
|
|
|
/** @var array|string $argumentConfiguration */ |
62
|
|
|
|
63
|
|
|
/** @var string|null $parameterTypeName */ |
64
|
9 |
|
$parameterTypeName = null; |
65
|
|
|
|
66
|
9 |
|
if (isset($argumentConfiguration['entity-class'])) { |
67
|
|
|
$parameterTypeName = $argumentConfiguration['entity-class']; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @var mixed $argumentValue */ |
71
|
9 |
|
$argumentValue = $this->resolveArgumentConfiguration( |
72
|
9 |
|
$argumentConfiguration, |
73
|
9 |
|
$request, |
74
|
9 |
|
$parameterTypeName, |
75
|
9 |
|
$additionalData |
76
|
|
|
); |
77
|
|
|
|
78
|
6 |
|
$routeArguments[$key] = $argumentValue; |
79
|
|
|
} |
80
|
|
|
|
81
|
5 |
|
return $routeArguments; |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
public function buildCallArguments( |
85
|
|
|
ReflectionFunctionAbstract $routineReflection, |
86
|
|
|
array $argumentsConfiguration, |
87
|
|
|
Request $request, |
88
|
|
|
array $predefinedArguments = array(), |
89
|
|
|
array $additionalData = array() |
90
|
|
|
): array { |
91
|
|
|
/** @var array<int, mixed> $callArguments */ |
92
|
4 |
|
$callArguments = array(); |
93
|
|
|
|
94
|
4 |
|
foreach ($routineReflection->getParameters() as $index => $parameterReflection) { |
95
|
|
|
/** @var ReflectionParameter $parameterReflection */ |
96
|
|
|
|
97
|
3 |
|
if (isset($predefinedArguments[$index])) { |
98
|
|
|
$callArguments[] = $predefinedArguments[$index]; |
99
|
|
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** @var string $parameterName */ |
103
|
3 |
|
$parameterName = $parameterReflection->getName(); |
|
|
|
|
104
|
|
|
|
105
|
|
|
/** @var mixed $requestValue */ |
106
|
3 |
|
$requestValue = $request->get($parameterName); |
107
|
|
|
|
108
|
|
|
/** @var string|null $parameterTypeName */ |
109
|
3 |
|
$parameterTypeName = null; |
110
|
|
|
|
111
|
3 |
|
if ($parameterReflection->hasType()) { |
112
|
|
|
/** @var ReflectionType|null $parameterType */ |
113
|
2 |
|
$parameterType = $parameterReflection->getType(); |
114
|
|
|
|
115
|
2 |
|
if ($parameterType instanceof ReflectionType) { |
|
|
|
|
116
|
2 |
|
$parameterTypeName = $parameterType->__toString(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
3 |
|
if (isset($argumentsConfiguration[$parameterName])) { |
121
|
|
|
/** @var array|string $argumentConfiguration */ |
122
|
3 |
|
$argumentConfiguration = $argumentsConfiguration[$parameterName]; |
123
|
|
|
|
124
|
|
|
/** @var mixed $argumentValue */ |
125
|
3 |
|
$argumentValue = $argumentConfiguration; |
126
|
|
|
|
127
|
3 |
|
if (is_string($argumentConfiguration) || is_array($argumentConfiguration)) { |
128
|
3 |
|
$argumentValue = $this->resolveArgumentConfiguration( |
129
|
3 |
|
$argumentConfiguration, |
130
|
3 |
|
$request, |
131
|
3 |
|
$parameterTypeName, |
132
|
3 |
|
$additionalData |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
$callArguments[$index] = $argumentValue; |
137
|
|
|
|
138
|
2 |
|
} elseif (isset($argumentsConfiguration[$index])) { |
139
|
|
|
/** @var array|string $argumentConfiguration */ |
140
|
|
|
$argumentConfiguration = $argumentsConfiguration[$index]; |
141
|
|
|
|
142
|
|
|
/** @var mixed $argumentValue */ |
143
|
|
|
$argumentValue = $argumentConfiguration; |
144
|
|
|
|
145
|
|
|
if (is_string($argumentConfiguration) || is_array($argumentConfiguration)) { |
146
|
|
|
$argumentValue = $this->resolveArgumentConfiguration( |
147
|
|
|
$argumentConfiguration, |
148
|
|
|
$request, |
149
|
|
|
$parameterTypeName, |
150
|
|
|
$additionalData |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$callArguments[$index] = $argumentValue; |
155
|
|
|
|
156
|
2 |
|
} elseif (!is_null($requestValue)) { |
157
|
1 |
|
if (!is_null($parameterTypeName)) { |
158
|
|
|
/** @psalm-suppress UndefinedClass ValueObjects\ValueObjectInterface does not exist */ |
159
|
|
|
if (is_subclass_of($parameterTypeName, ValueObjectInterface::class)) { |
|
|
|
|
160
|
|
|
$argumentValue = $parameterTypeName::fromNative($requestValue); |
161
|
|
|
|
162
|
|
|
$callArguments[$index] = $argumentValue; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
} else { |
166
|
1 |
|
$callArguments[$index] = $requestValue; |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
} elseif ($parameterTypeName === Request::class) { |
170
|
|
|
$callArguments[$index] = $request; |
171
|
|
|
|
172
|
|
|
} else { |
173
|
|
|
try { |
174
|
1 |
|
$callArguments[$index] = $parameterReflection->getDefaultValue(); |
175
|
|
|
|
176
|
1 |
|
} catch (ReflectionException $exception) { |
177
|
1 |
|
throw new InvalidArgumentException(sprintf( |
178
|
1 |
|
"Missing argument '%s' for the call to '%s'!", |
179
|
1 |
|
$parameterName, |
180
|
2 |
|
$routineReflection->getName() |
181
|
|
|
)); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
2 |
|
return $callArguments; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param array|string $argumentConfiguration |
191
|
|
|
* |
192
|
|
|
* @return mixed |
193
|
|
|
*/ |
194
|
12 |
|
public function resolveArgumentConfiguration( |
195
|
|
|
$argumentConfiguration, |
196
|
|
|
Request $request, |
197
|
|
|
?string $parameterTypeName, |
198
|
|
|
array $additionalData = array() |
199
|
|
|
) { |
200
|
|
|
/** @var mixed $argumentValue */ |
201
|
12 |
|
$argumentValue = null; |
202
|
|
|
|
203
|
12 |
|
if (is_array($argumentConfiguration)) { |
204
|
3 |
|
if (isset($argumentConfiguration['entity-class'])) { |
205
|
|
|
$parameterTypeName = $argumentConfiguration['entity-class']; |
206
|
|
|
} |
207
|
|
|
|
208
|
3 |
|
if (isset($argumentConfiguration['service-id'])) { |
209
|
3 |
|
$argumentValue = $this->container->get($argumentConfiguration['service-id']); |
210
|
|
|
|
211
|
3 |
|
Assert::object($argumentValue, sprintf( |
212
|
3 |
|
"Did not find service '%s'!", |
213
|
3 |
|
$argumentConfiguration['service-id'] |
214
|
|
|
)); |
215
|
|
|
|
216
|
|
|
} elseif (isset($argumentConfiguration['entity-id'])) { |
217
|
|
|
$argumentValue = $this->resolveStringArgumentConfiguration( |
218
|
|
|
$argumentConfiguration['entity-id'], |
219
|
|
|
$request |
220
|
|
|
); |
221
|
|
|
|
222
|
|
|
} elseif (class_exists($parameterTypeName)) { |
223
|
|
|
$argumentValue = $this->resolveStringArgumentConfiguration( |
224
|
|
|
$parameterTypeName, |
225
|
|
|
$request |
226
|
|
|
); |
227
|
|
|
} |
228
|
|
|
|
229
|
2 |
|
if (isset($argumentConfiguration['method'])) { |
230
|
2 |
|
$methodReflection = new ReflectionMethod($argumentValue, $argumentConfiguration['method']); |
231
|
|
|
|
232
|
2 |
|
if (!isset($argumentConfiguration['arguments'])) { |
233
|
1 |
|
$argumentConfiguration['arguments'] = []; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** @var array $callArguments */ |
237
|
2 |
|
$callArguments = $this->buildCallArguments( |
238
|
2 |
|
$methodReflection, |
239
|
2 |
|
$argumentConfiguration['arguments'], |
240
|
2 |
|
$request |
241
|
|
|
); |
242
|
|
|
|
243
|
1 |
|
$argumentValue = $methodReflection->invokeArgs($argumentValue, $callArguments); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
} else { |
247
|
10 |
|
$argumentValue = $this->resolveStringArgumentConfiguration( |
248
|
10 |
|
$argumentConfiguration, |
249
|
10 |
|
$request, |
250
|
10 |
|
$additionalData |
251
|
|
|
); |
252
|
|
|
} |
253
|
|
|
|
254
|
7 |
|
if (!empty($parameterTypeName) && !is_object($argumentValue)) { |
255
|
1 |
|
if (class_exists($parameterTypeName)) { |
256
|
|
|
/** @psalm-suppress UndefinedClass ValueObjects\ValueObjectInterface does not exist */ |
257
|
1 |
|
if (is_subclass_of($parameterTypeName, ValueObjectInterface::class)) { |
|
|
|
|
258
|
|
|
$argumentValue = call_user_func("{$parameterTypeName}::fromNative", $argumentValue); |
259
|
|
|
|
260
|
|
|
} else { |
261
|
1 |
|
$argumentValue = $this->entityManager->find($parameterTypeName, $argumentValue); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
7 |
|
return $argumentValue; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @return mixed |
271
|
|
|
*/ |
272
|
10 |
|
private function resolveStringArgumentConfiguration( |
273
|
|
|
string $argumentConfiguration, |
274
|
|
|
Request $request, |
275
|
|
|
array $additionalData = array() |
276
|
|
|
) { |
277
|
|
|
/** @var mixed $argumentValue */ |
278
|
10 |
|
$argumentValue = null; |
279
|
|
|
|
280
|
10 |
|
$argumentConfiguration = trim($argumentConfiguration); |
281
|
|
|
|
282
|
10 |
|
if (empty($argumentConfiguration)) { |
283
|
|
|
$argumentValue = ""; |
284
|
|
|
|
285
|
10 |
|
} elseif ($argumentConfiguration === 'true') { |
286
|
|
|
$argumentValue = true; |
287
|
|
|
|
288
|
10 |
|
} elseif ($argumentConfiguration === 'false') { |
289
|
|
|
$argumentValue = false; |
290
|
|
|
|
291
|
10 |
|
} elseif ($argumentConfiguration === 'null') { |
292
|
|
|
$argumentValue = null; |
293
|
|
|
|
294
|
10 |
|
} elseif (is_int(strpos($argumentConfiguration, '::'))) { |
295
|
2 |
|
[$factoryClass, $factoryMethod] = explode('::', $argumentConfiguration); |
|
|
|
|
296
|
|
|
|
297
|
|
|
/** @var array<string> $callArgumentConfigurations */ |
298
|
2 |
|
$callArgumentConfigurations = array(); |
299
|
|
|
|
300
|
2 |
|
if (is_int(strpos($factoryMethod, '('))) { |
|
|
|
|
301
|
|
|
$factoryMethod = str_replace(')', '', $factoryMethod); |
|
|
|
|
302
|
|
|
[$factoryMethod, $rawArguments] = explode('(', $factoryMethod, 2); |
|
|
|
|
303
|
|
|
|
304
|
|
|
foreach (explode(",", $rawArguments) as $rawArgument) { |
305
|
|
|
/** @var string $rawArgument */ |
306
|
|
|
|
307
|
|
|
$callArgumentConfigurations[] = trim($rawArgument); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
311
|
2 |
|
if (!empty($factoryClass)) { |
312
|
2 |
|
if ($factoryClass[0] == '@') { |
313
|
|
|
/** @var string $factoryServiceId */ |
314
|
2 |
|
$factoryServiceId = substr($factoryClass, 1); |
315
|
|
|
|
316
|
|
|
/** @var object $factoryObject */ |
317
|
2 |
|
$factoryObject = $this->container->get($factoryServiceId); |
318
|
|
|
|
319
|
2 |
|
Assert::object($factoryObject, sprintf( |
320
|
2 |
|
"Could not find service with id '%s'!", |
321
|
2 |
|
$factoryServiceId |
322
|
|
|
)); |
323
|
|
|
|
324
|
2 |
|
Assert::methodExists($factoryObject, $factoryMethod, sprintf( |
|
|
|
|
325
|
2 |
|
"Method '%s' does not exist on service '%s'! (Class '%s')", |
326
|
2 |
|
$factoryMethod, |
327
|
2 |
|
$factoryServiceId, |
328
|
2 |
|
get_class($factoryObject) |
329
|
|
|
)); |
330
|
|
|
|
331
|
|
|
/** @var array<int, mixed> $callArguments */ |
332
|
1 |
|
$callArguments = $this->buildCallArguments( |
333
|
1 |
|
new ReflectionMethod($factoryObject, $factoryMethod), |
334
|
1 |
|
$callArgumentConfigurations, |
335
|
1 |
|
$request |
336
|
|
|
); |
337
|
|
|
|
338
|
1 |
|
$argumentValue = $this->callOnObject( |
339
|
1 |
|
$factoryObject, |
340
|
1 |
|
$factoryMethod, |
341
|
1 |
|
$callArguments, |
342
|
1 |
|
$request, |
343
|
1 |
|
sprintf( |
344
|
1 |
|
"Did not find service with id '%s' that has a method '%s'!", |
345
|
1 |
|
$factoryServiceId, |
346
|
1 |
|
$factoryMethod |
347
|
|
|
) |
348
|
|
|
); |
349
|
|
|
|
350
|
|
|
} elseif (is_int(strpos($factoryClass, '#'))) { |
351
|
|
|
# Create by call on entity |
352
|
|
|
[$entityClass, $idRaw] = explode('#', $factoryClass); |
|
|
|
|
353
|
|
|
|
354
|
|
|
$id = $this->resolveStringArgumentConfiguration($idRaw, $request, $additionalData); |
355
|
|
|
|
356
|
|
|
/** @var object $entity */ |
357
|
|
|
$entity = $this->entityManager->find($entityClass, $id); |
358
|
|
|
|
359
|
|
|
Assert::object($entity, sprintf( |
360
|
|
|
"Could not find entity '%s' with id '%s'!", |
361
|
|
|
$entityClass, |
362
|
|
|
$id |
363
|
|
|
)); |
364
|
|
|
|
365
|
|
|
/** @var array<int, mixed> $callArguments */ |
366
|
|
|
$callArguments = $this->buildCallArguments( |
367
|
|
|
new ReflectionMethod($entity, $factoryMethod), |
368
|
|
|
$callArgumentConfigurations, |
369
|
|
|
$request |
370
|
|
|
); |
371
|
|
|
|
372
|
|
|
$argumentValue = $this->callOnObject( |
373
|
|
|
$entity, |
374
|
|
|
$factoryMethod, |
375
|
|
|
$callArguments, |
376
|
|
|
$request, |
377
|
|
|
sprintf( |
378
|
|
|
"Entity '%s' does not have method '%s'!", |
379
|
|
|
$entityClass, |
380
|
|
|
$factoryMethod |
381
|
|
|
) |
382
|
|
|
); |
383
|
|
|
|
384
|
|
|
} else { |
385
|
|
|
$callArguments = array(); |
386
|
|
|
|
387
|
|
|
if (is_int(strpos($argumentConfiguration, '('))) { |
388
|
|
|
$argumentConfiguration = str_replace(")", "", $argumentConfiguration); |
389
|
|
|
[$argumentConfiguration, $callArgumentsRaw] = explode('(', $argumentConfiguration); |
|
|
|
|
390
|
|
|
|
391
|
|
|
if (!empty($callArgumentsRaw)) { |
|
|
|
|
392
|
|
|
foreach (explode(',', $callArgumentsRaw) as $callArgumentRaw) { |
393
|
|
|
$callArguments[] = $this->resolveStringArgumentConfiguration( |
394
|
|
|
$callArgumentRaw, |
395
|
|
|
$request, |
396
|
|
|
$additionalData |
397
|
|
|
); |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
if ($factoryClass[0] === '%' && $factoryClass[-1] === '%') { |
403
|
|
|
/** @var string $key */ |
404
|
|
|
$key = substr($factoryClass, 1, strlen($factoryClass) - 2); |
405
|
|
|
|
406
|
|
|
if (isset($additionalData[$key])) { |
407
|
|
|
$argumentConfiguration = [$additionalData[$key], $factoryMethod]; |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
# Create by static factory-method of other class |
412
|
1 |
|
$argumentValue = call_user_func_array($argumentConfiguration, $callArguments); |
413
|
|
|
} |
414
|
|
|
|
415
|
1 |
|
} else { |
416
|
|
|
# TODO: What to do here? What could "::Something" be? A template? |
417
|
|
|
} |
418
|
|
|
|
419
|
10 |
|
} elseif ($argumentConfiguration[0] == "'" && $argumentConfiguration[strlen($argumentConfiguration) - 1] == "'") { |
420
|
1 |
|
$argumentValue = substr($argumentConfiguration, 1, strlen($argumentConfiguration) - 2); |
421
|
|
|
|
422
|
9 |
|
} elseif ($argumentConfiguration[0] == '"' && $argumentConfiguration[strlen($argumentConfiguration) - 1] == '"') { |
423
|
|
|
$argumentValue = substr($argumentConfiguration, 1, strlen($argumentConfiguration) - 2); |
424
|
|
|
|
425
|
9 |
|
} elseif ($argumentConfiguration == '$') { |
426
|
1 |
|
$argumentValue = $request->getContent(false); |
427
|
|
|
|
428
|
8 |
|
} elseif (substr($argumentConfiguration, 0, 7) === '$files.') { |
429
|
|
|
/** @var FileBag $files */ |
430
|
2 |
|
$files = $request->files; |
431
|
|
|
|
432
|
2 |
|
if (substr_count($argumentConfiguration, '.') <= 1) { |
433
|
|
|
$argumentConfiguration .= ".content"; |
434
|
|
|
} |
435
|
|
|
|
436
|
2 |
|
[, $filesKey, $fileArgument] = explode(".", $argumentConfiguration); |
|
|
|
|
437
|
|
|
|
438
|
|
|
/** @var UploadedFile $file */ |
439
|
2 |
|
$file = $files->get($filesKey); |
440
|
|
|
|
441
|
2 |
|
Assert::isInstanceOf($file, UploadedFile::class, sprintf( |
442
|
2 |
|
"Missing request-argument '%s' as uploaded file!", |
443
|
2 |
|
$filesKey |
444
|
|
|
)); |
445
|
|
|
|
446
|
|
|
$argumentValue = [ |
447
|
1 |
|
'object' => $file, |
448
|
1 |
|
'originalname' => $file->getClientOriginalName(), |
449
|
1 |
|
'filename' => $file->getFilename(), |
450
|
1 |
|
'content' => file_get_contents($file->getPathname()), |
451
|
1 |
|
'mimetype' => $file->getMimeType(), |
452
|
1 |
|
][$fileArgument]; |
453
|
|
|
|
454
|
6 |
|
} elseif ($argumentConfiguration[0] == '$') { |
455
|
3 |
|
$argumentValue = $request->get(substr($argumentConfiguration, 1)); |
456
|
|
|
|
457
|
3 |
|
if (is_string($argumentValue)) { |
458
|
3 |
|
$argumentValue = trim($argumentValue); |
459
|
|
|
} |
460
|
|
|
|
461
|
4 |
|
} elseif ($argumentConfiguration[0] == '@') { |
462
|
1 |
|
$argumentValue = $this->container->get(substr($argumentConfiguration, 1)); |
463
|
|
|
|
464
|
3 |
|
} elseif ($argumentConfiguration[0] == '%' && $argumentConfiguration[-1] == '%') { |
465
|
|
|
/** @var string $key */ |
466
|
3 |
|
$key = substr($argumentConfiguration, 1, strlen($argumentConfiguration) - 2); |
467
|
|
|
|
468
|
3 |
|
if (is_int(strpos($key, '.'))) { |
469
|
1 |
|
[$key, $methodName] = explode('.', $key); |
|
|
|
|
470
|
|
|
|
471
|
1 |
|
Assert::keyExists($additionalData, $key, sprintf( |
472
|
1 |
|
'Missing additional-data key "%s"', |
473
|
1 |
|
$key |
474
|
|
|
)); |
475
|
|
|
|
476
|
1 |
|
$argumentValue = $additionalData[$key]; |
477
|
|
|
|
478
|
1 |
|
Assert::methodExists($argumentValue, $methodName, sprintf( |
479
|
1 |
|
"Missing method '%s' on '%s'!", |
480
|
1 |
|
$methodName, |
481
|
1 |
|
str_replace("%", "%%", $argumentConfiguration) |
482
|
|
|
)); |
483
|
|
|
|
484
|
|
|
$argumentValue = call_user_func([$argumentValue, $methodName]); |
485
|
|
|
|
486
|
|
|
} else { |
487
|
2 |
|
Assert::keyExists($additionalData, $key, sprintf( |
488
|
2 |
|
'Missing additional-data key "%s"', |
489
|
2 |
|
$key |
490
|
|
|
)); |
491
|
|
|
|
492
|
1 |
|
$argumentValue = $additionalData[$key]; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
} elseif (is_int(strpos($argumentConfiguration, '#'))) { |
496
|
|
|
# Create as entity |
497
|
|
|
[$entityClass, $idRaw] = explode('#', $argumentConfiguration); |
498
|
|
|
|
499
|
|
|
$id = $this->resolveStringArgumentConfiguration($idRaw, $request); |
500
|
|
|
|
501
|
|
|
$argumentValue = $this->entityManager->find($entityClass, $id); |
502
|
|
|
|
503
|
|
|
Assert::object($argumentValue, sprintf("Did not find entity %s with id '%s'!", $entityClass, $id)); |
504
|
|
|
|
505
|
|
|
} else { |
506
|
|
|
$argumentValue = $argumentConfiguration; |
507
|
|
|
} |
508
|
|
|
|
509
|
7 |
|
return $argumentValue; |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* @param object $object |
514
|
|
|
* |
515
|
|
|
* @return mixed |
516
|
|
|
*/ |
517
|
1 |
|
private function callOnObject( |
518
|
|
|
$object, |
519
|
|
|
string $method, |
520
|
|
|
array $callArguments, |
521
|
|
|
Request $request, |
522
|
|
|
string $methodNotExistMessage |
523
|
|
|
) { |
524
|
1 |
|
Assert::methodExists($object, $method, $methodNotExistMessage); |
525
|
|
|
|
526
|
1 |
|
$objectReflection = new ReflectionClass($object); |
527
|
|
|
|
528
|
|
|
/** @var ReflectionMethod $methodReflection */ |
529
|
1 |
|
$methodReflection = $objectReflection->getMethod($method); |
530
|
|
|
|
531
|
1 |
|
$callArguments = $this->buildCallArguments( |
532
|
1 |
|
$methodReflection, |
533
|
1 |
|
[], # TODO |
534
|
1 |
|
$request, |
535
|
1 |
|
$callArguments |
536
|
|
|
); |
537
|
|
|
|
538
|
1 |
|
return call_user_func_array([$object, $method], $callArguments); |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
} |
542
|
|
|
|