--- /dev/null
+<?php
+/**
+ * An exception thrown when a variable is not set but should be.
+ *
+ * @author Roland Haeder <webmaster@ship-simu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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]
+?>
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
*
* @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;
// 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
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
*
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) {
$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;
}
/**
} // 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);
*/
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
}
// 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');
} // 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);
} // END - if
// Remove some variables
- unset($idx);
+ unset($index);
unset($currVariable);
// Run the compilation three times to get content from helper classes in