TYPO3 CMS  TYPO3_6-2
FlashMessageQueue.php
Go to the documentation of this file.
1 <?php
3 
22 class FlashMessageQueue extends \SplQueue {
23 
29  protected $identifier;
30 
34  public function __construct($identifier) {
35  $this->identifier = $identifier;
36  }
37 
45  public function enqueue($message) {
46  if (!($message instanceof FlashMessage)) {
47  throw new \TYPO3\CMS\Core\Exception(
48  'FlashMessageQueue::enqueue() expects an object of type \TYPO3\CMS\Core\Messaging\FlashMessage but got type "' . (is_object($message) ? get_class($message) : gettype($message)) . '"',
49  1376833554
50  );
51  }
52  if ($message->isSessionMessage()) {
53  $this->addFlashMessageToSession($message);
54  } else {
55  parent::enqueue($message);
56  }
57  }
58 
62  public function dequeue() {
63  // deliberately empty
64  }
65 
73  protected function addFlashMessageToSession(\TYPO3\CMS\Core\Messaging\FlashMessage $message) {
74  $queuedFlashMessages = $this->getFlashMessagesFromSession();
75  $queuedFlashMessages[] = $message;
76  $this->storeFlashMessagesInSession($queuedFlashMessages);
77  }
78 
84  protected function getAllMessages() {
85  // Get messages from user session
86  $queuedFlashMessagesFromSession = $this->getFlashMessagesFromSession();
87  $queuedFlashMessages = array_merge($queuedFlashMessagesFromSession, $this->toArray());
88  return $queuedFlashMessages;
89  }
90 
98  protected function getAllMessagesAndFlush() {
99  $queuedFlashMessages = $this->getAllMessages();
100  // Reset messages in user session
102  // Reset internal messages
103  $this->clear();
104  return $queuedFlashMessages;
105  }
106 
113  protected function storeFlashMessagesInSession(array $flashMessages) {
114  $this->getUserByContext()->setAndSaveSessionData($this->identifier, $flashMessages);
115  }
116 
122  protected function removeAllFlashMessagesFromSession() {
123  $this->getUserByContext()->setAndSaveSessionData($this->identifier, NULL);
124  }
125 
132  protected function getFlashMessagesFromSession() {
133  $flashMessages = $this->getUserByContext()->getSessionData($this->identifier);
134  return is_array($flashMessages) ? $flashMessages : array();
135  }
136 
142  protected function getUserByContext() {
143  return TYPO3_MODE === 'BE' ? $GLOBALS['BE_USER'] : $GLOBALS['TSFE']->fe_user;
144  }
145 
151  protected function renderFlashMessages() {
152  $content = '';
153  $flashMessages = $this->getAllMessagesAndFlush();
154  if (count($flashMessages)) {
155  foreach ($flashMessages as $flashMessage) {
157  $content .= $flashMessage->render();
158  }
159  }
160  return $content;
161  }
162 
168  public function toArray() {
169  $array = array();
170  $this->rewind();
171  while ($this->valid()) {
172  $array[] = $this->current();
173  $this->next();
174  }
175  return $array;
176  }
177 
183  public function clear() {
184  $this->rewind();
185  while (!$this->isEmpty()) {
186  parent::dequeue();
187  }
188  }
189 
208  static public function __callStatic($name, array $arguments) {
211  $flashMessageService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Messaging\FlashMessageService');
212  $identifier = 'core.template.flashMessages';
213  switch ($name) {
214  case 'renderFlashMessages':
215  return $flashMessageService->getMessageQueueByIdentifier($identifier)->renderFlashMessages();
216  break;
217  case 'getAllMessagesAndFlush':
218  return $flashMessageService->getMessageQueueByIdentifier($identifier)->getAllMessagesAndFlush();
219  break;
220  case 'getAllMessages':
221  return $flashMessageService->getMessageQueueByIdentifier($identifier)->getAllMessages();
222  break;
223  case 'addMessage':
224  $flashMessageService->getMessageQueueByIdentifier($identifier)->enqueue(current($arguments));
225  break;
226  default:
227  throw new \RuntimeException('The requested method "' . $name . '" cannot be called via __callStatic.', 1363300030);
228  }
229  }
230 
246  public function __call($name, array $arguments) {
247  switch ($name) {
248  case 'renderFlashMessages':
249  return $this->renderFlashMessages();
250  break;
251  case 'getAllMessagesAndFlush':
252  return $this->getAllMessagesAndFlush();
253  break;
254  case 'getAllMessages':
255  return $this->getAllMessages();
256  break;
257  case 'addMessage':
258  $this->enqueue(current($arguments));
259  break;
260  default:
261  throw new \RuntimeException('The requested method "' . $name . '" cannot be called via __call.', 1363300072);
262  }
263  }
264 }
const TYPO3_MODE
Definition: init.php:40
addFlashMessageToSession(\TYPO3\CMS\Core\Messaging\FlashMessage $message)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]