Completed decorator, extended interface
authorRoland Häder <roland@mxchange.org>
Wed, 24 Mar 2010 04:31:16 +0000 (04:31 +0000)
committerRoland Häder <roland@mxchange.org>
Wed, 24 Mar 2010 04:31:16 +0000 (04:31 +0000)
inc/classes/interfaces/template/class_CompileableTemplate.php
inc/classes/main/decorator/template/class_XmlRewriterTemplateDecorator.php
inc/classes/main/decorator/xml/class_XmlCompactorDecorator.php
inc/classes/main/template/class_BaseTemplateEngine.php

index d0de9ec..c6b1a1a 100644 (file)
@@ -180,6 +180,14 @@ interface CompileableTemplate extends FrameworkInterface {
         * @return      $xmlCompacting  Wether XML compacting is enabled or disabled
         */
        function isXmlCompactingEnabled ();
+
+       /**
+        * Removes all comments, tabs and new-line charcters to compact the content
+        *
+        * @param       $content        The uncompacted content
+        * @return      $content        The compacted content
+        */
+       function compactContent ($content);
 }
 
 // [EOF]
index 8885311..c849ef1 100644 (file)
@@ -389,6 +389,56 @@ class XmlRewriterTemplateDecorator extends BaseDecorator implements CompileableT
                // Call the inner class' method
                return $this->getTemplateInstance()->isXmlCompactingEnabled();
        }
+
+       /**
+        * Handles the start element of an XML resource
+        *
+        * @param       $resource               XML parser resource (currently ignored)
+        * @param       $element                The element we shall handle
+        * @param       $attributes             All attributes
+        * @return      void
+        * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
+        */
+       public function startElement ($resource, $element, array $attributes) {
+               // Call the inner class' method
+               $this->getTemplateInstance()->startElement($resource, $element, $attributes);
+       }
+
+       /**
+        * Ends the main or sub node by sending out the gathered data
+        *
+        * @param       $resource       An XML resource pointer (currently ignored)
+        * @param       $nodeName       Name of the node we want to finish
+        * @return      void
+        * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
+        */
+       public function endElement ($resource, $nodeName) {
+               // Call the inner class' method
+               $this->getTemplateInstance()->endElement($resource, $nodeName);
+       }
+
+       /**
+        * Currently not used
+        *
+        * @param       $resource               XML parser resource (currently ignored)
+        * @param       $characters             Characters to handle
+        * @return      void
+        * @todo        Find something useful with this!
+        */
+       public function characterHandler ($resource, $characters) {
+               // Call the inner class' method but trim the characters before
+               $this->getTemplateInstance()->characterHandler($resource, trim($characters));
+       }
+
+       /**
+        * Removes all comments, tabs and new-line charcters to compact the content
+        *
+        * @param       $content        The uncompacted content
+        * @return      $content        The compacted content
+        */
+       public function compactContent ($content) {
+               $this->getTemplateInstance()->compactContent($content);
+       }
 }
 
 // [EOF]
index dafee49..c73e484 100644 (file)
@@ -63,6 +63,9 @@ class XmlCompactorDecorator extends BaseDecorator implements Parseable {
         * @throws      XmlCompactorDecoratorException  If an XML error was found
         */
        public function parseXmlContent ($content) {
+               // Remove all comments for better compacting
+               $content = $this->getParserInstance()->getTemplateInstance()->compactContent($content);
+
                // Parse the content
                $this->getParserInstance()->parseXmlContent($content);
        }
index ecc71c2..b64477c 100644 (file)
@@ -590,10 +590,27 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                // Was it found?
                if ($index !== false) {
                        // Remove this variable
-                       $this->varStack->offsetUnset($index);
+                       $this->unsetVariableStackOffset($index);
                } // END - if
        }
 
+       /**
+        * Unsets the given offset in the variable stack
+        *
+        * @param       $index  Index to unset
+        * @return      void
+        */
+       protected final function unsetVariableStackOffset ($index) {
+               // Is the entry there?
+               if (!isset($this->varStack[$this->currGroup][$index])) {
+                       // Abort here, we need fixing!
+                       $this->debugInstance();
+               } // END - if
+
+               // Remove it
+               unset($this->varStack[$this->currGroup][$index]);
+       }
+
        /**
         * Private setter for raw template data
         *
@@ -958,6 +975,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
 
                } // for ($varMatches ...
        }
+
        /**
         * Compiles all loaded raw templates
         *
@@ -1474,6 +1492,32 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        public final function isXmlCompactingEnabled () {
                return $this->xmlCompacting;
        }
+
+       /**
+        * Removes all commentd, tabs and new-line characters to compact the content
+        *
+        * @param       $content        The uncompacted content
+        * @return      $content        The compacted content
+        */
+       public function compactContent ($content) {
+               // First, remove all tab/new-line/revert characters
+               $content = str_replace("\t", '', str_replace("\n", '', str_replace("\r", '', $content)));
+
+               // Then regex all comments like <!-- //--> away
+               preg_match_all('/<!--[\w\W]*?(\/\/){0,1}-->/', $content, $matches);
+
+               // Do we have entries?
+               if (isset($matches[0][0])) {
+                       // Remove all
+                       foreach ($matches[0] as $match) {
+                               // Remove the match
+                               $content = str_replace($match, '', $content);
+                       } // END - foreach
+               } // END - if
+
+               // Return compacted content
+               return $content;
+       }
 }
 
 // [EOF]