X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Ftemplate%2Fclass_BaseTemplateEngine.php;h=e719ebb7a1a1f63f932f74d34e4cb29e5f273179;hb=a3fa89c7cbc54491fc74f13db0927d14acf550c8;hp=6530ab0776136d165d2163667663278f8608c2c8;hpb=84e2207412d3c6ea9f940a83b2cdd4503509808a;p=core.git
diff --git a/inc/classes/main/template/class_BaseTemplateEngine.php b/inc/classes/main/template/class_BaseTemplateEngine.php
index 6530ab07..e719ebb7 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,13 +585,30 @@ 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->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]);
}
/**
@@ -721,13 +818,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');
@@ -878,6 +975,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
} // for ($varMatches ...
}
+
/**
* Compiles all loaded raw templates
*
@@ -1030,12 +1128,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 +1168,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
@@ -1147,8 +1245,11 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
* count has been found
*/
public function compileTemplate () {
+ // Get code type to make things shorter
+ $codeType = $this->getConfigInstance()->getConfigEntry('code_template_type');
+
// We will only work with template type "code" from configuration
- if ($this->getTemplateType() != $this->getConfigInstance()->getConfigEntry('code_template_type')) {
+ if (substr($this->getTemplateType(), 0, strlen($codeType)) != $codeType) {
// Abort here
throw new UnexpectedTemplateTypeException(array($this, $this->getTemplateType(), $this->getConfigInstance()->getConfigEntry('code_template_type')), self::EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED);
} // END - if
@@ -1394,6 +1495,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 $uncompactedContent The uncompacted content
+ * @return $compactedContent The compacted content
+ */
+ public function compactContent ($uncompactedContent) {
+ // First, remove all tab/new-line/revert characters
+ $compactedContent = str_replace("\t", '', str_replace("\n", '', str_replace("\r", '', $uncompactedContent)));
+
+ // Then regex all comments like away
+ preg_match_all('//', $compactedContent, $matches);
+
+ // Do we have entries?
+ if (isset($matches[0][0])) {
+ // Remove all
+ foreach ($matches[0] as $match) {
+ // Remove the match
+ $compactedContent = str_replace($match, '', $compactedContent);
+ } // END - foreach
+ } // END - if
+
+ // Return compacted content
+ return $compactedContent;
+ }
}
// [EOF]