[TASK] Cleanup and small improvements to Interceptors
authorSebastian Kurfuerst <sebastian@typo3.org>
Sun, 19 Jun 2011 08:14:10 +0000 (10:14 +0200)
committerSebastian Kurfuerst <sebastian@typo3.org>
Mon, 20 Jun 2011 08:16:59 +0000 (10:16 +0200)
Interceptors now also get passed the current Parsing State,
which they can use to interact with the tree. This can be helpful
to add new syntax tree elements dynamically after or before the
current one. While this functionality is not yet used inside Fluid,
it is used inside a package I am currently developing.

Original Change: 1c093afb1de5915c2302ebe737ced3c083abee67

Change-Id: Ie95c4e95fb4f8ca26e5e738aea965931a65adf5b

typo3/sysext/fluid/Classes/Core/Parser/Interceptor/Escape.php
typo3/sysext/fluid/Classes/Core/Parser/InterceptorInterface.php
typo3/sysext/fluid/Classes/Core/Parser/ParsingState.php
typo3/sysext/fluid/Classes/Core/Parser/TemplateParser.php
typo3/sysext/fluid/Classes/Core/Rendering/RenderingContext.php
typo3/sysext/fluid/Classes/Core/Rendering/RenderingContextInterface.php
typo3/sysext/fluid/Classes/View/AbstractTemplateView.php
typo3/sysext/fluid/Tests/Unit/Core/Parser/SyntaxTree/ViewHelperNodeTest.php

index 7a62d11..8df53b7 100644 (file)
@@ -63,11 +63,12 @@ class Tx_Fluid_Core_Parser_Interceptor_Escape implements Tx_Fluid_Core_Parser_In
         *
         * @param Tx_Fluid_Core_Parser_SyntaxTree_NodeInterface $node
         * @param integer $interceptorPosition One of the INTERCEPT_* constants for the current interception point
+        * @param Tx_Fluid_Core_Parser_ParsingState $parsingState the current parsing state. Not needed in this interceptor.
         * @return Tx_Fluid_Core_Parser_SyntaxTree_NodeInterface
         * @author Karsten Dambekalns <karsten@typo3.org>
         * @author Sebastian Kurfürst <sebastian@typo3.org>
         */
-       public function process(Tx_Fluid_Core_Parser_SyntaxTree_NodeInterface $node, $interceptorPosition) {
+       public function process(Tx_Fluid_Core_Parser_SyntaxTree_NodeInterface $node, $interceptorPosition, Tx_Fluid_Core_Parser_ParsingState $parsingState) {
                if ($interceptorPosition === Tx_Fluid_Core_Parser_InterceptorInterface::INTERCEPT_OPENING_VIEWHELPER) {
                        if (!$node->getUninitializedViewHelper()->isEscapingInterceptorEnabled()) {
                                $this->interceptorEnabled = FALSE;
index 92745b4..ee444fb 100644 (file)
@@ -39,9 +39,10 @@ interface Tx_Fluid_Core_Parser_InterceptorInterface {
         *
         * @param Tx_Fluid_Core_Parser_SyntaxTree_NodeInterface $node
         * @param integer $interceptorPosition One of the INTERCEPT_* constants for the current interception point
+        * @param Tx_Fluid_Core_Parser_ParsingState $parsingState the parsing state
         * @return Tx_Fluid_Core_Parser_SyntaxTree_NodeInterface
         */
-       public function process(Tx_Fluid_Core_Parser_SyntaxTree_NodeInterface $node, $interceptorPosition);
+       public function process(Tx_Fluid_Core_Parser_SyntaxTree_NodeInterface $node, $interceptorPosition, Tx_Fluid_Core_Parser_ParsingState $parsingState);
 
        /**
         * The interceptor should define at which interception positions it wants to be called.
index cb09af8..80b417a 100644 (file)
@@ -56,7 +56,7 @@ class Tx_Fluid_Core_Parser_ParsingState implements Tx_Fluid_Core_Parser_ParsedTe
         * @return void
         * @author Sebastian Kurfürst <sebastian@typo3.org>
         */
-       public function setVariableContainer(Tx_Fluid_Core_ViewHelper_TemplateVariableContainer $variableContainer) {
+       public function injectVariableContainer(Tx_Fluid_Core_ViewHelper_TemplateVariableContainer $variableContainer) {
                $this->variableContainer = $variableContainer;
        }
 
index 64864ce..bac2fef 100644 (file)
@@ -25,7 +25,7 @@
  *
  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
  */
-class Tx_Fluid_Core_Parser_TemplateParser implements t3lib_Singleton {
+class Tx_Fluid_Core_Parser_TemplateParser {
 
        public static $SCAN_PATTERN_NAMESPACEDECLARATION = '/(?<!\\\\){namespace\s*([a-zA-Z]+[a-zA-Z0-9]*)\s*=\s*((?:F3|Tx|t3lib|tslib)(?:FLUID_NAMESPACE_SEPARATOR\w+)+)\s*}/m';
 
@@ -404,8 +404,6 @@ class Tx_Fluid_Core_Parser_TemplateParser implements t3lib_Singleton {
                $state->setRootNode($rootNode);
                $state->pushNodeToStack($rootNode);
 
-               $state->setVariableContainer($this->objectManager->create('Tx_Fluid_Core_ViewHelper_TemplateVariableContainer'));
-
                foreach ($splitTemplate as $templateElement) {
                        $matchedVariables = array();
                        if (preg_match(self::$SCAN_PATTERN_CDATA, $templateElement, $matchedVariables) > 0) {
@@ -442,7 +440,7 @@ class Tx_Fluid_Core_Parser_TemplateParser implements t3lib_Singleton {
 
                if ($selfclosing) {
                        $node = $state->popNodeFromStack();
-                       $this->callInterceptor($node, Tx_Fluid_Core_Parser_InterceptorInterface::INTERCEPT_CLOSING_VIEWHELPER);
+                       $this->callInterceptor($node, Tx_Fluid_Core_Parser_InterceptorInterface::INTERCEPT_CLOSING_VIEWHELPER, $state);
                }
        }
 
@@ -479,7 +477,7 @@ class Tx_Fluid_Core_Parser_TemplateParser implements t3lib_Singleton {
                        call_user_func(array($viewHelper, 'postParseEvent'), $currentDynamicNode, $argumentsObjectTree, $state->getVariableContainer());
                }
 
-               $this->callInterceptor($currentDynamicNode, Tx_Fluid_Core_Parser_InterceptorInterface::INTERCEPT_OPENING_VIEWHELPER);
+               $this->callInterceptor($currentDynamicNode, Tx_Fluid_Core_Parser_InterceptorInterface::INTERCEPT_OPENING_VIEWHELPER, $state);
 
                $state->pushNodeToStack($currentDynamicNode);
        }
@@ -567,7 +565,7 @@ class Tx_Fluid_Core_Parser_TemplateParser implements t3lib_Singleton {
                if ($lastStackElement->getViewHelperClassName() != $this->resolveViewHelperName($namespaceIdentifier, $methodIdentifier)) {
                        throw new Tx_Fluid_Core_Parser_Exception('Templating tags not properly nested. Expected: ' . $lastStackElement->getViewHelperClassName() . '; Actual: ' . $this->resolveViewHelperName($namespaceIdentifier, $methodIdentifier), 1224485398);
                }
-               $this->callInterceptor($lastStackElement, Tx_Fluid_Core_Parser_InterceptorInterface::INTERCEPT_CLOSING_VIEWHELPER);
+               $this->callInterceptor($lastStackElement, Tx_Fluid_Core_Parser_InterceptorInterface::INTERCEPT_CLOSING_VIEWHELPER, $state);
        }
 
        /**
@@ -616,7 +614,7 @@ class Tx_Fluid_Core_Parser_TemplateParser implements t3lib_Singleton {
                if (strlen($objectAccessorString) > 0) {
 
                        $node = $this->objectManager->create('Tx_Fluid_Core_Parser_SyntaxTree_ObjectAccessorNode', $objectAccessorString);
-                       $this->callInterceptor($node, Tx_Fluid_Core_Parser_InterceptorInterface::INTERCEPT_OBJECTACCESSOR);
+                       $this->callInterceptor($node, Tx_Fluid_Core_Parser_InterceptorInterface::INTERCEPT_OBJECTACCESSOR, $state);
 
                        $state->getNodeFromStack()->addChildNode($node);
                }
@@ -624,7 +622,7 @@ class Tx_Fluid_Core_Parser_TemplateParser implements t3lib_Singleton {
                        // Close ViewHelper Tags if needed.
                for ($i=0; $i<$numberOfViewHelpers; $i++) {
                        $node = $state->popNodeFromStack();
-                       $this->callInterceptor($node, Tx_Fluid_Core_Parser_InterceptorInterface::INTERCEPT_CLOSING_VIEWHELPER);
+                       $this->callInterceptor($node, Tx_Fluid_Core_Parser_InterceptorInterface::INTERCEPT_CLOSING_VIEWHELPER, $state);
                }
        }
 
@@ -633,10 +631,11 @@ class Tx_Fluid_Core_Parser_TemplateParser implements t3lib_Singleton {
         *
         * @param Tx_Fluid_Core_Parser_SyntaxTree_NodeInterface $node The syntax tree node which can be modified by the interceptors.
         * @param integer $interceptionPoint the interception point. One of the Tx_Fluid_Core_Parser_InterceptorInterface::INTERCEPT_* constants.
+        * @param Tx_Fluid_Core_Parser_ParsingState the parsing state
         * @return void
         * @author Sebastian Kurfürst <sebastian@typo3.org>
         */
-       protected function callInterceptor(Tx_Fluid_Core_Parser_SyntaxTree_NodeInterface &$node, $interceptionPoint) {
+       protected function callInterceptor(Tx_Fluid_Core_Parser_SyntaxTree_NodeInterface &$node, $interceptionPoint, Tx_Fluid_Core_Parser_ParsingState $state) {
                if ($this->configuration !== NULL) {
                        // $this->configuration is UNSET inside the arguments of a ViewHelper.
                        // That's why the interceptors are only called if the object accesor is not inside a ViewHelper Argument
@@ -645,7 +644,7 @@ class Tx_Fluid_Core_Parser_TemplateParser implements t3lib_Singleton {
                        $interceptors = $this->configuration->getInterceptors($interceptionPoint);
                        if (count($interceptors) > 0) {
                                foreach($interceptors as $interceptor) {
-                                       $node = $interceptor->process($node, $interceptionPoint);
+                                       $node = $interceptor->process($node, $interceptionPoint, $state);
                                }
                        }
                }
@@ -841,7 +840,7 @@ class Tx_Fluid_Core_Parser_TemplateParser implements t3lib_Singleton {
         */
        protected function textHandler(Tx_Fluid_Core_Parser_ParsingState $state, $text) {
                $node = $this->objectManager->create('Tx_Fluid_Core_Parser_SyntaxTree_TextNode', $text);
-               $this->callInterceptor($node, Tx_Fluid_Core_Parser_InterceptorInterface::INTERCEPT_TEXT);
+               $this->callInterceptor($node, Tx_Fluid_Core_Parser_InterceptorInterface::INTERCEPT_TEXT, $state);
 
                $state->getNodeFromStack()->addChildNode($node);
        }
index cada727..1c00d8c 100644 (file)
@@ -64,8 +64,6 @@ class Tx_Fluid_Core_Rendering_RenderingContext implements Tx_Fluid_Core_Renderin
         */
        public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
                $this->objectManager = $objectManager;
-               $this->templateVariableContainer = $this->objectManager->create('Tx_Fluid_Core_ViewHelper_TemplateVariableContainer');
-               $this->viewHelperVariableContainer = $this->objectManager->create('Tx_Fluid_Core_ViewHelper_ViewHelperVariableContainer');
        }
 
        /**
@@ -85,7 +83,7 @@ class Tx_Fluid_Core_Rendering_RenderingContext implements Tx_Fluid_Core_Renderin
         * @param Tx_Fluid_Core_ViewHelper_TemplateVariableContainer $templateVariableContainer The template variable container to set
         * @author Sebastian Kurfürst <sebastian@typo3.org>
         */
-       public function setTemplateVariableContainer(Tx_Fluid_Core_ViewHelper_TemplateVariableContainer $templateVariableContainer) {
+       public function injectTemplateVariableContainer(Tx_Fluid_Core_ViewHelper_TemplateVariableContainer $templateVariableContainer) {
                $this->templateVariableContainer = $templateVariableContainer;
        }
 
@@ -126,7 +124,7 @@ class Tx_Fluid_Core_Rendering_RenderingContext implements Tx_Fluid_Core_Renderin
         * @return void
         * @author Sebastian Kurfürst <sebastian@typo3.org>
         */
-       public function setViewHelperVariableContainer(Tx_Fluid_Core_ViewHelper_ViewHelperVariableContainer $viewHelperVariableContainer) {
+       public function injectViewHelperVariableContainer(Tx_Fluid_Core_ViewHelper_ViewHelperVariableContainer $viewHelperVariableContainer) {
                $this->viewHelperVariableContainer = $viewHelperVariableContainer;
        }
 
@@ -140,4 +138,4 @@ class Tx_Fluid_Core_Rendering_RenderingContext implements Tx_Fluid_Core_Renderin
                return $this->viewHelperVariableContainer;
        }
 }
-?>
\ No newline at end of file
+?>
index 7658c0e..4909441 100644 (file)
@@ -40,7 +40,7 @@ interface Tx_Fluid_Core_Rendering_RenderingContextInterface {
         *
         * @param Tx_Fluid_Core_ViewHelper_TemplateVariableContainer $templateVariableContainer The template variable container to set
         */
-       public function setTemplateVariableContainer(Tx_Fluid_Core_ViewHelper_TemplateVariableContainer $templateVariableContainer);
+       public function injectTemplateVariableContainer(Tx_Fluid_Core_ViewHelper_TemplateVariableContainer $templateVariableContainer);
 
        /**
         * Get the template variable container
@@ -69,7 +69,7 @@ interface Tx_Fluid_Core_Rendering_RenderingContextInterface {
         * @param Tx_Fluid_Core_ViewHelper_ViewHelperVariableContainer $viewHelperVariableContainer
         * @return void
         */
-       public function setViewHelperVariableContainer(Tx_Fluid_Core_ViewHelper_ViewHelperVariableContainer $viewHelperVariableContainer);
+       public function injectViewHelperVariableContainer(Tx_Fluid_Core_ViewHelper_ViewHelperVariableContainer $viewHelperVariableContainer);
 
        /**
         * Get the ViewHelperVariableContainer
index 1e3b4fc..5bf41fe 100644 (file)
@@ -212,7 +212,7 @@ abstract class Tx_Fluid_View_AbstractTemplateView implements Tx_Extbase_MVC_View
                } else {
                        $variableContainer = $this->objectManager->create('Tx_Fluid_Core_ViewHelper_TemplateVariableContainer', $variables);
                        $renderingContext = clone $renderingContext;
-                       $renderingContext->setTemplateVariableContainer($variableContainer);
+                       $renderingContext->injectTemplateVariableContainer($variableContainer);
                        $renderingTypeOnNextLevel = $this->getCurrentRenderingType();
                }
 
@@ -241,7 +241,7 @@ abstract class Tx_Fluid_View_AbstractTemplateView implements Tx_Extbase_MVC_View
                $partial = $this->templateParser->parse($this->getPartialSource($partialName));
                $variableContainer = $this->objectManager->create('Tx_Fluid_Core_ViewHelper_TemplateVariableContainer', $variables);
                $renderingContext = clone $this->getCurrentRenderingContext();
-               $renderingContext->setTemplateVariableContainer($variableContainer);
+               $renderingContext->injectTemplateVariableContainer($variableContainer);
 
                $this->startRendering(self::RENDERING_PARTIAL, $partial, $renderingContext);
                if ($sectionName !== NULL) {
index 8733294..1ba1fbf 100644 (file)
@@ -70,13 +70,13 @@ class Tx_Fluid_Tests_Unit_Core_Parser_SyntaxTree_ViewHelperNodeTest extends Tx_E
                $this->renderingContext->injectObjectManager($this->mockObjectManager);
 
                $this->templateVariableContainer = $this->getMock('Tx_Fluid_Core_ViewHelper_TemplateVariableContainer');
-               $this->renderingContext->setTemplateVariableContainer($this->templateVariableContainer);
+               $this->renderingContext->injectTemplateVariableContainer($this->templateVariableContainer);
 
                $this->controllerContext = $this->getMock('Tx_Extbase_MVC_Controller_ControllerContext', array(), array(), '', FALSE);
                $this->renderingContext->setControllerContext($this->controllerContext);
 
                $this->viewHelperVariableContainer = $this->getMock('Tx_Fluid_Core_ViewHelper_ViewHelperVariableContainer');
-               $this->renderingContext->setViewHelperVariableContainer($this->viewHelperVariableContainer);
+               $this->renderingContext->injectViewHelperVariableContainer($this->viewHelperVariableContainer);
        }
 
        /**