[FEATURE] Added support for multiple FlashMessage queues 89/36589/10
authorStefan Neufeind <typo3.neufeind@speedpartner.de>
Tue, 14 Jul 2015 18:43:06 +0000 (20:43 +0200)
committerMarkus Klein <markus.klein@typo3.org>
Sun, 16 Aug 2015 13:55:42 +0000 (15:55 +0200)
Each controller defaults to its own queue.
Allow to access other queues like queues from different
controllers or split message into multiple queues for
specific parts in one controller

The FlashMessageService already supports multiple queues.
It just has a different default-queue.

Since FlashMessageService already is a Singleton, avoid
separate caching of queues in the ControllerContext.

Change-Id: I5e0bb867c6b02979b899e46bb42bf209b6a8e47a
Resolves: #64726
Releases: master
Reviewed-on: http://review.typo3.org/36589
Reviewed-by: Alexander Schnitzler <typo3@alexanderschnitzler.de>
Tested-by: Alexander Schnitzler <typo3@alexanderschnitzler.de>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
typo3/sysext/core/Documentation/Changelog/master/Feature-64726-UsingArbitraryFlashmessageQueues.rst [new file with mode: 0644]
typo3/sysext/extbase/Classes/Mvc/Controller/ControllerContext.php
typo3/sysext/fluid/Classes/ViewHelpers/FlashMessagesViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/FlashMessagesViewHelperTest.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-64726-UsingArbitraryFlashmessageQueues.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-64726-UsingArbitraryFlashmessageQueues.rst
new file mode 100644 (file)
index 0000000..afa0cba
--- /dev/null
@@ -0,0 +1,26 @@
+================================================================
+Feature: #64726 - Added support for multiple FlashMessage queues
+================================================================
+
+Description
+===========
+
+In Extbase ControllerContext the method getFlashMessageQueue now optionally
+allows to specify which queue to fetch. If none is specified the default-
+messagequeue for the current controller/plugin will be used.
+
+.. code-block:: php
+       $this->controllerContext->getFlashMessageQueue($queueIdentifier);
+
+In Fluid the flashMessages-ViewHelper also allows to specify a queue to
+use.
+
+.. code-block:: html
+       <f:flashMessages queueIdentifier="myQueue" />
+
+
+Impact
+======
+
+Extensions may now render foreign flash message queues and add messages
+to them.
\ No newline at end of file
index 9ed48b1..6d0c1a6 100644 (file)
@@ -43,9 +43,9 @@ class ControllerContext {
        protected $uriBuilder;
 
        /**
-        * @var \TYPO3\CMS\Core\Messaging\FlashMessageQueue
+        * @var string
         */
-       protected $flashMessageQueue;
+       protected $flashMessageQueueDefaultIdentifier;
 
        /**
         * @var \TYPO3\CMS\Core\Messaging\FlashMessageService
@@ -148,17 +148,19 @@ class ControllerContext {
        }
 
        /**
+        * @param string $identifier Queue-identifier
         * @return \TYPO3\CMS\Core\Messaging\FlashMessageQueue
         * @api
         */
-       public function getFlashMessageQueue() {
-               if (!$this->flashMessageQueue instanceof \TYPO3\CMS\Core\Messaging\FlashMessageQueue) {
-                       $this->flashMessageQueue = $this->flashMessageService->getMessageQueueByIdentifier(
-                               'extbase.flashmessages.' . $this->extensionService->getPluginNamespace($this->request->getControllerExtensionName(), $this->request->getPluginName())
-                       );
+       public function getFlashMessageQueue($identifier = NULL) {
+               if ($identifier === NULL) {
+                       if ($this->flashMessageQueueDefaultIdentifier === NULL) {
+                               // cache the default-identifier for performance-reasons
+                               $this->flashMessageQueueDefaultIdentifier = 'extbase.flashmessages.' . $this->extensionService->getPluginNamespace($this->request->getControllerExtensionName(), $this->request->getPluginName());
+                       }
+                       $identifier = $this->flashMessageQueueDefaultIdentifier;
                }
-
-               return $this->flashMessageQueue;
+               return $this->flashMessageService->getMessageQueueByIdentifier($identifier);
        }
 
 }
index a3c05b0..c928701 100644 (file)
@@ -77,6 +77,10 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
  * </dl>
  * </output>
  *
+ * <code title="Using a specific queue">
+ * <f:flashMessages queueIdentifier="myQueue" />
+ * </code>
+ *
  * @api
  */
 class FlashMessagesViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper {
@@ -110,6 +114,7 @@ class FlashMessagesViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractT
         */
        public function initializeArguments() {
                $this->registerUniversalTagAttributes();
+               $this->registerArgument('queueIdentifier', 'string', 'Flash-message queue to use', FALSE);
        }
 
        /**
@@ -124,7 +129,8 @@ class FlashMessagesViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractT
         * @api
         */
        public function render($renderMode = NULL, $as = NULL) {
-               $flashMessages = $this->controllerContext->getFlashMessageQueue()->getAllMessagesAndFlush();
+               $queueIdentifier = isset($this->arguments['queueIdentifier']) ? $this->arguments['queueIdentifier'] : NULL;
+               $flashMessages = $this->controllerContext->getFlashMessageQueue($queueIdentifier)->getAllMessagesAndFlush();
                if ($flashMessages === NULL || count($flashMessages) === 0) {
                        return '';
                }
index 2071ded..089c48a 100644 (file)
@@ -27,6 +27,11 @@ class FlashMessagesViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelper
        protected $mockTagBuilder;
 
        /**
+        * @var \TYPO3\CMS\Core\Messaging\FlashMessageQueue|\PHPUnit_Framework_MockObject_MockObject $mockFlashMessagingQueue
+        */
+       protected $mockFlashMessagingQueue;
+
+       /**
         * Sets up this test case
         *
         * @return void
@@ -35,6 +40,7 @@ class FlashMessagesViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelper
                /** @var \TYPO3\CMS\Core\Messaging\FlashMessageQueue|\PHPUnit_Framework_MockObject_MockObject $mockFlashMessagingQueue */
                $mockFlashMessagingQueue = $this->getMock(\TYPO3\CMS\Core\Messaging\FlashMessageQueue::class, array('getAllMessagesAndFlush'), array('foo'));
                $mockFlashMessagingQueue->expects($this->once())->method('getAllMessagesAndFlush')->will($this->returnValue(array()));
+               $this->mockFlashMessagingQueue = $mockFlashMessagingQueue;
 
                $mockControllerContext = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext::class, array(), array(), '', FALSE);
                $mockControllerContext->expects($this->any())->method('getFlashMessageQueue')->will($this->returnValue($mockFlashMessagingQueue));
@@ -53,4 +59,19 @@ class FlashMessagesViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelper
                $this->assertEmpty($this->viewHelper->render());
        }
 
+       /**
+        * @test
+        */
+       public function fetchMessagesFromSpecificQueue() {
+               $queueIdentifier = 'myQueue';
+
+               $mockControllerContext = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext::class, array(), array(), '', FALSE);
+               $mockControllerContext->expects($this->once())->method('getFlashMessageQueue')->with($queueIdentifier)->will($this->returnValue($this->mockFlashMessagingQueue));
+
+               $this->viewHelper->_set('controllerContext', $mockControllerContext);
+               $this->viewHelper->setArguments(array('queueIdentifier' => $queueIdentifier));
+
+               $this->assertEmpty($this->viewHelper->render());
+       }
+
 }