TYPO3 CMS  TYPO3_8-7
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
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 
56  public function enqueue($message)
57  {
58  if (!($message instanceof FlashMessage)) {
59  throw new \TYPO3\CMS\Core\Exception(
60  'FlashMessageQueue::enqueue() expects an object of type \TYPO3\CMS\Core\Messaging\FlashMessage but got type "' . (is_object($message) ? get_class($message) : gettype($message)) . '"',
61  1376833554
62  );
63  }
64  if ($message->isSessionMessage()) {
65  $this->addFlashMessageToSession($message);
66  } else {
67  parent::enqueue($message);
68  }
69  }
70 
74  public function addMessage(FlashMessage $message)
75  {
76  $this->enqueue($message);
77  }
78 
82  public function dequeue()
83  {
84  // deliberately empty
85  }
86 
93  protected function addFlashMessageToSession(FlashMessage $message)
94  {
95  $queuedFlashMessages = $this->getFlashMessagesFromSession();
96  $queuedFlashMessages[] = $message;
97  $this->storeFlashMessagesInSession($queuedFlashMessages);
98  }
99 
106  public function getAllMessages($severity = null)
107  {
108  // Get messages from user session
109  $queuedFlashMessagesFromSession = $this->getFlashMessagesFromSession();
110  $queuedFlashMessages = array_merge($queuedFlashMessagesFromSession, $this->toArray());
111  if ($severity !== null) {
112  $filteredFlashMessages = [];
113  foreach ($queuedFlashMessages as $message) {
114  if ($message->getSeverity() === $severity) {
115  $filteredFlashMessages[] = $message;
116  }
117  }
118  return $filteredFlashMessages;
119  }
120 
121  return $queuedFlashMessages;
122  }
123 
132  public function getAllMessagesAndFlush($severity = null)
133  {
134  $queuedFlashMessages = $this->getAllMessages($severity);
135  // Reset messages in user session
136  $this->removeAllFlashMessagesFromSession($severity);
137  // Reset internal messages
138  $this->clear($severity);
139  return $queuedFlashMessages;
140  }
141 
147  protected function storeFlashMessagesInSession(array $flashMessages = null)
148  {
149  $this->getUserByContext()->setAndSaveSessionData($this->identifier, $flashMessages);
150  }
151 
157  protected function removeAllFlashMessagesFromSession($severity = null)
158  {
159  if ($severity === null) {
160  $this->storeFlashMessagesInSession(null);
161  } else {
162  $messages = $this->getFlashMessagesFromSession();
163  foreach ($messages as $index => $message) {
164  if ($message->getSeverity() === $severity) {
165  unset($messages[$index]);
166  }
167  }
168  $this->storeFlashMessagesInSession($messages);
169  }
170  }
171 
178  protected function getFlashMessagesFromSession()
179  {
180  $flashMessages = $this->getUserByContext()->getSessionData($this->identifier);
181  return is_array($flashMessages) ? $flashMessages : [];
182  }
183 
189  protected function getUserByContext()
190  {
191  return TYPO3_MODE === 'BE' ? $GLOBALS['BE_USER'] : $GLOBALS['TSFE']->fe_user;
192  }
193 
200  public function renderFlashMessages(FlashMessageRendererInterface $flashMessageRenderer = null)
201  {
202  $content = '';
203  $flashMessages = $this->getAllMessagesAndFlush();
204 
205  if (!empty($flashMessages)) {
206  if ($flashMessageRenderer === null) {
207  $flashMessageRenderer = GeneralUtility::makeInstance(FlashMessageRendererResolver::class)->resolve();
208  }
209  $content = $flashMessageRenderer->render($flashMessages);
210  }
211 
212  return $content;
213  }
214 
220  public function toArray()
221  {
222  $array = [];
223  $this->rewind();
224  while ($this->valid()) {
225  $array[] = $this->current();
226  $this->next();
227  }
228  return $array;
229  }
230 
236  public function clear($severity = null)
237  {
238  $this->rewind();
239  if ($severity === null) {
240  while (!$this->isEmpty()) {
241  parent::dequeue();
242  }
243  } else {
244  $keysToRemove = [];
245  while ($cur = $this->current()) {
246  if ($cur->getSeverity() === $severity) {
247  $keysToRemove[] = $this->key();
248  }
249  $this->next();
250  }
251  // keys are renumbered when unsetting elements
252  // so unset them from last to first
253  $keysToRemove = array_reverse($keysToRemove);
254  foreach ($keysToRemove as $key) {
255  $this->offsetUnset($key);
256  }
257  }
258  }
259 }
renderFlashMessages(FlashMessageRendererInterface $flashMessageRenderer=null)
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
storeFlashMessagesInSession(array $flashMessages=null)