From 0b0228ee8e795d5bd429449eeefcef27730c895e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Wed, 24 Mar 2010 04:31:16 +0000 Subject: [PATCH] Completed decorator, extended interface --- .../template/class_CompileableTemplate.php | 8 +++ .../class_XmlRewriterTemplateDecorator.php | 50 +++++++++++++++++++ .../xml/class_XmlCompactorDecorator.php | 3 ++ .../template/class_BaseTemplateEngine.php | 46 ++++++++++++++++- 4 files changed, 106 insertions(+), 1 deletion(-) diff --git a/inc/classes/interfaces/template/class_CompileableTemplate.php b/inc/classes/interfaces/template/class_CompileableTemplate.php index d0de9ec0..c6b1a1a8 100644 --- a/inc/classes/interfaces/template/class_CompileableTemplate.php +++ b/inc/classes/interfaces/template/class_CompileableTemplate.php @@ -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] diff --git a/inc/classes/main/decorator/template/class_XmlRewriterTemplateDecorator.php b/inc/classes/main/decorator/template/class_XmlRewriterTemplateDecorator.php index 88853119..c849ef13 100644 --- a/inc/classes/main/decorator/template/class_XmlRewriterTemplateDecorator.php +++ b/inc/classes/main/decorator/template/class_XmlRewriterTemplateDecorator.php @@ -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] diff --git a/inc/classes/main/decorator/xml/class_XmlCompactorDecorator.php b/inc/classes/main/decorator/xml/class_XmlCompactorDecorator.php index dafee49f..c73e4841 100644 --- a/inc/classes/main/decorator/xml/class_XmlCompactorDecorator.php +++ b/inc/classes/main/decorator/xml/class_XmlCompactorDecorator.php @@ -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); } diff --git a/inc/classes/main/template/class_BaseTemplateEngine.php b/inc/classes/main/template/class_BaseTemplateEngine.php index ecc71c22..b64477c4 100644 --- a/inc/classes/main/template/class_BaseTemplateEngine.php +++ b/inc/classes/main/template/class_BaseTemplateEngine.php @@ -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('//', $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] -- 2.30.2