These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the CRUDlex package. |
||
5 | * |
||
6 | * (c) Philip Lehmann-Böhm <[email protected]> |
||
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 CRUDlex; |
||
13 | |||
14 | use League\Flysystem\FilesystemInterface; |
||
15 | use League\Flysystem\Util\MimeType; |
||
16 | use Symfony\Component\HttpFoundation\RedirectResponse; |
||
17 | use Symfony\Component\HttpFoundation\Request; |
||
18 | use Symfony\Component\HttpFoundation\Response; |
||
19 | use Symfony\Component\HttpFoundation\Session\SessionInterface; |
||
20 | use Symfony\Component\HttpFoundation\StreamedResponse; |
||
21 | use Symfony\Component\Translation\TranslatorInterface; |
||
22 | use Twig_Environment; |
||
23 | |||
24 | |||
25 | /** |
||
26 | * Default implementation of the ControllerInterface. |
||
27 | */ |
||
28 | class Controller implements ControllerInterface { |
||
29 | |||
30 | /** |
||
31 | * Often used i18n key. |
||
32 | */ |
||
33 | const TR_RESOURCE_NOT_FOUND = 'crudlex.resourceNotFound'; |
||
34 | |||
35 | /** |
||
36 | * Often used i18n key. |
||
37 | */ |
||
38 | const TR_INSTANCE_NOT_FOUND = 'crudlex.instanceNotFound'; |
||
39 | |||
40 | /** |
||
41 | * Holds the filesystme. |
||
42 | * @var FilesystemInterface |
||
43 | */ |
||
44 | protected $filesystem; |
||
45 | |||
46 | /** |
||
47 | * Holds the session. |
||
48 | * @var SessionInterface |
||
49 | */ |
||
50 | protected $session; |
||
51 | |||
52 | /** |
||
53 | * Holds the translator. |
||
54 | * @var TranslatorInterface |
||
55 | */ |
||
56 | protected $translator; |
||
57 | |||
58 | /** |
||
59 | * Holds the service. |
||
60 | * @var Service |
||
61 | */ |
||
62 | protected $service; |
||
63 | |||
64 | /** |
||
65 | * Holds the Twig instance. |
||
66 | * @var Twig_Environment |
||
67 | */ |
||
68 | protected $twig; |
||
69 | |||
70 | /** |
||
71 | * Postprocesses the entity after modification by handling the uploaded |
||
72 | * files and setting the flash. |
||
73 | * |
||
74 | * @param Request $request |
||
75 | * the current request |
||
76 | * @param AbstractData $crudData |
||
77 | * the data instance of the entity |
||
78 | * @param Entity $instance |
||
79 | * the entity |
||
80 | * @param string $entity |
||
81 | * the name of the entity |
||
82 | * @param string $mode |
||
83 | * whether to 'edit' or to 'create' the entity |
||
84 | * |
||
85 | * @return null|\Symfony\Component\HttpFoundation\RedirectResponse |
||
86 | * the HTTP response of this modification |
||
87 | */ |
||
88 | protected function modifyFilesAndSetFlashBag(Request $request, AbstractData $crudData, Entity $instance, $entity, $mode) |
||
89 | { |
||
90 | $id = $instance->get('id'); |
||
91 | $fileHandler = new FileHandler($this->filesystem, $crudData->getDefinition()); |
||
92 | $result = $mode == 'edit' ? $fileHandler->updateFiles($crudData, $request, $instance, $entity) : $fileHandler->createFiles($crudData, $request, $instance, $entity); |
||
93 | if (!$result) { |
||
94 | return null; |
||
95 | } |
||
96 | $this->session->getFlashBag()->add('success', $this->translator->trans('crudlex.'.$mode.'.success', [ |
||
97 | '%label%' => $crudData->getDefinition()->getLabel(), |
||
98 | '%id%' => $id |
||
99 | ])); |
||
100 | return new RedirectResponse($this->service->generateURL('crudShow', ['entity' => $entity, 'id' => $id])); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Sets the flashes of a failed entity modification. |
||
105 | * |
||
106 | * @param boolean $optimisticLocking |
||
107 | * whether the optimistic locking failed |
||
108 | * @param string $mode |
||
109 | * the modification mode, either 'create' or 'edit' |
||
110 | */ |
||
111 | protected function setValidationFailedFlashes($optimisticLocking, $mode) |
||
112 | { |
||
113 | $this->session->getFlashBag()->add('danger', $this->translator->trans('crudlex.'.$mode.'.error')); |
||
114 | if ($optimisticLocking) { |
||
115 | $this->session->getFlashBag()->add('danger', $this->translator->trans('crudlex.edit.locked')); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Validates and saves the new or updated entity and returns the appropriate HTTP |
||
121 | * response. |
||
122 | * |
||
123 | * @param Request $request |
||
124 | * the current request |
||
125 | * @param AbstractData $crudData |
||
126 | * the data instance of the entity |
||
127 | * @param Entity $instance |
||
128 | * the entity |
||
129 | * @param string $entity |
||
130 | * the name of the entity |
||
131 | * @param boolean $edit |
||
132 | * whether to edit (true) or to create (false) the entity |
||
133 | * |
||
134 | * @return Response |
||
135 | * the HTTP response of this modification |
||
136 | */ |
||
137 | protected function modifyEntity(Request $request, AbstractData $crudData, Entity $instance, $entity, $edit) |
||
138 | { |
||
139 | $fieldErrors = []; |
||
140 | $mode = $edit ? 'edit' : 'create'; |
||
141 | if ($request->getMethod() == 'POST') { |
||
142 | $instance->populateViaRequest($request); |
||
143 | $validator = new EntityValidator($instance); |
||
144 | $validation = $validator->validate($crudData, intval($request->get('version'))); |
||
145 | |||
146 | $fieldErrors = $validation['errors']; |
||
147 | if (!$validation['valid']) { |
||
148 | $optimisticLocking = isset($fieldErrors['version']); |
||
149 | $this->setValidationFailedFlashes($optimisticLocking, $mode); |
||
150 | } else { |
||
151 | $modified = $edit ? $crudData->update($instance) : $crudData->create($instance); |
||
152 | $response = $modified ? $this->modifyFilesAndSetFlashBag($request, $crudData, $instance, $entity, $mode) : false; |
||
153 | if ($response) { |
||
154 | return $response; |
||
155 | } |
||
156 | $this->session->getFlashBag()->add('danger', $this->translator->trans('crudlex.'.$mode.'.failed')); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | return new Response($this->twig->render($this->service->getTemplate('template', 'form', $entity), [ |
||
161 | 'crud' => $this->service, |
||
162 | 'crudEntity' => $entity, |
||
163 | 'crudData' => $crudData, |
||
164 | 'entity' => $instance, |
||
165 | 'mode' => $mode, |
||
166 | 'fieldErrors' => $fieldErrors, |
||
167 | 'layout' => $this->service->getTemplate('layout', $mode, $entity) |
||
168 | ])); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Gets the parameters for the redirection after deleting an entity. |
||
173 | * |
||
174 | * @param Request $request |
||
175 | * the current request |
||
176 | * @param string $entity |
||
177 | * the entity name |
||
178 | * @param string $redirectPage |
||
179 | * reference, where the page to redirect to will be stored |
||
180 | * |
||
181 | * @return array<string,string> |
||
182 | * the parameters of the redirection, entity and id |
||
183 | */ |
||
184 | protected function getAfterDeleteRedirectParameters(Request $request, $entity, &$redirectPage) |
||
185 | { |
||
186 | $redirectPage = 'crudList'; |
||
187 | $redirectParameters = ['entity' => $entity]; |
||
188 | $redirectEntity = $request->get('redirectEntity'); |
||
189 | $redirectId = $request->get('redirectId'); |
||
190 | if ($redirectEntity && $redirectId) { |
||
191 | $redirectPage = 'crudShow'; |
||
192 | $redirectParameters = [ |
||
193 | 'entity' => $redirectEntity, |
||
194 | 'id' => $redirectId |
||
195 | ]; |
||
196 | } |
||
197 | return $redirectParameters; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Builds up the parameters of the list page filters. |
||
202 | * |
||
203 | * @param Request $request |
||
204 | * the current request |
||
205 | * @param EntityDefinition $definition |
||
206 | * the current entity definition |
||
207 | * @param array &$filter |
||
208 | * will hold a map of fields to request parameters for the filters |
||
209 | * @param boolean $filterActive |
||
210 | * reference, will be true if at least one filter is active |
||
211 | * @param array $filterToUse |
||
212 | * reference, will hold a map of fields to integers (0 or 1) which boolean filters are active |
||
213 | * @param array $filterOperators |
||
214 | * reference, will hold a map of fields to operators for AbstractData::listEntries() |
||
215 | * |
||
216 | * @return array |
||
217 | * the raw filter query parameters |
||
218 | */ |
||
219 | protected function buildUpListFilter(Request $request, EntityDefinition $definition, &$filter, &$filterActive, &$filterToUse, &$filterOperators) |
||
220 | { |
||
221 | $rawFilter = []; |
||
222 | foreach ($definition->getFilter() as $filterField) { |
||
223 | $type = $definition->getType($filterField); |
||
224 | $filter[$filterField] = $request->get('crudFilter'.$filterField); |
||
225 | $rawFilter[$filterField] = $filter[$filterField]; |
||
226 | if ($filter[$filterField]) { |
||
227 | $filterActive = true; |
||
228 | $filterToUse[$filterField] = $filter[$filterField]; |
||
229 | $filterOperators[$filterField] = '='; |
||
230 | if ($type === 'boolean') { |
||
231 | $filterToUse[$filterField] = $filter[$filterField] == 'true' ? 1 : 0; |
||
232 | } else if ($type === 'reference') { |
||
233 | $filter[$filterField] = ['id' => $filter[$filterField]]; |
||
234 | } else if ($type === 'many') { |
||
235 | $filter[$filterField] = array_map(function($value) { |
||
236 | return ['id' => $value]; |
||
237 | }, $filter[$filterField]); |
||
238 | $filterToUse[$filterField] = $filter[$filterField]; |
||
239 | } else if (in_array($type, ['text', 'multiline', 'fixed'])) { |
||
240 | $filterToUse[$filterField] = '%'.$filter[$filterField].'%'; |
||
241 | $filterOperators[$filterField] = 'LIKE'; |
||
242 | } |
||
243 | } |
||
244 | } |
||
245 | return $rawFilter; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Generates the not found page. |
||
250 | * |
||
251 | * @param string $error |
||
252 | * the cause of the not found error |
||
253 | * |
||
254 | * @return Response |
||
255 | * the rendered not found page with the status code 404 |
||
256 | */ |
||
257 | protected function getNotFoundPage($error) |
||
258 | { |
||
259 | return new Response($this->twig->render('@crud/notFound.twig', [ |
||
260 | 'crud' => $this->service, |
||
261 | 'error' => $error, |
||
262 | 'crudEntity' => '', |
||
263 | 'layout' => $this->service->getTemplate('layout', '', '') |
||
264 | ]), 404); |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Controller constructor. |
||
269 | * |
||
270 | * @param Service $service |
||
271 | * the CRUDlex service |
||
272 | * @param FilesystemInterface $filesystem |
||
273 | * the used filesystem |
||
274 | * @param \Twig\Environment $twig |
||
275 | * the Twig environment |
||
276 | * @param SessionInterface $session |
||
277 | * the session service |
||
278 | * @param TranslatorInterface $translator |
||
279 | * the translation service |
||
280 | */ |
||
281 | public function __construct(Service $service, FilesystemInterface $filesystem, \Twig\Environment $twig, SessionInterface $session, TranslatorInterface $translator) |
||
282 | { |
||
283 | $this->service = $service; |
||
284 | $this->filesystem = $filesystem; |
||
285 | $this->twig = $twig; |
||
286 | $this->session = $session; |
||
287 | $this->translator = $translator; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * {@inheritdoc} |
||
292 | */ |
||
293 | public function setLocaleAndCheckEntity(Request $request) |
||
294 | { |
||
295 | $locale = $this->translator->getLocale(); |
||
296 | $this->service->setLocale($locale); |
||
297 | if (!$this->service->getData($request->get('entity'))) { |
||
298 | return $this->getNotFoundPage($this->translator->trans('crudlex.entityNotFound')); |
||
299 | } |
||
300 | return null; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * {@inheritdoc} |
||
305 | */ |
||
306 | public function create(Request $request, $entity) |
||
307 | { |
||
308 | $crudData = $this->service->getData($entity); |
||
309 | if (!$crudData) { |
||
310 | return $this->getNotFoundPage($this->translator->trans(static::TR_RESOURCE_NOT_FOUND)); |
||
311 | } |
||
312 | $instance = $crudData->createEmpty(); |
||
313 | $instance->populateViaRequest($request); |
||
314 | return $this->modifyEntity($request, $crudData, $instance, $entity, false); |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * {@inheritdoc} |
||
319 | */ |
||
320 | public function showList(Request $request, $entity) |
||
321 | { |
||
322 | $crudData = $this->service->getData($entity); |
||
323 | if (!$crudData) { |
||
324 | return $this->getNotFoundPage($this->translator->trans(static::TR_RESOURCE_NOT_FOUND)); |
||
325 | } |
||
326 | $definition = $crudData->getDefinition(); |
||
327 | |||
328 | $filter = []; |
||
329 | $filterActive = false; |
||
330 | $filterToUse = []; |
||
331 | $filterOperators = []; |
||
332 | $rawFilter = $this->buildUpListFilter($request, $definition, $filter, $filterActive, $filterToUse, $filterOperators); |
||
333 | |||
334 | $pageSize = $definition->getPageSize(); |
||
335 | $total = $crudData->countBy($definition->getTable(), $filterToUse, $filterOperators, true); |
||
336 | $page = abs(intval($request->get('crudPage', 0))); |
||
337 | $maxPage = intval($total / $pageSize); |
||
338 | if ($total % $pageSize == 0) { |
||
339 | $maxPage--; |
||
340 | } |
||
341 | if ($page > $maxPage) { |
||
342 | $page = $maxPage; |
||
343 | } |
||
344 | $skip = $page * $pageSize; |
||
345 | |||
346 | $sortField = $request->get('crudSortField', $definition->getInitialSortField()); |
||
347 | $sortAscendingRequest = $request->get('crudSortAscending'); |
||
348 | $sortAscending = $sortAscendingRequest !== null ? $sortAscendingRequest === 'true' : $definition->isInitialSortAscending(); |
||
349 | |||
350 | $entities = $crudData->listEntries($filterToUse, $filterOperators, $skip, $pageSize, $sortField, $sortAscending); |
||
351 | |||
352 | return new Response($this->twig->render($this->service->getTemplate('template', 'list', $entity), [ |
||
353 | 'crud' => $this->service, |
||
354 | 'crudEntity' => $entity, |
||
355 | 'crudData' => $crudData, |
||
356 | 'definition' => $definition, |
||
357 | 'entities' => $entities, |
||
358 | 'pageSize' => $pageSize, |
||
359 | 'maxPage' => $maxPage, |
||
360 | 'page' => $page, |
||
361 | 'total' => $total, |
||
362 | 'filter' => $filter, |
||
363 | 'rawFilter' => $rawFilter, |
||
364 | 'filterActive' => $filterActive, |
||
365 | 'sortField' => $sortField, |
||
366 | 'sortAscending' => $sortAscending, |
||
367 | 'layout' => $this->service->getTemplate('layout', 'list', $entity) |
||
368 | ])); |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * {@inheritdoc} |
||
373 | */ |
||
374 | public function show($entity, $id) |
||
375 | { |
||
376 | $crudData = $this->service->getData($entity); |
||
377 | if (!$crudData) { |
||
378 | return $this->getNotFoundPage($this->translator->trans(static::TR_RESOURCE_NOT_FOUND)); |
||
379 | } |
||
380 | $instance = $crudData->get($id); |
||
381 | if (!$instance) { |
||
382 | return $this->getNotFoundPage($this->translator->trans(static::TR_INSTANCE_NOT_FOUND)); |
||
383 | } |
||
384 | $definition = $crudData->getDefinition(); |
||
385 | |||
386 | $childrenLabelFields = $definition->getChildrenLabelFields(); |
||
387 | $children = []; |
||
388 | if (count($childrenLabelFields) > 0) { |
||
389 | foreach ($definition->getChildren() as $child) { |
||
390 | $childField = $child[1]; |
||
391 | $childEntity = $child[2]; |
||
392 | $childLabelField = array_key_exists($childEntity, $childrenLabelFields) ? $childrenLabelFields[$childEntity] : 'id'; |
||
393 | $childCrud = $this->service->getData($childEntity); |
||
394 | $children[] = [ |
||
395 | $childCrud->getDefinition()->getLabel(), |
||
396 | $childEntity, |
||
397 | $childLabelField, |
||
398 | $childCrud->listEntries([$childField => $instance->get('id')]), |
||
399 | $childField |
||
400 | ]; |
||
401 | } |
||
402 | } |
||
403 | |||
404 | return new Response($this->twig->render($this->service->getTemplate('template', 'show', $entity), [ |
||
405 | 'crud' => $this->service, |
||
406 | 'crudEntity' => $entity, |
||
407 | 'entity' => $instance, |
||
408 | 'children' => $children, |
||
409 | 'layout' => $this->service->getTemplate('layout', 'show', $entity) |
||
410 | ])); |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * {@inheritdoc} |
||
415 | */ |
||
416 | public function edit(Request $request, $entity, $id) |
||
417 | { |
||
418 | $crudData = $this->service->getData($entity); |
||
419 | if (!$crudData) { |
||
420 | return $this->getNotFoundPage($this->translator->trans(static::TR_RESOURCE_NOT_FOUND)); |
||
421 | } |
||
422 | $instance = $crudData->get($id); |
||
423 | if (!$instance) { |
||
424 | return $this->getNotFoundPage($this->translator->trans(static::TR_INSTANCE_NOT_FOUND)); |
||
425 | } |
||
426 | |||
427 | return $this->modifyEntity($request, $crudData, $instance, $entity, true); |
||
428 | } |
||
429 | |||
430 | /** |
||
431 | * {@inheritdoc} |
||
432 | */ |
||
433 | public function delete(Request $request, $entity, $id) |
||
434 | { |
||
435 | $crudData = $this->service->getData($entity); |
||
436 | if (!$crudData) { |
||
437 | return $this->getNotFoundPage($this->translator->trans(static::TR_RESOURCE_NOT_FOUND)); |
||
438 | } |
||
439 | $instance = $crudData->get($id); |
||
440 | if (!$instance) { |
||
441 | return $this->getNotFoundPage($this->translator->trans(static::TR_INSTANCE_NOT_FOUND); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
442 | } |
||
443 | |||
444 | $fileHandler = new FileHandler($this->filesystem, $crudData->getDefinition()); |
||
445 | $filesDeleted = $fileHandler->deleteFiles($crudData, $instance, $entity); |
||
446 | $deleted = $filesDeleted ? $crudData->delete($instance) : AbstractData::DELETION_FAILED_EVENT; |
||
447 | |||
448 | if ($deleted === AbstractData::DELETION_FAILED_EVENT) { |
||
449 | $this->session->getFlashBag()->add('danger', $this->translator->trans('crudlex.delete.failed')); |
||
450 | return new RedirectResponse($this->service->generateURL('crudShow', ['entity' => $entity, 'id' => $id])); |
||
451 | } elseif ($deleted === AbstractData::DELETION_FAILED_STILL_REFERENCED) { |
||
452 | $this->session->getFlashBag()->add('danger', $this->translator->trans('crudlex.delete.error', [ |
||
453 | '%label%' => $crudData->getDefinition()->getLabel() |
||
454 | ])); |
||
455 | return new RedirectResponse($this->service->generateURL('crudShow', ['entity' => $entity, 'id' => $id])); |
||
456 | } |
||
457 | |||
458 | $redirectPage = 'crudList'; |
||
459 | $redirectParameters = $this->getAfterDeleteRedirectParameters($request, $entity, $redirectPage); |
||
460 | |||
461 | $this->session->getFlashBag()->add('success', $this->translator->trans('crudlex.delete.success', [ |
||
462 | '%label%' => $crudData->getDefinition()->getLabel() |
||
463 | ])); |
||
464 | return new RedirectResponse($this->service->generateURL($redirectPage, $redirectParameters)); |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * {@inheritdoc} |
||
469 | */ |
||
470 | public function renderFile($entity, $id, $field) |
||
471 | { |
||
472 | $crudData = $this->service->getData($entity); |
||
473 | if (!$crudData) { |
||
474 | return $this->getNotFoundPage($this->translator->trans(static::TR_RESOURCE_NOT_FOUND)); |
||
475 | } |
||
476 | $instance = $crudData->get($id); |
||
477 | $definition = $crudData->getDefinition(); |
||
478 | if (!$instance || $definition->getType($field) != 'file' || !$instance->get($field)) { |
||
479 | return $this->getNotFoundPage($this->translator->trans(static::TR_INSTANCE_NOT_FOUND)); |
||
480 | } |
||
481 | $fileHandler = new FileHandler($this->filesystem, $definition); |
||
482 | return $fileHandler->renderFile($instance, $entity, $field); |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * {@inheritdoc} |
||
487 | */ |
||
488 | public function deleteFile($entity, $id, $field) |
||
489 | { |
||
490 | $crudData = $this->service->getData($entity); |
||
491 | if (!$crudData) { |
||
492 | return $this->getNotFoundPage($this->translator->trans(static::TR_RESOURCE_NOT_FOUND)); |
||
493 | } |
||
494 | $instance = $crudData->get($id); |
||
495 | if (!$instance) { |
||
496 | return $this->getNotFoundPage($this->translator->trans(static::TR_INSTANCE_NOT_FOUND)); |
||
497 | } |
||
498 | $fileHandler = new FileHandler($this->filesystem, $crudData->getDefinition()); |
||
499 | if (!$crudData->getDefinition()->getField($field, 'required', false) && $fileHandler->deleteFile($crudData, $instance, $entity, $field)) { |
||
500 | $instance->set($field, ''); |
||
501 | $crudData->update($instance); |
||
502 | $this->session->getFlashBag()->add('success', $this->translator->trans('crudlex.file.deleted')); |
||
503 | } else { |
||
504 | $this->session->getFlashBag()->add('danger', $this->translator->trans('crudlex.file.notDeleted')); |
||
505 | } |
||
506 | return new RedirectResponse($this->service->generateURL('crudShow', ['entity' => $entity, 'id' => $id])); |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * {@inheritdoc} |
||
511 | */ |
||
512 | public function staticFile(Request $request) |
||
513 | { |
||
514 | $fileParam = str_replace('..', '', $request->get('file')); |
||
515 | $file = __DIR__.'/../static/'.$fileParam; |
||
516 | if (!$fileParam || !file_exists($file)) { |
||
517 | return $this->getNotFoundPage($this->translator->trans(static::TR_RESOURCE_NOT_FOUND)); |
||
518 | } |
||
519 | |||
520 | $mimeType = MimeType::detectByFilename($file); |
||
521 | $size = filesize($file); |
||
522 | |||
523 | $streamedFileResponse = new StreamedFileResponse(); |
||
524 | $response = new StreamedResponse($streamedFileResponse->getStreamedFileFunction($file), 200, [ |
||
525 | 'Content-Type' => $mimeType, |
||
526 | 'Content-Disposition' => 'attachment; filename="'.basename($file).'"', |
||
527 | 'Content-length' => $size |
||
528 | ]); |
||
529 | |||
530 | $response->setETag(filemtime($file))->setPublic()->isNotModified($request); |
||
531 | $response->send(); |
||
532 | |||
533 | return $response; |
||
534 | } |
||
535 | |||
536 | /** |
||
537 | * {@inheritdoc} |
||
538 | */ |
||
539 | public function setLocale(Request $request, $locale) |
||
540 | { |
||
541 | |||
542 | if (!in_array($locale, $this->service->getLocales())) { |
||
543 | return $this->getNotFoundPage('Locale '.$locale.' not found.'); |
||
544 | } |
||
545 | |||
546 | $manageI18n = $this->service->isManageI18n(); |
||
547 | if ($manageI18n) { |
||
548 | $this->session->set('locale', $locale); |
||
549 | } |
||
550 | $redirect = $request->get('redirect'); |
||
551 | return new RedirectResponse($redirect); |
||
552 | } |
||
553 | } |
||
554 |