From: Roland Häder Date: Mon, 1 Mar 2010 01:15:38 +0000 (+0000) Subject: Variable handling in template engine rewritten X-Git-Url: https://git.mxchange.org/?p=core.git;a=commitdiff_plain;h=bd8a0f8e45a51ded51fd3afb2996c5e29f6852aa;ds=sidebyside Variable handling in template engine rewritten --- diff --git a/.gitattributes b/.gitattributes index 2a7ae369..17cd397a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -111,6 +111,7 @@ inc/classes/exceptions/template/class_BasePathIsEmptyException.php -text inc/classes/exceptions/template/class_BasePathIsNoDirectoryException.php -text inc/classes/exceptions/template/class_BasePathReadProtectedException.php -text inc/classes/exceptions/template/class_InvalidBasePathStringException.php -text +inc/classes/exceptions/template/class_NoVariableException.php -text inc/classes/exceptions/template/class_UnexpectedTemplateTypeException.php -text inc/classes/exceptions/template/class_UnsupportedTemplateEngineException.php -text inc/classes/exceptions/user/.htaccess -text diff --git a/inc/classes/exceptions/template/class_NoVariableException.php b/inc/classes/exceptions/template/class_NoVariableException.php new file mode 100644 index 00000000..56c6ac91 --- /dev/null +++ b/inc/classes/exceptions/template/class_NoVariableException.php @@ -0,0 +1,47 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class NoVariableException extends FrameworkException { + /** + * The constructor + * + * @param $messageArray Data for the message + * @param $code Code number for the exception + * @return void + */ + public function __construct (array $messageArray, $code) { + // Add a message around the missing class + $message = sprintf("[%s:%d] Variable %s is not set, cannot modify(?) it's value to %s.", + $messageArray[0]->__toString(), + $this->getLine(), + $messageArray[1], + $messageArray[2] + ); + + // Call parent constructor + parent::__construct($message, $code); + } +} + +// [EOF] +?> diff --git a/inc/classes/main/menu/class_BaseMenu.php b/inc/classes/main/menu/class_BaseMenu.php index 8ab109f8..4dd1d9c4 100644 --- a/inc/classes/main/menu/class_BaseMenu.php +++ b/inc/classes/main/menu/class_BaseMenu.php @@ -64,7 +64,7 @@ class BaseMenu extends BaseFrameworkSystem { // Render it here $this->getTemplateInstance()->renderXmlContent($menuContent); - die('!OK'); + //die('!OK'); } /** diff --git a/inc/classes/main/template/class_BaseTemplateEngine.php b/inc/classes/main/template/class_BaseTemplateEngine.php index 6530ab07..ecc71c22 100644 --- a/inc/classes/main/template/class_BaseTemplateEngine.php +++ b/inc/classes/main/template/class_BaseTemplateEngine.php @@ -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}
\n"; + foreach ($this->getVarStack($stack) as $index => $currEntry) { + //* DEBUG: */ echo __METHOD__.":currGroup={$stack},idx={$index},currEntry={$currEntry['name']},var={$var}
\n"; // Is the entry found? if ($currEntry['name'] == $var) { // Found! //* DEBUG: */ echo __METHOD__.":FOUND!
\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."
\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}
\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