/**
* A generic template engine
*
- * @author Roland Haeder <webmaster@ship-simu.org>
+ * @author Roland Haeder <webmaster@shipsimu.org>
* @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team
* @license GNU GPL 3.0 or any newer version
- * @link http://www.ship-simu.org
+ * @link http://www.shipsimu.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
*/
private $regExpCodeTags = '/\{\?([a-z_]+)(:("[^"]+"|[^?}]+)+)?\?\}/';
+ /**
+ * A regular expression to find template comments like <!-- Comment here //-->
+ */
+ private $regExpComments = '/<!--[\w\W]*?(\/\/){0,1}-->/';
+
/**
* Loaded helpers
*/
// First everything is not found
$found = FALSE;
- // If the stack is null, use the current group
+ // If the stack is NULL, use the current group
if (is_null($variableGroup)) {
// Use current group
//* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__.' currGroup=' . $this->currGroup . ' set as stack!');
// First everything is not found
$content = NULL;
- // If the stack is null, use the current group
+ // If the stack is NULL, use the current group
if (is_null($variableGroup)) {
// Use current group
//* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__.' currGroup=' . $this->currGroup . ' set as stack!');
// Is the variable set?
if ($index === FALSE) {
+ // Is the stack there?
+ if (!isset($this->varStack[$varGroup])) {
+ // Then initialize it here
+ $this->varStack[$varGroup] = array();
+ } // END - if
+
// Not found, add it
array_push($this->varStack[$varGroup], $this->generateVariableArray($variableName, $value));
} else {
return $this->templateType;
}
- /**
- * Assign (add) a given variable with a value
- *
- * @param $variableName The variable we are looking for
- * @param $value The value we want to store in the variable
- * @return void
- * @throws EmptyVariableException If the variable name is left empty
- */
- public final function assignVariable ($variableName, $value) {
- // Replace all dashes to underscores to match variables with configuration entries
- $variableName = trim($this->convertDashesToUnderscores($variableName));
-
- // Empty variable found?
- if (empty($variableName)) {
- // Throw an exception
- throw new EmptyVariableException(array($this, 'variableName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
- } // END - if
-
- // First search for the variable if it was already added
- $index = $this->getVariableIndex($variableName);
-
- // Was it found?
- if ($index === FALSE) {
- // Add it to the stack
- //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':ADD: ' . $variableName . '[' . gettype($value) . ']=' . $value);
- $this->addVariable($variableName, $value);
- } elseif (!empty($value)) {
- // Modify the stack entry
- //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':MOD: ' . $variableName . '[' . gettype($value) . ']=' . $value);
- $this->modifyVariable($variableName, $value);
- }
- }
-
- /**
- * Removes a given variable
- *
- * @param $variableName The variable we are looking for
- * @param $variableGroup Name of variable group (default: 'general')
- * @return void
- */
- public final function removeVariable ($variableName, $variableGroup = 'general') {
- // First search for the variable if it was already added
- $index = $this->getVariableIndex($variableName, $variableGroup);
-
- // Was it found?
- if ($index !== FALSE) {
- // Remove this variable
- //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':UNSET: variableGroup=' . $variableGroup . ',variableName=' . $variableName . ',index=' . $index);
- $this->unsetVariableStackOffset($index, $variableGroup);
- } // END - if
- }
-
/**
* Unsets the given offset in the variable group
*
// Is language support enabled?
if ($this->isLanguageSupportEnabled()) {
// Construct the FQFN for the template by honoring the current language
- $fqfn = sprintf("%s%s%s%s/%s/%s%s",
+ $fqfn = sprintf('%s%s%s%s/%s/%s%s',
$this->getConfigInstance()->getConfigEntry('base_path'),
$this->getTemplateBasePath(),
$this->getGenericBasePath(),
);
} else {
// Construct the FQFN for the template without language
- $fqfn = sprintf("%s%s%s%s/%s%s",
+ $fqfn = sprintf('%s%s%s%s/%s%s',
$this->getConfigInstance()->getConfigEntry('base_path'),
$this->getTemplateBasePath(),
$this->getGenericBasePath(),
$this->loadTemplate($template);
}
+ /**
+ * Assign (add) a given variable with a value
+ *
+ * @param $variableName The variable we are looking for
+ * @param $value The value we want to store in the variable
+ * @return void
+ * @throws EmptyVariableException If the variable name is left empty
+ */
+ public final function assignVariable ($variableName, $value) {
+ // Replace all dashes to underscores to match variables with configuration entries
+ $variableName = trim($this->convertDashesToUnderscores($variableName));
+
+ // Empty variable found?
+ if (empty($variableName)) {
+ // Throw an exception
+ throw new EmptyVariableException(array($this, 'variableName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
+ } // END - if
+
+ // First search for the variable if it was already added
+ $index = $this->getVariableIndex($variableName);
+
+ // Was it found?
+ if ($index === FALSE) {
+ // Add it to the stack
+ //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':ADD: ' . $variableName . '[' . gettype($value) . ']=' . $value);
+ $this->addVariable($variableName, $value);
+ } elseif (!empty($value)) {
+ // Modify the stack entry
+ //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':MOD: ' . $variableName . '[' . gettype($value) . ']=' . $value);
+ $this->modifyVariable($variableName, $value);
+ }
+ }
+
+ /**
+ * Removes a given variable
+ *
+ * @param $variableName The variable we are looking for
+ * @param $variableGroup Name of variable group (default: 'general')
+ * @return void
+ */
+ public final function removeVariable ($variableName, $variableGroup = 'general') {
+ // First search for the variable if it was already added
+ $index = $this->getVariableIndex($variableName, $variableGroup);
+
+ // Was it found?
+ if ($index !== FALSE) {
+ // Remove this variable
+ //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':UNSET: variableGroup=' . $variableGroup . ',variableName=' . $variableName . ',index=' . $index);
+ $this->unsetVariableStackOffset($index, $variableGroup);
+ } // END - if
+ }
+
+ /**
+ * Assigns the last loaded raw template content with a given variable
+ *
+ * @param $templateName Name of the template we want to assign
+ * @param $variableName Name of the variable we want to assign
+ * @return void
+ */
+ public function assignTemplateWithVariable ($templateName, $variableName) {
+ // Get the content from last loaded raw template
+ $content = $this->getRawTemplateData();
+
+ // Assign the variable
+ $this->assignVariable($variableName, $content);
+
+ // Purge raw content
+ $this->setRawTemplateData('');
+ }
+
/**
* Assign a given congfiguration variable with a value
*
$this->setVariable('config', $variableName, $this->getConfigInstance()->getConfigEntry($variableName));
}
+ /**
+ * Injects an array of config variables into the stack of currently loaded
+ * template. This method should only be used in very rare circumstances,
+ * e.g. when you have to copy a whole set of variables into the template
+ * engine. Before you use this method, please make sure you have considered
+ * all other possiblities.
+ *
+ * @param $variables An array with variables to be injected
+ * @return void
+ */
+ public function injectConfigVariables (array $variables) {
+ // "Inject" all
+ foreach ($variables as $name => $value) {
+ // Set variable with name for 'config' group
+ $this->setVariable('config', $name, $value);
+ } // END - foreach
+ }
+
+ /**
+ * Assigns all the application data with template variables
+ *
+ * @param $applicationInstance A manageable application instance
+ * @return void
+ */
+ public function assignApplicationData (ManageableApplication $applicationInstance) {
+ // Get long name and assign it
+ $this->assignVariable('app_full_name' , $applicationInstance->getAppName());
+
+ // Get short name and assign it
+ $this->assignVariable('app_short_name', $applicationInstance->getAppShortName());
+
+ // Get version number and assign it
+ $this->assignVariable('app_version' , $applicationInstance->getAppVersion());
+
+ // Assign extra application-depending data
+ $applicationInstance->assignExtraTemplateData($this);
+ }
+
/**
* Load a specified code template into the engine
*
$this->finalizeVariableCompilation();
// Prepare the eval() command for comiling the template
- $eval = sprintf("\$result = \"%s\";",
+ $eval = sprintf('$result = "%s";',
addslashes($this->getRawTemplateData())
);
$evalRight = substr($evalRight, (strpos($evalRight, $this->codeEnd) + 2));
// And put all together
- $eval = sprintf("%s<%%php %s %%>%s", $evalLeft, $evalMiddle, $evalRight);
+ $eval = sprintf('%s<%%php %s %%>%s', $evalLeft, $evalMiddle, $evalRight);
} // END - while
// Prepare PHP code for eval() command
$eval = str_replace(
- "<%php", "\";",
+ '<%php', '";',
str_replace(
- "%>",
+ '%>',
"\n\$result .= \"",
$eval
)
// Goes something wrong?
if ((!isset($result)) || (empty($result))) {
// Output eval command
- self::createDebugInstance(__CLASS__)->debugOutput(sprintf("Failed eval() code: <pre>%s</pre>", $this->markupCode($eval, TRUE)), TRUE);
+ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('Failed eval() code: <pre>%s</pre>', $this->markupCode($eval, TRUE)), TRUE);
// Output backtrace here
$this->debugBackTrace();
return $this->helpers[$helperName];
}
- /**
- * Assigns the last loaded raw template content with a given variable
- *
- * @param $templateName Name of the template we want to assign
- * @param $variableName Name of the variable we want to assign
- * @return void
- */
- public function assignTemplateWithVariable ($templateName, $variableName) {
- // Get the content from last loaded raw template
- $content = $this->getRawTemplateData();
-
- // Assign the variable
- $this->assignVariable($variableName, $content);
-
- // Purge raw content
- $this->setRawTemplateData('');
- }
-
/**
* Transfers the content of this template engine to a given response instance
*
$responseInstance->writeToBody($this->getCompiledData());
}
- /**
- * Assigns all the application data with template variables
- *
- * @param $applicationInstance A manageable application instance
- * @return void
- */
- public function assignApplicationData (ManageableApplication $applicationInstance) {
- // Get long name and assign it
- $this->assignVariable('app_full_name' , $applicationInstance->getAppName());
-
- // Get short name and assign it
- $this->assignVariable('app_short_name', $applicationInstance->getAppShortName());
-
- // Get version number and assign it
- $this->assignVariable('app_version' , $applicationInstance->getAppVersion());
-
- // Assign extra application-depending data
- $applicationInstance->assignExtraTemplateData($this);
- }
-
/**
* "Compiles" a variable by replacing {?var?} with it's content
*
$compactedContent = str_replace(chr(9), '', str_replace(chr(10), '', str_replace(chr(13), '', $uncompactedContent)));
// Then regex all comments like <!-- //--> away
- preg_match_all('/<!--[\w\W]*?(\/\/){0,1}-->/', $compactedContent, $matches);
+ preg_match_all($this->regExpComments, $compactedContent, $matches);
// Do we have entries?
if (isset($matches[0][0])) {