Decorators moved, naming convention applied:
[core.git] / inc / classes / main / template / class_BaseTemplateEngine.php
index 2256fab0f24879fe8c00921f4b722f8b4c349cb5..d04e154c60b0d3ddc26d7e42ae941d90cecde7df 100644 (file)
@@ -136,6 +136,11 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         */
        private $languageSupport = true;
 
+       /**
+        * XML compacting is disabled by default
+        */
+       private $xmlCompacting = false;
+
        // Exception codes for the template engine
        const EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED   = 0x110;
        const EXCEPTION_TEMPLATE_CONTAINS_INVALID_VAR = 0x111;
@@ -156,17 +161,21 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * Search for a variable in the stack
         *
         * @param       $var    The variable we are looking for
+        * @param       $stack  Optional variable stack to look in
         * @return      $idx    FALSE means not found, >=0 means found on a specific index
         */
-       private function isVariableAlreadySet ($var) {
+       private function isVariableAlreadySet ($var, $stack = null) {
                // First everything is not found
                $found = false;
 
+               // If the stack is null, use the current group
+               if (is_null($stack)) $stack = $this->currGroup;
+
                // Is the group there?
-               if (isset($this->varStack[$this->currGroup])) {
+               if ($this->isVarStackSet($stack)) {
                        // Now search for it
-                       foreach ($this->varStack[$this->currGroup] as $idx => $currEntry) {
-                               //* DEBUG: */ echo __METHOD__.":currGroup={$this->currGroup},idx={$idx},currEntry={$currEntry['name']},var={$var}<br />\n";
+                       foreach ($this->getVarStack($stack) as $idx => $currEntry) {
+                               //* DEBUG: */ echo __METHOD__.":currGroup={$stack},idx={$idx},currEntry={$currEntry['name']},var={$var}<br />\n";
                                // Is the entry found?
                                if ($currEntry['name'] == $var) {
                                        // Found!
@@ -181,27 +190,55 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                return $found;
        }
 
+       /**
+        * Checks wether the given variable stack is set
+        *
+        * @param       $stack  Variable stack to check
+        * @return      $isSet  Wether the given variable stack is set
+        */
+       protected final function isVarStackSet ($stack) {
+               // Check it
+               $isSet = isset($this->varStack[$stack]);
+
+               // Return result
+               return $isSet;
+       }
+
+       /**
+        * Getter for given variable stack
+        *
+        * @param       $stack          Variable stack to check
+        * @return      $varStack       Found variable stack
+        */
+       public final function getVarStack ($stack) {
+               return $this->varStack[$stack];
+       }
+
        /**
         * Return a content of a variable or null if not found
         *
         * @param       $var            The variable we are looking for
+        * @param       $stack          Optional variable stack to look in
         * @return      $content        Content of the variable or null if not found
         */
-       protected function readVariable ($var) {
+       protected function readVariable ($var, $stack = null) {
                // First everything is not found
                $content = null;
 
+               // If the stack is null, use the current group
+               if (is_null($stack)) $stack = $this->currGroup;
+
                // Get variable index
-               $found = $this->isVariableAlreadySet($var);
+               $found = $this->isVariableAlreadySet($var, $stack);
 
                // Is the variable found?
                if ($found !== false) {
                        // Read it
-                       $content = $this->varStack[$this->currGroup][$found]['value'];
+                       $content = $this->getVariableValue($stack, $found);
                } // END - if
 
                // Return the current position
-               //* DEBUG: */ echo __METHOD__.": group=".$this->currGroup.",var=".$var.", content[".gettype($content)."]=".$content."<br />\n";
+               //* DEBUG: */ echo __METHOD__.": group=".$stack.",var=".$var.", content[".gettype($content)."]=".$content."<br />\n";
                return $content;
        }
 
@@ -230,9 +267,9 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                $result = array();
 
                // Is the group there?
-               if (isset($this->varStack[$this->currGroup])) {
+               if ($this->isVarStackSet($this->currGroup)) {
                        // Then use it
-                       $result = $this->varStack[$this->currGroup];
+                       $result = $this->getVarStack($this->currGroup);
                } // END - if
 
                // Return result
@@ -244,7 +281,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         *
         * @param       $groupName      Name of variable group
         * @param       $add            Wether add this group
-        * @retur4n     void
+        * @retur     void
         */
        public function setVariableGroup ($groupName, $add = true) {
                // Set group name
@@ -278,7 +315,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                );
 
                // Add it to the stack
-               $this->varStack[$this->currGroup] = $currVars;
+               $this->setVarStack($this->currGroup, $currVars);
        }
 
        /**
@@ -295,7 +332,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                // Is the variable set?
                if ($idx !== false) {
                        // Then modify it
-                       $this->varStack[$this->currGroup][$idx]['value'] = $value;
+                       $this->setVariableValue($this->currGroup, $idx, $value);
                } // END - if
        }
 
@@ -486,7 +523,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        protected final function setRawTemplateData ($rawTemplateData) {
                // And store it in this class
                //* DEBUG: */ echo __METHOD__.':'.$this->getUniqueId().': '.strlen($rawTemplateData).' Bytes set.<br />\n';
-               //* DEBUG: */ echo $this->currGroup.' variables: '.count($this->varStack[$this->currGroup]).', groups='.count($this->varStack).'<br />\n';
+               //* DEBUG: */ echo $this->currGroup.' variables: '.count($this->getVarStack($this->currGroup)).', groups='.count($this->varStack).'<br />\n';
                $this->rawTemplateData = (string) $rawTemplateData;
        }
 
@@ -690,9 +727,9 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
 
                                        // Recursive protection:
                                        $this->loadedTemplates[] = $template;
-                               } elseif (isset($this->varStack['config'][$template])) {
+                               } elseif ($this->isVariableAlreadySet($template, 'config')) {
                                        // Use that content here
-                                       $this->loadedRawData[$template] = $this->varStack['config'][$template];
+                                       $this->loadedRawData[$template] = $this->readVariable($template, 'config');
 
                                        // Recursive protection:
                                        $this->loadedTemplates[] = $template;
@@ -908,18 +945,25 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                $content = $this->getRawTemplateData();
                //* DEBUG: */ echo __METHOD__.': content before='.strlen($content).' ('.md5($content).')<br />\n';
 
+               // Do we have the stack?
+               if (!$this->isVarStackSet('general')) {
+                       // Abort here silently
+                       // @TODO This silent abort should be logged, maybe.
+                       return;
+               } // END - if
+
                // Walk through all variables
-               foreach ($this->varStack['general'] as $currEntry) {
+               foreach ($this->getVarStack('general') as $currEntry) {
                        //* DEBUG: */ echo __METHOD__.': name='.$currEntry['name'].', value=<pre>'.htmlentities($currEntry['value']).'</pre>\n';
                        // Replace all [$var] or {?$var?} with the content
                        // @TODO Old behaviour, will become obsolete!
-                       $content = str_replace("\$content[".$currEntry['name'].']', $currEntry['value'], $content);
+                       $content = str_replace('$content[' . $currEntry['name'] . ']', $currEntry['value'], $content);
 
                        // @TODO Yet another old way
-                       $content = str_replace('['.$currEntry['name'].']', $currEntry['value'], $content);
+                       $content = str_replace('[' . $currEntry['name'] . ']', $currEntry['value'], $content);
 
                        // The new behaviour
-                       $content = str_replace('{?'.$currEntry['name'].'?}', $currEntry['value'], $content);
+                       $content = str_replace('{?' . $currEntry['name'] . '?}', $currEntry['value'], $content);
                } // END - for
 
                //* DEBUG: */ echo __METHOD__.': content after='.strlen($content).' ('.md5($content).')<br />\n';
@@ -952,24 +996,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        public function assignConfigVariable ($var) {
                // Sweet and simple...
                //* DEBUG: */ echo __METHOD__.':var={$var}<br />\n';
-               $this->varStack['config'][$var] = $this->getConfigInstance()->getConfigEntry($var);
-       }
-
-       /**
-        * Load a specified email template into the engine
-        *
-        * @param       $template       The email template we shall load which is located in
-        *                                              'emails' by default
-        * @return      void
-        * @deprecated
-        * @see         See loadCodeTemplate()
-        */
-       public function loadEmailTemplate ($template) {
-               // Set template type
-               $this->setTemplateType($this->getConfigInstance()->getConfigEntry('email_template_type'));
-
-               // Load the special template
-               $this->loadTemplate($template);
+               $this->setVariable('config', $var, $this->getConfigInstance()->getConfigEntry($var));
        }
 
        /**
@@ -995,13 +1022,20 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @return      void
         */
        public final function compileConfigInVariables () {
+               // Do we have the stack?
+               if (!$this->isVarStackSet('general')) {
+                       // Abort here silently
+                       // @TODO This silent abort should be logged, maybe.
+                       return;
+               } // END - if
+
                // Iterate through all general variables
-               foreach ($this->varStack['general'] as $idx=>$currVariable) {
+               foreach ($this->getVarStack('general') as $idx=>$currVariable) {
                        // Compile the value
                        $value = $this->compileRawCode($this->readVariable($currVariable['name']), true);
 
                        // Remove it from stack
-                       unset($this->varStack['general'][$idx]);
+                       $this->removeVariable($idx, 'general');
 
                        // Re-assign the variable
                        $this->assignConfigVariable($value);
@@ -1020,7 +1054,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                $dummy = array();
 
                // Iterate through all general variables
-               foreach ($this->varStack['general'] as $currVariable) {
+               foreach ($this->getVarStack('general') as $currVariable) {
                        // Transfer it's name/value combination to the $content array
                        //* DEBUG: */ echo $currVariable['name'].'=<pre>'.htmlentities($currVariable['value']).'</pre>\n';
                        $dummy[$currVariable['name']] = $currVariable['value'];
@@ -1031,8 +1065,8 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
 
                // Prepare all configuration variables
                $config = null;
-               if (isset($this->varStack['config'])) {
-                       $config = $this->varStack['config'];
+               if ($this->isVarStackSet('config')) {
+                       $config = $this->getVarStack('config');
                } // END - if
 
                // Remove some variables
@@ -1158,35 +1192,6 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                } // END - if($templateMatches ...
        }
 
-       /**
-        * A old deprecated method
-        *
-        * @return      void
-        * @deprecated
-        * @see         BaseTemplateEngine::transferToResponse
-        */
-       public function output () {
-               // Check which type of template we have
-               switch ($this->getTemplateType()) {
-                       case 'html': // Raw HTML templates can be send to the output buffer
-                               // Quick-N-Dirty:
-                               $this->getWebOutputInstance()->output($this->getCompiledData());
-                               break;
-
-                       default: // Unknown type found
-                               // Construct message
-                               $msg = sprintf("[%s-&gt;%s] Unknown/unsupported template type <span class=\"data\">%s</span> detected.",
-                                       $this->__toString(),
-                                       __FUNCTION__,
-                                       $this->getTemplateType()
-                               );
-
-                               // Write the problem to the world...
-                               $this->debugOutput($msg);
-                               break;
-               } // END - switch
-       }
-
        /**
         * Loads a given view helper (by name)
         *
@@ -1342,6 +1347,12 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                // Get a XmlParser instance
                $parserInstance = ObjectFactory::createObjectByConfiguredName('xml_parser_class', array($this));
 
+               // Check if we have XML compacting enabled
+               if ($this->isXmlCompactingEnabled()) {
+                       // Yes, so get a decorator class for transparent compacting
+                       $parserInstance = ObjectFactory::createObjectByConfiguredName('deco_compacting_xml_parser_class', array($parserInstance));
+               } // END - if
+
                // Parse the XML document
                $parserInstance->parseXmlContent($content);
        }
@@ -1364,6 +1375,25 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        public final function isLanguageSupportEnabled () {
                return $this->languageSupport;
        }
+
+       /**
+        * Enables or disables XML compacting
+        *
+        * @param       $xmlCompacting  New XML compacting setting
+        * @return      void
+        */
+       public final function enableXmlCompacting ($xmlCompacting = true) {
+               $this->xmlCompacting = (bool) $xmlCompacting;
+       }
+
+       /**
+        * Checks wether XML compacting is enabled
+        *
+        * @return      $xmlCompacting  Wether XML compacting is enabled or disabled
+        */
+       public final function isXmlCompactingEnabled () {
+               return $this->xmlCompacting;
+       }
 }
 
 // [EOF]