‪TYPO3CMS  10.4
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 
61  public function ‪enqueue($message): ‪FlashMessageQueue
62  {
63  if (!($message instanceof ‪FlashMessage)) {
64  throw new ‪Exception(
65  'FlashMessageQueue::enqueue() expects an object of type \TYPO3\CMS\Core\Messaging\FlashMessage but got type "' . (is_object($message) ? get_class($message) : gettype($message)) . '"',
66  1376833554
67  );
68  }
69  if ($message->isSessionMessage()) {
70  $this->‪addFlashMessageToSession($message);
71  } else {
72  parent::enqueue($message);
73  }
74  return $this;
75  }
76 
80  public function ‪addMessage(‪FlashMessage $message)
81  {
82  $this->‪enqueue($message);
83  }
84 
88  public function ‪dequeue()
89  {
90  // deliberately empty
91  }
92 
99  protected function ‪addFlashMessageToSession(‪FlashMessage $message)
100  {
101  $queuedFlashMessages = $this->‪getFlashMessagesFromSession();
102  $queuedFlashMessages[] = $message;
103  $this->‪storeFlashMessagesInSession($queuedFlashMessages);
104  }
105 
112  public function ‪getAllMessages($severity = null)
113  {
114  // Get messages from user session
115  $queuedFlashMessagesFromSession = $this->‪getFlashMessagesFromSession();
116  $queuedFlashMessages = array_merge($queuedFlashMessagesFromSession, $this->‪toArray());
117  if ($severity !== null) {
118  $filteredFlashMessages = [];
119  foreach ($queuedFlashMessages as $message) {
120  if ($message->getSeverity() === $severity) {
121  $filteredFlashMessages[] = $message;
122  }
123  }
124  return $filteredFlashMessages;
125  }
126 
127  return $queuedFlashMessages;
128  }
129 
138  public function ‪getAllMessagesAndFlush($severity = null)
139  {
140  $queuedFlashMessages = $this->‪getAllMessages($severity);
141  // Reset messages in user session
142  $this->‪removeAllFlashMessagesFromSession($severity);
143  // Reset internal messages
144  $this->‪clear($severity);
145  return $queuedFlashMessages;
146  }
147 
153  protected function ‪storeFlashMessagesInSession(array $flashMessages = null)
154  {
155  $this->‪getUserByContext()->‪setAndSaveSessionData($this->identifier, $flashMessages);
156  }
157 
163  protected function ‪removeAllFlashMessagesFromSession($severity = null)
164  {
165  if (!$this->‪getUserByContext() instanceof ‪AbstractUserAuthentication) {
166  return;
167  }
168  if ($severity === null) {
169  $this->‪storeFlashMessagesInSession(null);
170  } else {
171  $messages = $this->‪getFlashMessagesFromSession();
172  foreach ($messages as $index => $message) {
173  if ($message->‪getSeverity() === $severity) {
174  unset($messages[$index]);
175  }
176  }
177  $this->‪storeFlashMessagesInSession($messages);
178  }
179  }
180 
187  protected function ‪getFlashMessagesFromSession(): array
188  {
189  $sessionMessages = [];
190  $user = $this->‪getUserByContext();
191  if ($user instanceof ‪AbstractUserAuthentication) {
192  $sessionMessages = $user->getSessionData($this->identifier);
193  $sessionMessages = is_array($sessionMessages) ? $sessionMessages : [];
194  }
195  return $sessionMessages;
196  }
197 
203  protected function ‪getUserByContext()
204  {
205  if ((‪$GLOBALS['TSFE'] ?? null) instanceof ‪TypoScriptFrontendController && ‪$GLOBALS['TSFE']->fe_user instanceof ‪FrontendUserAuthentication) {
206  return ‪$GLOBALS['TSFE']->fe_user;
207  }
208  return ‪$GLOBALS['BE_USER'];
209  }
210 
217  public function ‪renderFlashMessages(‪FlashMessageRendererInterface $flashMessageRenderer = null)
218  {
219  $content = '';
220  $flashMessages = $this->‪getAllMessagesAndFlush();
221 
222  if (!empty($flashMessages)) {
223  if ($flashMessageRenderer === null) {
224  $flashMessageRenderer = GeneralUtility::makeInstance(FlashMessageRendererResolver::class)->resolve();
225  }
226  $content = $flashMessageRenderer->render($flashMessages);
227  }
228 
229  return $content;
230  }
231 
237  public function ‪toArray()
238  {
239  $array = [];
240  $this->rewind();
241  while ($this->valid()) {
242  $array[] = $this->current();
243  $this->next();
244  }
245  return $array;
246  }
247 
253  public function ‪clear($severity = null)
254  {
255  $this->rewind();
256  if ($severity === null) {
257  while (!$this->isEmpty()) {
258  parent::dequeue();
259  }
260  } else {
261  $keysToRemove = [];
262  while ($cur = $this->current()) {
263  if ($cur->getSeverity() === $severity) {
264  $keysToRemove[] = $this->key();
265  }
266  $this->next();
267  }
268  // keys are renumbered when unsetting elements
269  // so unset them from last to first
270  $keysToRemove = array_reverse($keysToRemove);
271  foreach ($keysToRemove as $key) {
272  $this->offsetUnset($key);
273  }
274  }
275  }
276 
280  public function ‪jsonSerialize(): array
281  {
282  return $this->‪toArray();
283  }
284 }
‪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:22
‪TYPO3\CMS\Core\Exception
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\__construct
‪__construct($identifier)
Definition: FlashMessageQueue.php:39
‪TYPO3\CMS\Core\Messaging\AbstractMessage\getSeverity
‪int getSeverity()
Definition: AbstractMessage.php:96
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\clear
‪clear($severity=null)
Definition: FlashMessageQueue.php:252
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\jsonSerialize
‪array jsonSerialize()
Definition: FlashMessageQueue.php:279
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\addFlashMessageToSession
‪addFlashMessageToSession(FlashMessage $message)
Definition: FlashMessageQueue.php:98
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\enqueue
‪FlashMessageQueue enqueue($message)
Definition: FlashMessageQueue.php:60
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\addMessage
‪addMessage(FlashMessage $message)
Definition: FlashMessageQueue.php:79
‪TYPO3\CMS\Core\Messaging\Renderer\FlashMessageRendererInterface
Definition: FlashMessageRendererInterface.php:26
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\storeFlashMessagesInSession
‪storeFlashMessagesInSession(array $flashMessages=null)
Definition: FlashMessageQueue.php:152
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getAllMessages
‪FlashMessage[] getAllMessages($severity=null)
Definition: FlashMessageQueue.php:111
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\setAndSaveSessionData
‪setAndSaveSessionData($key, $data)
Definition: AbstractUserAuthentication.php:1233
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getFlashMessagesFromSession
‪FlashMessage[] getFlashMessagesFromSession()
Definition: FlashMessageQueue.php:186
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\removeAllFlashMessagesFromSession
‪removeAllFlashMessagesFromSession($severity=null)
Definition: FlashMessageQueue.php:162
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:24
‪TYPO3\CMS\Core\Messaging
Definition: AbstractMessage.php:18
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:98
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
Definition: FrontendUserAuthentication.php:30
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getAllMessagesAndFlush
‪FlashMessage[] getAllMessagesAndFlush($severity=null)
Definition: FlashMessageQueue.php:137
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\dequeue
‪dequeue()
Definition: FlashMessageQueue.php:87
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue
Definition: FlashMessageQueue.php:29
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\getUserByContext
‪AbstractUserAuthentication getUserByContext()
Definition: FlashMessageQueue.php:202
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\toArray
‪FlashMessage[] toArray()
Definition: FlashMessageQueue.php:236
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue\renderFlashMessages
‪string renderFlashMessages(FlashMessageRendererInterface $flashMessageRenderer=null)
Definition: FlashMessageQueue.php:216
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:51