1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\ResourceBundle\Controller; |
13
|
|
|
|
14
|
|
|
use Sylius\Component\Resource\Metadata\MetadataInterface; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Resource controller configuration. |
20
|
|
|
* |
21
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
22
|
|
|
* @author Saša Stamenković <[email protected]> |
23
|
|
|
* @author Gustavo Perdomo <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class RequestConfiguration |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Request |
29
|
|
|
*/ |
30
|
|
|
protected $request; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var MetadataInterface |
34
|
|
|
*/ |
35
|
|
|
protected $metadata; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Parameters |
39
|
|
|
*/ |
40
|
|
|
protected $parameters; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param MetadataInterface $metadata |
44
|
|
|
* @param Request $request |
45
|
|
|
* @param Parameters $parameters |
46
|
|
|
*/ |
47
|
|
|
public function __construct(MetadataInterface $metadata, Request $request, Parameters $parameters) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$this->metadata = $metadata; |
50
|
|
|
$this->request = $request; |
51
|
|
|
$this->parameters = $parameters; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return Request |
56
|
|
|
*/ |
57
|
|
|
public function getRequest() |
58
|
|
|
{ |
59
|
|
|
return $this->request; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return MetadataInterface |
64
|
|
|
*/ |
65
|
|
|
public function getMetadata() |
66
|
|
|
{ |
67
|
|
|
return $this->metadata; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return Parameters |
72
|
|
|
*/ |
73
|
|
|
public function getParameters() |
74
|
|
|
{ |
75
|
|
|
return $this->parameters; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string|null |
80
|
|
|
*/ |
81
|
|
|
public function getSection() |
82
|
|
|
{ |
83
|
|
|
return $this->parameters->get('section'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function isHtmlRequest() |
90
|
|
|
{ |
91
|
|
|
return 'html' === $this->request->getRequestFormat(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $name |
96
|
|
|
* |
97
|
|
|
* @return null|string |
98
|
|
|
*/ |
99
|
|
|
public function getDefaultTemplate($name) |
100
|
|
|
{ |
101
|
|
|
$templatesNamespace = $this->metadata->getTemplatesNamespace(); |
102
|
|
|
|
103
|
|
|
if (false !== strpos($templatesNamespace, ':')) { |
104
|
|
|
return sprintf('%s:%s.%s', $templatesNamespace ?: ':', $name, 'twig'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return sprintf('%s/%s.%s', $templatesNamespace, $name, 'twig'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param $name |
112
|
|
|
* |
113
|
|
|
* @return mixed|null |
114
|
|
|
*/ |
115
|
|
|
public function getTemplate($name) |
116
|
|
|
{ |
117
|
|
|
$template = $this->parameters->get('template', $this->getDefaultTemplate($name)); |
118
|
|
|
|
119
|
|
|
if (null === $template) { |
120
|
|
|
throw new \RuntimeException(sprintf('Could not resolve template for resource "%s".', $this->metadata->getAlias())); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $template; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return mixed|null |
128
|
|
|
*/ |
129
|
|
|
public function getFormType() |
130
|
|
|
{ |
131
|
|
|
$form = $this->parameters->get('form', sprintf('%s_%s', $this->metadata->getApplicationName(), $this->metadata->getName())); |
132
|
|
|
|
133
|
|
|
if (is_array($form) && array_key_exists('type', $form)) { |
134
|
|
|
return $form['type']; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $form; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return mixed|null |
142
|
|
|
*/ |
143
|
|
|
public function getFormOptions() |
144
|
|
|
{ |
145
|
|
|
$form = $this->parameters->get('form', sprintf('%s_%s', $this->metadata->getApplicationName(), $this->metadata->getName())); |
146
|
|
|
|
147
|
|
|
if (is_array($form) && array_key_exists('options', $form)) { |
148
|
|
|
return $form['options']; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return []; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param $name |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function getRouteName($name) |
160
|
|
|
{ |
161
|
|
|
$sectionPrefix = $this->getSection() ? $this->getSection().'_' : ''; |
162
|
|
|
|
163
|
|
|
return sprintf('%s_%s%s_%s', $this->metadata->getApplicationName(), $sectionPrefix, $this->metadata->getName(), $name); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param $name |
168
|
|
|
* |
169
|
|
|
* @return mixed|null|string |
170
|
|
|
*/ |
171
|
|
|
public function getRedirectRoute($name) |
172
|
|
|
{ |
173
|
|
|
$redirect = $this->parameters->get('redirect'); |
174
|
|
|
|
175
|
|
|
if (null === $redirect) { |
176
|
|
|
return $this->getRouteName($name); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if (is_array($redirect)) { |
180
|
|
|
if (!empty($redirect['referer'])) { |
181
|
|
|
return 'referer'; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $redirect['route']; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $redirect; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get url hash fragment (#text) which is you configured. |
192
|
|
|
* |
193
|
|
|
* @return null|string |
194
|
|
|
*/ |
195
|
|
|
public function getRedirectHash() |
196
|
|
|
{ |
197
|
|
|
$redirect = $this->parameters->get('redirect'); |
198
|
|
|
|
199
|
|
|
if (!is_array($redirect) || empty($redirect['hash'])) { |
200
|
|
|
return null; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return '#'.$redirect['hash']; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get redirect referer, This will detected by configuration |
208
|
|
|
* If not exists, The `referrer` from headers will be used. |
209
|
|
|
* |
210
|
|
|
* @return null|string |
211
|
|
|
*/ |
212
|
|
|
public function getRedirectReferer() |
213
|
|
|
{ |
214
|
|
|
$redirect = $this->parameters->get('redirect'); |
215
|
|
|
$referer = $this->request->headers->get('referer'); |
|
|
|
|
216
|
|
|
|
217
|
|
|
if (!is_array($redirect) || empty($redirect['referer'])) { |
218
|
|
|
return $referer; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
if ($redirect['referer'] === true) { |
222
|
|
|
return $referer; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $redirect['referer']; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param object|null $resource |
230
|
|
|
* |
231
|
|
|
* @return array |
232
|
|
|
*/ |
233
|
|
|
public function getRedirectParameters($resource = null) |
234
|
|
|
{ |
235
|
|
|
$redirect = $this->parameters->get('redirect'); |
236
|
|
|
|
237
|
|
|
if (!is_array($redirect) || empty($redirect['parameters'])) { |
238
|
|
|
$redirect = ['parameters' => []]; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
$parameters = $redirect['parameters']; |
242
|
|
|
|
243
|
|
|
if (null !== $resource) { |
244
|
|
|
$parameters = $this->parseResourceValues($parameters, $resource); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return $parameters; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @return bool |
252
|
|
|
*/ |
253
|
|
|
public function isLimited() |
254
|
|
|
{ |
255
|
|
|
return (bool) $this->parameters->get('limit', false); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @return int|null |
260
|
|
|
*/ |
261
|
|
|
public function getLimit() |
262
|
|
|
{ |
263
|
|
|
$limit = null; |
264
|
|
|
|
265
|
|
|
if ($this->isLimited()) { |
266
|
|
|
$limit = (int) $this->parameters->get('limit', 10); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
return $limit; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @return bool |
274
|
|
|
*/ |
275
|
|
|
public function isPaginated() |
276
|
|
|
{ |
277
|
|
|
return (bool) $this->parameters->get('paginate', true); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @return int |
282
|
|
|
*/ |
283
|
|
|
public function getPaginationMaxPerPage() |
284
|
|
|
{ |
285
|
|
|
return (int) $this->parameters->get('paginate', 10); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @return bool |
290
|
|
|
*/ |
291
|
|
|
public function isFilterable() |
292
|
|
|
{ |
293
|
|
|
return (bool) $this->parameters->get('filterable', false); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @param array $criteria |
298
|
|
|
* |
299
|
|
|
* @return array |
300
|
|
|
*/ |
301
|
|
|
public function getCriteria(array $criteria = []) |
302
|
|
|
{ |
303
|
|
|
$defaultCriteria = array_merge($this->parameters->get('criteria', []), $criteria); |
304
|
|
|
|
305
|
|
|
if ($this->isFilterable()) { |
306
|
|
|
return $this->getRequestParameter('criteria', $defaultCriteria); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return $defaultCriteria; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @return bool |
314
|
|
|
*/ |
315
|
|
|
public function isSortable() |
316
|
|
|
{ |
317
|
|
|
return (bool) $this->parameters->get('sortable', false); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @param array $sorting |
322
|
|
|
* |
323
|
|
|
* @return array |
324
|
|
|
*/ |
325
|
|
|
public function getSorting(array $sorting = []) |
326
|
|
|
{ |
327
|
|
|
$defaultSorting = array_merge($this->parameters->get('sorting', []), $sorting); |
328
|
|
|
|
329
|
|
|
if ($this->isSortable()) { |
330
|
|
|
$sorting = $this->getRequestParameter('sorting'); |
331
|
|
|
foreach ($defaultSorting as $key => $value) { |
332
|
|
|
if (!isset($sorting[$key])) { |
333
|
|
|
$sorting[$key] = $value; |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
return $sorting; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
return $defaultSorting; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @param $parameter |
345
|
|
|
* @param array $defaults |
346
|
|
|
* |
347
|
|
|
* @return array |
348
|
|
|
*/ |
349
|
|
|
public function getRequestParameter($parameter, $defaults = []) |
350
|
|
|
{ |
351
|
|
|
return array_replace_recursive( |
352
|
|
|
$defaults, |
353
|
|
|
$this->request->get($parameter, []) |
354
|
|
|
); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* @return string|null |
359
|
|
|
*/ |
360
|
|
|
public function getRepositoryMethod() |
361
|
|
|
{ |
362
|
|
|
if (!$this->parameters->has('repository')) { |
363
|
|
|
return null; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
$repository = $this->parameters->get('repository'); |
367
|
|
|
|
368
|
|
|
return is_array($repository) ? $repository['method'] : $repository; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* @return array |
373
|
|
|
*/ |
374
|
|
|
public function getRepositoryArguments() |
375
|
|
|
{ |
376
|
|
|
if (!$this->parameters->has('repository')) { |
377
|
|
|
return []; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
$repository = $this->parameters->get('repository'); |
381
|
|
|
|
382
|
|
|
if (!isset($repository['arguments'])) { |
383
|
|
|
return []; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
return is_array($repository['arguments']) ? $repository['arguments'] : [$repository['arguments']]; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* @return string|null |
391
|
|
|
*/ |
392
|
|
|
public function getFactoryMethod() |
393
|
|
|
{ |
394
|
|
|
if (!$this->parameters->has('factory')) { |
395
|
|
|
return null; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
$factory = $this->parameters->get('factory'); |
399
|
|
|
|
400
|
|
|
return is_array($factory) ? $factory['method'] : $factory; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* @return array |
405
|
|
|
*/ |
406
|
|
|
public function getFactoryArguments() |
407
|
|
|
{ |
408
|
|
|
if (!$this->parameters->has('factory')) { |
409
|
|
|
return []; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
$factory = $this->parameters->get('factory'); |
413
|
|
|
|
414
|
|
|
if (!isset($factory['arguments'])) { |
415
|
|
|
return []; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
return is_array($factory['arguments']) ? $factory['arguments'] : [$factory['arguments']]; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* @param null $message |
423
|
|
|
* |
424
|
|
|
* @return mixed|null |
425
|
|
|
*/ |
426
|
|
|
public function getFlashMessage($message) |
427
|
|
|
{ |
428
|
|
|
return $this->parameters->get('flash', sprintf('%s.%s.%s', $this->metadata->getApplicationName(), $this->metadata->getName(), $message)); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* @return mixed|null |
433
|
|
|
*/ |
434
|
|
|
public function getSortablePosition() |
435
|
|
|
{ |
436
|
|
|
return $this->parameters->get('sortable_position', 'position'); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* @return mixed|null |
441
|
|
|
*/ |
442
|
|
|
public function getSerializationGroups() |
443
|
|
|
{ |
444
|
|
|
return $this->parameters->get('serialization_groups', []); |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* @return mixed|null |
449
|
|
|
*/ |
450
|
|
|
public function getSerializationVersion() |
451
|
|
|
{ |
452
|
|
|
return $this->parameters->get('serialization_version'); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* @return string|null |
457
|
|
|
*/ |
458
|
|
|
public function getEvent() |
459
|
|
|
{ |
460
|
|
|
return $this->parameters->get('event'); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* @return bool |
465
|
|
|
*/ |
466
|
|
|
public function hasPermission() |
467
|
|
|
{ |
468
|
|
|
if (!$this->parameters->has('permission')) { |
469
|
|
|
return true; |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
return false !== $this->parameters->get('permission'); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* @param string $name |
477
|
|
|
* |
478
|
|
|
* @return string |
479
|
|
|
* |
480
|
|
|
* @throws \LogicException |
481
|
|
|
*/ |
482
|
|
|
public function getPermission($name) |
483
|
|
|
{ |
484
|
|
|
if (!$this->hasPermission()) { |
485
|
|
|
throw new \LogicException('Current action does not require any authorization.'); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
if (!$this->parameters->has('permission')) { |
489
|
|
|
return sprintf('%s.%s.%s', $this->metadata->getApplicationName(), $this->metadata->getName(), $name); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
return $this->parameters->get('permission'); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* @return bool |
497
|
|
|
*/ |
498
|
|
|
public function isHeaderRedirection() |
499
|
|
|
{ |
500
|
|
|
$redirect = $this->parameters->get('redirect'); |
501
|
|
|
|
502
|
|
|
if (!is_array($redirect) || !isset($redirect['header'])) { |
503
|
|
|
return false; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
if ('xhr' === $redirect['header']) { |
507
|
|
|
return $this->getRequest()->isXmlHttpRequest(); |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
return (bool) $redirect['header']; |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
public function getVars() |
514
|
|
|
{ |
515
|
|
|
return $this->parameters->get('vars', []); |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* @param array $parameters |
520
|
|
|
* @param object $resource |
521
|
|
|
* |
522
|
|
|
* @return array |
523
|
|
|
*/ |
524
|
|
|
private function parseResourceValues(array $parameters, $resource) |
525
|
|
|
{ |
526
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
527
|
|
|
|
528
|
|
|
if (empty($parameters)) { |
529
|
|
|
return ['id' => $accessor->getValue($resource, 'id')]; |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
foreach ($parameters as $key => $value) { |
533
|
|
|
if (is_array($value)) { |
534
|
|
|
$parameters[$key] = $this->parseResourceValues($value, $resource); |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
if (is_string($value) && 0 === strpos($value, 'resource.')) { |
538
|
|
|
$parameters[$key] = $accessor->getValue($resource, substr($value, 9)); |
539
|
|
|
} |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
return $parameters; |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
/** |
546
|
|
|
* @return bool |
547
|
|
|
*/ |
548
|
|
|
public function hasGrid() |
549
|
|
|
{ |
550
|
|
|
return $this->parameters->has('grid'); |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* @return string |
555
|
|
|
* |
556
|
|
|
* @throws \LogicException |
557
|
|
|
*/ |
558
|
|
|
public function getGrid() |
559
|
|
|
{ |
560
|
|
|
if (!$this->hasGrid()) { |
561
|
|
|
throw new \LogicException('Current action does not use grid.'); |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
return $this->parameters->get('grid'); |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
/** |
568
|
|
|
* @return bool |
569
|
|
|
*/ |
570
|
|
|
public function hasStateMachine() |
571
|
|
|
{ |
572
|
|
|
return $this->parameters->has('state_machine'); |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
/** |
576
|
|
|
* @return string |
577
|
|
|
*/ |
578
|
|
|
public function getStateMachineGraph() |
579
|
|
|
{ |
580
|
|
|
return $this->parameters->get('state_machine[graph]', null, true); |
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
/** |
584
|
|
|
* @return string |
585
|
|
|
*/ |
586
|
|
|
public function getStateMachineTransition() |
587
|
|
|
{ |
588
|
|
|
return $this->parameters->get('state_machine[transition]', null, true); |
589
|
|
|
} |
590
|
|
|
} |
591
|
|
|
|