[TASK] Create own response instance in controller actions
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / FlashMessageController.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Controller;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Psr\Http\Message\ResponseInterface;
19 use TYPO3\CMS\Core\Http\JsonResponse;
20 use TYPO3\CMS\Core\Messaging\FlashMessageService;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Render FlashMessages
25 */
26 class FlashMessageController
27 {
28 /**
29 * Renders the FlashMessages from queue and returns them as JSON.
30 *
31 * @return ResponseInterface
32 */
33 public function getQueuedFlashMessagesAction(): ResponseInterface
34 {
35 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
36 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
37 $flashMessages = $defaultFlashMessageQueue->getAllMessagesAndFlush();
38
39 $messages = [];
40 foreach ($flashMessages as $flashMessage) {
41 $messages[] = [
42 'title' => $flashMessage->getTitle(),
43 'message' => $flashMessage->getMessage(),
44 'severity' => $flashMessage->getSeverity()
45 ];
46 }
47
48 return (new JsonResponse())->setPayload($messages);
49 }
50 }