]> git.mxchange.org Git - core.git/blobdiff - inc/classes/main/template/class_BaseTemplateEngine.php
Variable handling in template engine rewritten
[core.git] / inc / classes / main / template / class_BaseTemplateEngine.php
index 6530ab0776136d165d2163667663278f8608c2c8..ecc71c22a7de080950dc59ca6e3fd2527d37c3ee 100644 (file)
@@ -145,6 +145,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        const EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED   = 0x110;
        const EXCEPTION_TEMPLATE_CONTAINS_INVALID_VAR = 0x111;
        const EXCEPTION_INVALID_VIEW_HELPER           = 0x112;
+       const EXCEPTION_VARIABLE_IS_MISSING           = 0x113;
 
        /**
         * Protected constructor
@@ -162,9 +163,9 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         *
         * @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
+        * @return      $index  FALSE means not found, >=0 means found on a specific index
         */
-       private function isVariableAlreadySet ($var, $stack = null) {
+       private function getVariableIndex ($var, $stack = null) {
                // First everything is not found
                $found = false;
 
@@ -174,13 +175,13 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                // Is the group there?
                if ($this->isVarStackSet($stack)) {
                        // Now search for it
-                       foreach ($this->getVarStack($stack) as $idx => $currEntry) {
-                               //* DEBUG: */ echo __METHOD__.":currGroup={$stack},idx={$idx},currEntry={$currEntry['name']},var={$var}<br />\n";
+                       foreach ($this->getVarStack($stack) as $index => $currEntry) {
+                               //* DEBUG: */ echo __METHOD__.":currGroup={$stack},idx={$index},currEntry={$currEntry['name']},var={$var}<br />\n";
                                // Is the entry found?
                                if ($currEntry['name'] == $var) {
                                        // Found!
                                        //* DEBUG: */ echo __METHOD__.":FOUND!<br />\n";
-                                       $found = $idx;
+                                       $found = $index;
                                        break;
                                } // END - if
                        } // END - foreach
@@ -214,6 +215,17 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                return $this->varStack[$stack];
        }
 
+       /**
+        * Setter for given variable stack
+        *
+        * @param       $stack          Variable stack to check
+        * @param       $varStack       Variable stack to check
+        * @return      void
+        */
+       protected final function setVarStack ($stack, array $varStack) {
+               $this->varStack[$stack]  = $varStack;
+       }
+
        /**
         * Return a content of a variable or null if not found
         *
@@ -229,7 +241,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                if (is_null($stack)) $stack = $this->currGroup;
 
                // Get variable index
-               $found = $this->isVariableAlreadySet($var, $stack);
+               $found = $this->getVariableIndex($var, $stack);
 
                // Is the variable found?
                if ($found !== false) {
@@ -309,31 +321,99 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                $currVars = $this->readCurrentGroup();
 
                // Append our variable
-               $currVars[] = array(
-                       'name'  => $var,
-                       'value' => $value
-               );
+               $currVars[] = $this->generateVariableArray($var, $value);
 
                // Add it to the stack
                $this->setVarStack($this->currGroup, $currVars);
        }
 
+       /**
+        * Getter for variable value, throws a NoVariableException if the variable is not found
+        *
+        * @param       $varGroup       Variable group to use
+        * @param       $index          Index in variable array
+        * @return      $value          Value to set
+        */
+       private function getVariableValue ($varGroup, $index) {
+               // Return it
+               return $this->varStack[$varGroup][$index]['value'];
+       }
+
        /**
         * Modify an entry on the stack
         *
         * @param       $var    The variable we are looking for
         * @param       $value  The value we want to store in the variable
         * @return      void
+        * @throws      NoVariableException     If the given variable is not found
         */
        private function modifyVariable ($var, $value) {
                // Get index for variable
-               $idx = $this->isVariableAlreadySet($var);
+               $index = $this->getVariableIndex($var);
 
                // Is the variable set?
-               if ($idx !== false) {
-                       // Then modify it
-                       $this->setVariableValue($this->currGroup, $idx, $value);
+               if ($index === false) {
+                       // Unset variables cannot be modified
+                       throw new NoVariableException(array($this, $var, $value), self::EXCEPTION_VARIABLE_IS_MISSING);
                } // END - if
+
+               // Then modify it
+               $this->setVariableValue($this->currGroup, $index, $value);
+       }
+
+       /**
+        * Sets a variable value for given variable group and index
+        *
+        * @param       $varGroup       Variable group to use
+        * @param       $index          Index in variable array
+        * @param       $value          Value to set
+        * @return      void
+        */
+       private function setVariableValue ($varGroup, $index, $value) {
+               $this->varStack[$varGroup][$index]['value'] = $value;
+       }
+
+       /**
+        * Sets a variable within given group. This method does detect if the
+        * variable is already set. If so, the variable got modified, otherwise
+        * added.
+        *
+        * @param       $varGroup       Variable group to use
+        * @param       $var            Variable to set
+        * @param       $value          Value to set
+        * @return      void
+        */
+       protected function setVariable ($varGroup, $var, $value) {
+               // Get index for variable
+               $index = $this->getVariableIndex($var);
+
+               // Is the variable set?
+               if ($index === false) {
+                       // Not found, add it
+                       $this->varStack[$varGroup][] = $this->generateVariableArray($var, $value);
+               } else {
+                       // Then modify it
+                       $this->setVariableValue($this->currGroup, $index, $value);
+               }
+       }
+
+       /**
+        * "Generates" (better returns) an array for all variables for given
+        * variable/value pay.
+        *
+        * @param       $var            Variable to set
+        * @param       $value          Value to set
+        * @return      $varData        Variable data array
+        */
+       private function generateVariableArray ($var, $value) {
+               // Generate the temporary array
+               $varData = array(
+                       'name'  => $var,
+                       'value' => $value
+               );
+
+               // And return it
+               return $varData;
        }
 
        /**
@@ -483,10 +563,10 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                } // END - if
 
                // First search for the variable if it was already added
-               $idx = $this->isVariableAlreadySet($var);
+               $index = $this->getVariableIndex($var);
 
                // Was it found?
-               if ($idx === false) {
+               if ($index === false) {
                        // Add it to the stack
                        //* DEBUG: */ echo "ADD: ".$var."<br />\n";
                        $this->addVariable($var, $value);
@@ -505,12 +585,12 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         */
        public final function removeVariable ($var) {
                // First search for the variable if it was already added
-               $idx = $this->isVariableAlreadySet($var);
+               $index = $this->getVariableIndex($var);
 
                // Was it found?
-               if ($idx !== false) {
+               if ($index !== false) {
                        // Remove this variable
-                       $this->varStack->offsetUnset($idx);
+                       $this->varStack->offsetUnset($index);
                } // END - if
        }
 
@@ -721,13 +801,13 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
 
                                // Template not found, but maybe variable assigned?
                                //* DEBUG: */ echo __METHOD__.":template={$template}<br />\n";
-                               if ($this->isVariableAlreadySet($template) !== false) {
+                               if ($this->getVariableIndex($template) !== false) {
                                        // Use that content here
                                        $this->loadedRawData[$template] = $this->readVariable($template);
 
                                        // Recursive protection:
                                        $this->loadedTemplates[] = $template;
-                               } elseif ($this->isVariableAlreadySet($template, 'config')) {
+                               } elseif ($this->getVariableIndex($template, 'config')) {
                                        // Use that content here
                                        $this->loadedRawData[$template] = $this->readVariable($template, 'config');
 
@@ -1030,12 +1110,12 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                } // END - if
 
                // Iterate through all general variables
-               foreach ($this->getVarStack('general') as $idx=>$currVariable) {
+               foreach ($this->getVarStack('general') as $index=>$currVariable) {
                        // Compile the value
                        $value = $this->compileRawCode($this->readVariable($currVariable['name']), true);
 
                        // Remove it from stack
-                       $this->removeVariable($idx, 'general');
+                       $this->removeVariable($index, 'general');
 
                        // Re-assign the variable
                        $this->assignConfigVariable($value);
@@ -1070,7 +1150,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                } // END - if
 
                // Remove some variables
-               unset($idx);
+               unset($index);
                unset($currVariable);
 
                // Run the compilation three times to get content from helper classes in