‪TYPO3CMS  ‪main
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 {
30  public const ‪FLASHMESSAGE_QUEUE = 'core.template.flashMessages';
31  public const ‪NOTIFICATION_QUEUE = 'core.template.notifications';
32 
38  protected ‪$identifier;
39 
43  public function ‪__construct(‪$identifier)
44  {
45  $this->identifier = ‪$identifier;
46  }
47 
51  public function ‪getIdentifier()
52  {
53  return ‪$this->identifier;
54  }
55 
63  public function ‪enqueue($message): void
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 "' . get_debug_type($message) . '"',
68  1376833554
69  );
70  }
71  if ($message->isSessionMessage()) {
72  $this->‪addFlashMessageToSession($message);
73  } else {
74  parent::enqueue($message);
75  }
76  }
77 
78  public function ‪addMessage(‪FlashMessage $message)
79  {
80  $this->‪enqueue($message);
81  }
82 
88  public function ‪dequeue(): mixed
89  {
90  // deliberately empty
91  return null;
92  }
93 
98  protected function ‪addFlashMessageToSession(FlashMessage $message)
99  {
100  $queuedFlashMessages = $this->‪getFlashMessagesFromSession();
101  $queuedFlashMessages[] = $message;
102  $this->‪storeFlashMessagesInSession($queuedFlashMessages);
103  }
104 
111  public function ‪getAllMessages(‪ContextualFeedbackSeverity|null $severity = null)
112  {
113  // Get messages from user session
114  $queuedFlashMessagesFromSession = $this->‪getFlashMessagesFromSession();
115  $queuedFlashMessages = array_merge($queuedFlashMessagesFromSession, $this->‪toArray());
116  if ($severity !== null) {
117  $filteredFlashMessages = [];
118  foreach ($queuedFlashMessages as $message) {
119  if ($message->getSeverity() === $severity) {
120  $filteredFlashMessages[] = $message;
121  }
122  }
123  return $filteredFlashMessages;
124  }
125 
126  return $queuedFlashMessages;
127  }
128 
137  public function ‪getAllMessagesAndFlush(‪ContextualFeedbackSeverity|null $severity = null)
138  {
139  $queuedFlashMessages = $this->‪getAllMessages($severity);
140  // Reset messages in user session
141  $this->‪removeAllFlashMessagesFromSession($severity);
142  // Reset internal messages
143  $this->‪clear($severity);
144  return $queuedFlashMessages;
145  }
146 
152  protected function ‪storeFlashMessagesInSession(array $flashMessages = null)
153  {
154  if (is_array($flashMessages)) {
155  $flashMessages = array_map(json_encode(...), $flashMessages);
156  }
157  $user = $this->‪getUserByContext();
158  $user?->setAndSaveSessionData($this->identifier, $flashMessages);
159  }
160 
166  protected function ‪removeAllFlashMessagesFromSession(‪ContextualFeedbackSeverity|null $severity = null)
167  {
168  if (!$this->‪getUserByContext() instanceof AbstractUserAuthentication) {
169  return;
170  }
171  if ($severity === null) {
173  } else {
174  $messages = $this->‪getFlashMessagesFromSession();
175  foreach ($messages as $index => $message) {
176  if ($message->getSeverity() === $severity) {
177  unset($messages[$index]);
178  }
179  }
180  $this->‪storeFlashMessagesInSession($messages);
181  }
182  }
183 
190  protected function ‪getFlashMessagesFromSession(): array
191  {
192  $sessionMessages = [];
193  $user = $this->‪getUserByContext();
194  if ($user !== null) {
195  $messagesFromSession = $user->getSessionData($this->identifier);
196  $messagesFromSession = is_array($messagesFromSession) ? $messagesFromSession : [];
197  foreach ($messagesFromSession as $messageData) {
198  $sessionMessages[] = ‪FlashMessage::createFromArray(json_decode($messageData, true));
199  }
200  }
201  return $sessionMessages;
202  }
203 
212  protected function ‪getUserByContext(): ?AbstractUserAuthentication
213  {
214  if ((‪$GLOBALS['TYPO3_REQUEST'] ?? null)
215  && ‪$GLOBALS['TYPO3_REQUEST']->getAttribute('frontend.user') instanceof FrontendUserAuthentication
216  ) {
217  return ‪$GLOBALS['TYPO3_REQUEST']->getAttribute('frontend.user');
218  }
219  return ‪$GLOBALS['BE_USER'] ?? null;
220  }
221 
228  public function ‪renderFlashMessages(FlashMessageRendererInterface $flashMessageRenderer = null)
229  {
230  $content = '';
231  $flashMessages = $this->‪getAllMessagesAndFlush();
232 
233  if (!empty($flashMessages)) {
234  if ($flashMessageRenderer === null) {
235  $flashMessageRenderer = GeneralUtility::makeInstance(FlashMessageRendererResolver::class)->resolve();
236  }
237  $content = $flashMessageRenderer->render($flashMessages);
238  }
239 
240  return $content;
241  }
242 
248  public function ‪toArray()
249  {
250  $array = [];
251  $this->rewind();
252  while ($this->valid()) {
253  $array[] = $this->current();
254  $this->next();
255  }
256  return $array;
257  }
258 
264  public function ‪clear(‪ContextualFeedbackSeverity|null $severity = null)
265  {
266  $this->rewind();
267  if ($severity === null) {
268  while (!$this->isEmpty()) {
269  parent::dequeue();
270  }
271  } else {
272  $keysToRemove = [];
273  while ($cur = $this->current()) {
274  if ($cur->getSeverity() === $severity) {
275  $keysToRemove[] = $this->key();
276  }
277  $this->next();
278  }
279  // keys are renumbered when unsetting elements
280  // so unset them from last to first
281  $keysToRemove = array_reverse($keysToRemove);
282  foreach ($keysToRemove as $key) {
283  $this->offsetUnset($key);
284  }
285  }
286  }
287 
291  public function ‪jsonSerialize(): array
292  {
293  return $this->‪toArray();
294  }
295 }
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getIdentifier
‪string getIdentifier()
Definition: FlashMessageQueue.php:50
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\$identifier
‪string $identifier
Definition: FlashMessageQueue.php:37
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Messaging\FlashMessage\createFromArray
‪static static createFromArray(array $data)
Definition: FlashMessage.php:56
‪TYPO3\CMS\Core\Exception
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\__construct
‪__construct($identifier)
Definition: FlashMessageQueue.php:42
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getAllMessages
‪FlashMessage[] getAllMessages(ContextualFeedbackSeverity|null $severity=null)
Definition: FlashMessageQueue.php:110
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\jsonSerialize
‪array jsonSerialize()
Definition: FlashMessageQueue.php:290
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\addFlashMessageToSession
‪addFlashMessageToSession(FlashMessage $message)
Definition: FlashMessageQueue.php:97
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\addMessage
‪addMessage(FlashMessage $message)
Definition: FlashMessageQueue.php:77
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\FLASHMESSAGE_QUEUE
‪const FLASHMESSAGE_QUEUE
Definition: FlashMessageQueue.php:30
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\clear
‪clear(ContextualFeedbackSeverity|null $severity=null)
Definition: FlashMessageQueue.php:263
‪TYPO3\CMS\Core\Type\ContextualFeedbackSeverity
‪ContextualFeedbackSeverity
Definition: ContextualFeedbackSeverity.php:25
‪TYPO3\CMS\Core\Messaging\Renderer\FlashMessageRendererInterface
Definition: FlashMessageRendererInterface.php:26
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\storeFlashMessagesInSession
‪storeFlashMessagesInSession(array $flashMessages=null)
Definition: FlashMessageQueue.php:151
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\NOTIFICATION_QUEUE
‪const NOTIFICATION_QUEUE
Definition: FlashMessageQueue.php:31
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getFlashMessagesFromSession
‪FlashMessage[] getFlashMessagesFromSession()
Definition: FlashMessageQueue.php:189
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\removeAllFlashMessagesFromSession
‪removeAllFlashMessagesFromSession(ContextualFeedbackSeverity|null $severity=null)
Definition: FlashMessageQueue.php:165
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:27
‪TYPO3\CMS\Core\Messaging
Definition: AbstractMessage.php:18
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getUserByContext
‪getUserByContext()
Definition: FlashMessageQueue.php:211
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\enqueue
‪enqueue($message)
Definition: FlashMessageQueue.php:62
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
Definition: FrontendUserAuthentication.php:33
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\dequeue
‪dequeue()
Definition: FlashMessageQueue.php:87
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue
Definition: FlashMessageQueue.php:29
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\toArray
‪FlashMessage[] toArray()
Definition: FlashMessageQueue.php:247
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getAllMessagesAndFlush
‪FlashMessage[] getAllMessagesAndFlush(ContextualFeedbackSeverity|null $severity=null)
Definition: FlashMessageQueue.php:136
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\renderFlashMessages
‪string renderFlashMessages(FlashMessageRendererInterface $flashMessageRenderer=null)
Definition: FlashMessageQueue.php:227
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:64