X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Ftemplate%2Fclass_BaseTemplateEngine.php;h=2b9406b0f055efe0948579aae92ef378b696d306;hp=d04e154c60b0d3ddc26d7e42ae941d90cecde7df;hb=e51607c0f33062258ba0a07b79b8e1f34fd6b832;hpb=43deb129f6e8a12c37f0df17ad0870d3491bdd34 diff --git a/inc/classes/main/template/class_BaseTemplateEngine.php b/inc/classes/main/template/class_BaseTemplateEngine.php index d04e154c..2b9406b0 100644 --- a/inc/classes/main/template/class_BaseTemplateEngine.php +++ b/inc/classes/main/template/class_BaseTemplateEngine.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -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 @@ -155,6 +156,12 @@ class BaseTemplateEngine extends BaseFrameworkSystem { protected function __construct ($className) { // Call parent constructor parent::__construct($className); + + // Init file I/O instance + $ioInstance = ObjectFactory::createObjectByConfiguredName('file_io_class'); + + // Set it + $this->setFileIoInstance($ioInstance); } /** @@ -162,9 +169,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 +181,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 +221,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 +247,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 +327,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 +569,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,15 +591,32 @@ 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]); + } + /** * Private setter for raw template data * @@ -721,13 +824,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 +981,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { } // for ($varMatches ... } + /** * Compiles all loaded raw templates * @@ -1030,12 +1134,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 +1174,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 +1251,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 +1501,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]