‪TYPO3CMS  9.5
FlashMessageQueue.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
20 
24 class ‪FlashMessageQueue extends \SplQueue implements \JsonSerializable
25 {
31  protected ‪$identifier;
32 
36  public function ‪__construct(‪$identifier)
37  {
38  $this->identifier = ‪$identifier;
39  }
40 
44  public function ‪getIdentifier()
45  {
46  return ‪$this->identifier;
47  }
48 
57  public function ‪enqueue($message): ‪FlashMessageQueue
58  {
59  if (!($message instanceof ‪FlashMessage)) {
60  throw new \TYPO3\CMS\Core\Exception(
61  'FlashMessageQueue::enqueue() expects an object of type \TYPO3\CMS\Core\Messaging\FlashMessage but got type "' . (is_object($message) ? get_class($message) : gettype($message)) . '"',
62  1376833554
63  );
64  }
65  if ($message->isSessionMessage()) {
66  $this->‪addFlashMessageToSession($message);
67  } else {
68  parent::enqueue($message);
69  }
70  return $this;
71  }
72 
76  public function ‪addMessage(‪FlashMessage $message)
77  {
78  $this->‪enqueue($message);
79  }
80 
84  public function ‪dequeue()
85  {
86  // deliberately empty
87  }
88 
95  protected function ‪addFlashMessageToSession(‪FlashMessage $message)
96  {
97  $queuedFlashMessages = $this->‪getFlashMessagesFromSession();
98  $queuedFlashMessages[] = $message;
99  $this->‪storeFlashMessagesInSession($queuedFlashMessages);
100  }
101 
108  public function ‪getAllMessages($severity = null)
109  {
110  // Get messages from user session
111  $queuedFlashMessagesFromSession = $this->‪getFlashMessagesFromSession();
112  $queuedFlashMessages = array_merge($queuedFlashMessagesFromSession, $this->‪toArray());
113  if ($severity !== null) {
114  $filteredFlashMessages = [];
115  foreach ($queuedFlashMessages as $message) {
116  if ($message->getSeverity() === $severity) {
117  $filteredFlashMessages[] = $message;
118  }
119  }
120  return $filteredFlashMessages;
121  }
122 
123  return $queuedFlashMessages;
124  }
125 
134  public function ‪getAllMessagesAndFlush($severity = null)
135  {
136  $queuedFlashMessages = $this->‪getAllMessages($severity);
137  // Reset messages in user session
138  $this->‪removeAllFlashMessagesFromSession($severity);
139  // Reset internal messages
140  $this->‪clear($severity);
141  return $queuedFlashMessages;
142  }
143 
149  protected function ‪storeFlashMessagesInSession(array $flashMessages = null)
150  {
151  $this->‪getUserByContext()->‪setAndSaveSessionData($this->identifier, $flashMessages);
152  }
153 
159  protected function ‪removeAllFlashMessagesFromSession($severity = null)
160  {
161  if (!$this->‪getUserByContext() instanceof ‪AbstractUserAuthentication) {
162  return;
163  }
164  if ($severity === null) {
165  $this->‪storeFlashMessagesInSession(null);
166  } else {
167  $messages = $this->‪getFlashMessagesFromSession();
168  foreach ($messages as $index => $message) {
169  if ($message->‪getSeverity() === $severity) {
170  unset($messages[$index]);
171  }
172  }
173  $this->‪storeFlashMessagesInSession($messages);
174  }
175  }
176 
183  protected function ‪getFlashMessagesFromSession(): array
184  {
185  $sessionMessages = [];
186  $user = $this->‪getUserByContext();
187  if ($user instanceof ‪AbstractUserAuthentication) {
188  $sessionMessages = $user->getSessionData($this->identifier);
189  $sessionMessages = is_array($sessionMessages) ? $sessionMessages : [];
190  }
191  return $sessionMessages;
192  }
193 
199  protected function ‪getUserByContext()
200  {
201  return TYPO3_MODE === 'BE' ? ‪$GLOBALS['BE_USER'] : ‪$GLOBALS['TSFE']->fe_user;
202  }
203 
210  public function ‪renderFlashMessages(‪FlashMessageRendererInterface $flashMessageRenderer = null)
211  {
212  $content = '';
213  $flashMessages = $this->‪getAllMessagesAndFlush();
214 
215  if (!empty($flashMessages)) {
216  if ($flashMessageRenderer === null) {
217  $flashMessageRenderer = GeneralUtility::makeInstance(FlashMessageRendererResolver::class)->resolve();
218  }
219  $content = $flashMessageRenderer->render($flashMessages);
220  }
221 
222  return $content;
223  }
224 
230  public function ‪toArray()
231  {
232  $array = [];
233  $this->rewind();
234  while ($this->valid()) {
235  $array[] = $this->current();
236  $this->next();
237  }
238  return $array;
239  }
240 
246  public function ‪clear($severity = null)
247  {
248  $this->rewind();
249  if ($severity === null) {
250  while (!$this->isEmpty()) {
251  parent::dequeue();
252  }
253  } else {
254  $keysToRemove = [];
255  while ($cur = $this->current()) {
256  if ($cur->getSeverity() === $severity) {
257  $keysToRemove[] = $this->key();
258  }
259  $this->next();
260  }
261  // keys are renumbered when unsetting elements
262  // so unset them from last to first
263  $keysToRemove = array_reverse($keysToRemove);
264  foreach ($keysToRemove as $key) {
265  $this->offsetUnset($key);
266  }
267  }
268  }
269 
273  public function ‪jsonSerialize(): array
274  {
275  return $this->‪toArray();
276  }
277 }
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getIdentifier
‪string getIdentifier()
Definition: FlashMessageQueue.php:43
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\$identifier
‪string $identifier
Definition: FlashMessageQueue.php:30
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\__construct
‪__construct($identifier)
Definition: FlashMessageQueue.php:35
‪TYPO3\CMS\Core\Messaging\AbstractMessage\getSeverity
‪int getSeverity()
Definition: AbstractMessage.php:94
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\clear
‪clear($severity=null)
Definition: FlashMessageQueue.php:245
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\jsonSerialize
‪array jsonSerialize()
Definition: FlashMessageQueue.php:272
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\addFlashMessageToSession
‪addFlashMessageToSession(FlashMessage $message)
Definition: FlashMessageQueue.php:94
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\enqueue
‪FlashMessageQueue enqueue($message)
Definition: FlashMessageQueue.php:56
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\addMessage
‪addMessage(FlashMessage $message)
Definition: FlashMessageQueue.php:75
‪TYPO3\CMS\Core\Messaging\Renderer\FlashMessageRendererInterface
Definition: FlashMessageRendererInterface.php:24
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\storeFlashMessagesInSession
‪storeFlashMessagesInSession(array $flashMessages=null)
Definition: FlashMessageQueue.php:148
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getAllMessages
‪FlashMessage[] getAllMessages($severity=null)
Definition: FlashMessageQueue.php:107
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\setAndSaveSessionData
‪setAndSaveSessionData($key, $data)
Definition: AbstractUserAuthentication.php:1285
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getFlashMessagesFromSession
‪FlashMessage[] getFlashMessagesFromSession()
Definition: FlashMessageQueue.php:182
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\removeAllFlashMessagesFromSession
‪removeAllFlashMessagesFromSession($severity=null)
Definition: FlashMessageQueue.php:158
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:22
‪TYPO3\CMS\Core\Messaging
Definition: AbstractMessage.php:3
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getAllMessagesAndFlush
‪FlashMessage[] getAllMessagesAndFlush($severity=null)
Definition: FlashMessageQueue.php:133
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\dequeue
‪dequeue()
Definition: FlashMessageQueue.php:83
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue
Definition: FlashMessageQueue.php:25
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getUserByContext
‪AbstractUserAuthentication getUserByContext()
Definition: FlashMessageQueue.php:198
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\toArray
‪FlashMessage[] toArray()
Definition: FlashMessageQueue.php:229
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\renderFlashMessages
‪string renderFlashMessages(FlashMessageRendererInterface $flashMessageRenderer=null)
Definition: FlashMessageQueue.php:209
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:51