‪TYPO3CMS  11.5
FlashMessageQueue.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
24 
28 class ‪FlashMessageQueue extends \SplQueue implements \JsonSerializable
29 {
35  protected ‪$identifier;
36 
40  public function ‪__construct(‪$identifier)
41  {
42  $this->identifier = ‪$identifier;
43  }
44 
48  public function ‪getIdentifier()
49  {
50  return ‪$this->identifier;
51  }
52 
62  #[\ReturnTypeWillChange]
63  public function ‪enqueue($message): ‪FlashMessageQueue
64  {
65  if (!($message instanceof ‪FlashMessage)) {
66  throw new ‪Exception(
67  'FlashMessageQueue::enqueue() expects an object of type \TYPO3\CMS\Core\Messaging\FlashMessage but got type "' . (is_object($message) ? get_class($message) : gettype($message)) . '"',
68  1376833554
69  );
70  }
71  if ($message->isSessionMessage()) {
72  $this->‪addFlashMessageToSession($message);
73  } else {
74  parent::enqueue($message);
75  }
76  return $this;
77  }
78 
82  public function ‪addMessage(‪FlashMessage $message)
83  {
84  $this->‪enqueue($message);
85  }
86 
92  #[\ReturnTypeWillChange]
93  public function ‪dequeue()
94  {
95  // deliberately empty
96  }
97 
104  protected function ‪addFlashMessageToSession(‪FlashMessage $message)
105  {
106  $queuedFlashMessages = $this->‪getFlashMessagesFromSession();
107  $queuedFlashMessages[] = $message;
108  $this->‪storeFlashMessagesInSession($queuedFlashMessages);
109  }
110 
117  public function ‪getAllMessages($severity = null)
118  {
119  // Get messages from user session
120  $queuedFlashMessagesFromSession = $this->‪getFlashMessagesFromSession();
121  $queuedFlashMessages = array_merge($queuedFlashMessagesFromSession, $this->‪toArray());
122  if ($severity !== null) {
123  $filteredFlashMessages = [];
124  foreach ($queuedFlashMessages as $message) {
125  if ($message->getSeverity() === $severity) {
126  $filteredFlashMessages[] = $message;
127  }
128  }
129  return $filteredFlashMessages;
130  }
131 
132  return $queuedFlashMessages;
133  }
134 
143  public function ‪getAllMessagesAndFlush($severity = null)
144  {
145  $queuedFlashMessages = $this->‪getAllMessages($severity);
146  // Reset messages in user session
147  $this->‪removeAllFlashMessagesFromSession($severity);
148  // Reset internal messages
149  $this->‪clear($severity);
150  return $queuedFlashMessages;
151  }
152 
158  protected function ‪storeFlashMessagesInSession(?array $flashMessages = null)
159  {
160  if (is_array($flashMessages)) {
161  $flashMessages = array_map('json_encode', $flashMessages);
162  }
163  $user = $this->‪getUserByContext();
164  if ($user instanceof AbstractUserAuthentication) {
165  $user->setAndSaveSessionData($this->identifier, $flashMessages);
166  }
167  }
168 
174  protected function ‪removeAllFlashMessagesFromSession($severity = null)
175  {
176  if (!$this->‪getUserByContext() instanceof AbstractUserAuthentication) {
177  return;
178  }
179  if ($severity === null) {
180  $this->‪storeFlashMessagesInSession(null);
181  } else {
182  $messages = $this->‪getFlashMessagesFromSession();
183  foreach ($messages as $index => $message) {
184  if ($message->getSeverity() === $severity) {
185  unset($messages[$index]);
186  }
187  }
188  $this->‪storeFlashMessagesInSession($messages);
189  }
190  }
191 
198  protected function ‪getFlashMessagesFromSession(): array
199  {
200  $sessionMessages = [];
201  $user = $this->‪getUserByContext();
202  if ($user instanceof AbstractUserAuthentication) {
203  $messagesFromSession = $user->getSessionData($this->identifier);
204  $messagesFromSession = is_array($messagesFromSession) ? $messagesFromSession : [];
205  foreach ($messagesFromSession as $messageData) {
206  $sessionMessages[] = ‪FlashMessage::createFromArray(json_decode($messageData, true));
207  }
208  }
209  return $sessionMessages;
210  }
211 
218  protected function ‪getUserByContext(): ?AbstractUserAuthentication
219  {
220  if ((‪$GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController && ‪$GLOBALS['TSFE']->fe_user instanceof FrontendUserAuthentication) {
221  return ‪$GLOBALS['TSFE']->fe_user;
222  }
223  return ‪$GLOBALS['BE_USER'] ?? null;
224  }
225 
232  public function ‪renderFlashMessages(?FlashMessageRendererInterface $flashMessageRenderer = null)
233  {
234  $content = '';
235  $flashMessages = $this->‪getAllMessagesAndFlush();
236 
237  if (!empty($flashMessages)) {
238  if ($flashMessageRenderer === null) {
239  $flashMessageRenderer = GeneralUtility::makeInstance(FlashMessageRendererResolver::class)->resolve();
240  }
241  $content = $flashMessageRenderer->render($flashMessages);
242  }
243 
244  return $content;
245  }
246 
252  public function ‪toArray()
253  {
254  $array = [];
255  $this->rewind();
256  while ($this->valid()) {
257  $array[] = $this->current();
258  $this->next();
259  }
260  return $array;
261  }
262 
268  public function ‪clear($severity = null)
269  {
270  $this->rewind();
271  if ($severity === null) {
272  while (!$this->isEmpty()) {
273  parent::dequeue();
274  }
275  } else {
276  $keysToRemove = [];
277  while ($cur = $this->current()) {
278  if ($cur->getSeverity() === $severity) {
279  $keysToRemove[] = $this->key();
280  }
281  $this->next();
282  }
283  // keys are renumbered when unsetting elements
284  // so unset them from last to first
285  $keysToRemove = array_reverse($keysToRemove);
286  foreach ($keysToRemove as $key) {
287  $this->offsetUnset($key);
288  }
289  }
290  }
291 
295  public function ‪jsonSerialize(): array
296  {
297  return $this->‪toArray();
298  }
299 }
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\renderFlashMessages
‪string renderFlashMessages(?FlashMessageRendererInterface $flashMessageRenderer=null)
Definition: FlashMessageQueue.php:231
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getIdentifier
‪string getIdentifier()
Definition: FlashMessageQueue.php:47
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\$identifier
‪string $identifier
Definition: FlashMessageQueue.php:34
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Messaging\FlashMessage\createFromArray
‪static static createFromArray(array $data)
Definition: FlashMessage.php:55
‪TYPO3\CMS\Core\Exception
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\__construct
‪__construct($identifier)
Definition: FlashMessageQueue.php:39
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\clear
‪clear($severity=null)
Definition: FlashMessageQueue.php:267
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\jsonSerialize
‪array jsonSerialize()
Definition: FlashMessageQueue.php:294
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\addFlashMessageToSession
‪addFlashMessageToSession(FlashMessage $message)
Definition: FlashMessageQueue.php:103
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\enqueue
‪FlashMessageQueue enqueue($message)
Definition: FlashMessageQueue.php:62
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\addMessage
‪addMessage(FlashMessage $message)
Definition: FlashMessageQueue.php:81
‪TYPO3\CMS\Core\Messaging\Renderer\FlashMessageRendererInterface
Definition: FlashMessageRendererInterface.php:26
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getAllMessages
‪FlashMessage[] getAllMessages($severity=null)
Definition: FlashMessageQueue.php:116
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\storeFlashMessagesInSession
‪storeFlashMessagesInSession(?array $flashMessages=null)
Definition: FlashMessageQueue.php:157
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getFlashMessagesFromSession
‪FlashMessage[] getFlashMessagesFromSession()
Definition: FlashMessageQueue.php:197
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getUserByContext
‪AbstractUserAuthentication null getUserByContext()
Definition: FlashMessageQueue.php:217
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\removeAllFlashMessagesFromSession
‪removeAllFlashMessagesFromSession($severity=null)
Definition: FlashMessageQueue.php:173
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:26
‪TYPO3\CMS\Core\Messaging
Definition: AbstractMessage.php:18
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:104
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
Definition: FrontendUserAuthentication.php:32
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getAllMessagesAndFlush
‪FlashMessage[] getAllMessagesAndFlush($severity=null)
Definition: FlashMessageQueue.php:142
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\dequeue
‪dequeue()
Definition: FlashMessageQueue.php:92
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue
Definition: FlashMessageQueue.php:29
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\toArray
‪FlashMessage[] toArray()
Definition: FlashMessageQueue.php:251
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:56