[TASK] Re-work/simplify copyright header in PHP files - Part 2
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Messaging / FlashMessageQueue.php
1 <?php
2 namespace TYPO3\CMS\Core\Messaging;
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 /**
17 * A class which collects and renders flash messages.
18 *
19 * @author Rupert Germann <rupi@gmx.li>
20 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
21 */
22 class FlashMessageQueue extends \SplQueue {
23
24 /**
25 * A unique identifier for this queue
26 *
27 * @var string
28 */
29 protected $identifier;
30
31 /**
32 * @param string $identifier The unique identifier for this queue
33 */
34 public function __construct($identifier) {
35 $this->identifier = $identifier;
36 }
37
38 /**
39 * Adds a message either to the BE_USER session (if the $message has the storeInSession flag set)
40 * or it enqueues the message.
41 *
42 * @param \TYPO3\CMS\Core\Messaging\FlashMessage $message Instance of \TYPO3\CMS\Core\Messaging\FlashMessage, representing a message
43 * @return void
44 */
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
59 /**
60 * @return void
61 */
62 public function dequeue() {
63 // deliberately empty
64 }
65
66 /**
67 * Adds the given flash message to the array of
68 * flash messages that will be stored in the session.
69 *
70 * @param \TYPO3\CMS\Core\Messaging\FlashMessage $message
71 * @return void
72 */
73 protected function addFlashMessageToSession(\TYPO3\CMS\Core\Messaging\FlashMessage $message) {
74 $queuedFlashMessages = $this->getFlashMessagesFromSession();
75 $queuedFlashMessages[] = $message;
76 $this->storeFlashMessagesInSession($queuedFlashMessages);
77 }
78
79 /**
80 * Returns all messages from the current PHP session and from the current request.
81 *
82 * @return array Array of \TYPO3\CMS\Core\Messaging\FlashMessage objects
83 */
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
91 /**
92 * Returns all messages from the current PHP session and from the current request.
93 * After fetching the messages the internal queue and the message queue in the session
94 * will be emptied.
95 *
96 * @return array Array of \TYPO3\CMS\Core\Messaging\FlashMessage objects
97 */
98 protected function getAllMessagesAndFlush() {
99 $queuedFlashMessages = $this->getAllMessages();
100 // Reset messages in user session
101 $this->removeAllFlashMessagesFromSession();
102 // Reset internal messages
103 $this->clear();
104 return $queuedFlashMessages;
105 }
106
107 /**
108 * Stores given flash messages in the session
109 *
110 * @param array $flashMessages Array of \TYPO3\CMS\Core\Messaging\FlashMessage
111 * @return void
112 */
113 protected function storeFlashMessagesInSession(array $flashMessages) {
114 $this->getUserByContext()->setAndSaveSessionData($this->identifier, $flashMessages);
115 }
116
117 /**
118 * Removes all flash messages from the session
119 *
120 * @return void
121 */
122 protected function removeAllFlashMessagesFromSession() {
123 $this->getUserByContext()->setAndSaveSessionData($this->identifier, NULL);
124 }
125
126 /**
127 * Returns current flash messages from the session, making sure to always
128 * return an array.
129 *
130 * @return array An array of \TYPO3\CMS\Core\Messaging\FlashMessage flash messages.
131 */
132 protected function getFlashMessagesFromSession() {
133 $flashMessages = $this->getUserByContext()->getSessionData($this->identifier);
134 return is_array($flashMessages) ? $flashMessages : array();
135 }
136
137 /**
138 * Gets user object by context
139 *
140 * @return object User object
141 */
142 protected function getUserByContext() {
143 return TYPO3_MODE === 'BE' ? $GLOBALS['BE_USER'] : $GLOBALS['TSFE']->fe_user;
144 }
145
146 /**
147 * Fetches and renders all available flash messages from the queue.
148 *
149 * @return string All flash messages in the queue rendered as HTML.
150 */
151 protected function renderFlashMessages() {
152 $content = '';
153 $flashMessages = $this->getAllMessagesAndFlush();
154 if (count($flashMessages)) {
155 foreach ($flashMessages as $flashMessage) {
156 /** @var $flashMessage \TYPO3\CMS\Core\Messaging\FlashMessage */
157 $content .= $flashMessage->render();
158 }
159 }
160 return $content;
161 }
162
163 /**
164 * Returns all items of the queue as array
165 *
166 * @return array
167 */
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
178 /**
179 * Removes all items from the queue
180 *
181 * @return void
182 */
183 public function clear() {
184 $this->rewind();
185 while (!$this->isEmpty()) {
186 parent::dequeue();
187 }
188 }
189
190 /**
191 * This method provides a fallback for deprecated static calls like:
192 * FlashMessageQueue::renderFlashMessages,
193 * FlashMessageQueue::getAllMessagesAndFlush,
194 * FlashMessageQueue::getAllMessages and
195 * FlashMessageQueue::addMessage
196 *
197 * From 6.3 on __callStatic and __call will be removed and the
198 * protected non static methods "renderFlashMessages",
199 * "getAllMessagesAndFlush", "getAllMessages" and "addMessage"
200 * will be made public.
201 *
202 * @param string $name
203 * @param array $arguments
204 * @throws \RuntimeException
205 * @return void|array|string
206 * @deprecated since 6.1 will be removed in 6.3
207 */
208 static public function __callStatic($name, array $arguments) {
209 \TYPO3\CMS\Core\Utility\GeneralUtility::logDeprecatedFunction();
210 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
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
231 /**
232 * This method is deprecated but will not log a deprecation
233 * message because once the here used method names are 'free'
234 * again they will be implemented natively in this class. This
235 * is not possible at the moment because these methods have
236 * been static and need to be statically callable through
237 * __callStatic until 6.3.
238 *
239 * @param string $name
240 * @param array $arguments
241 * @throws \RuntimeException
242 * @return void|array|string
243 * @see __callStatic
244 * @deprecated since 6.1 will be removed in 6.3
245 */
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 }