]> git.mxchange.org Git - core.git/commitdiff
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 d0de9ec092fc02c0472d7a93440faf7cc4332f71..c6b1a1a8a87d5a9b3fe5f6596844773a03720f6b 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 888531191ec53c9564f3a4f7358e58097cc7f3ae..c849ef1351eb9f746ccc25d36086179d7ff09eba 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 dafee49f5cd9e3ba7732f24f928f898a594fbaa1..c73e4841d441809b9bc542da64f1aa110da3a51b 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 ecc71c22a7de080950dc59ca6e3fd2527d37c3ee..b64477c4f10b41704cfbe582ba49b11ef1cfbfde 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]