X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Ftemplate%2Fclass_BaseTemplateEngine.php;h=543bd88f6d182e44dd549f6e6174f2a7259f33a4;hp=cb02e4d3fc55f36ebb387d62002f12f6f73b5d76;hb=6efe73fc37d258a0f7a5ff1e95048cadd7632f30;hpb=1ee35e6d96c456b8e3499bd683f1647aa28bd501 diff --git a/inc/classes/main/template/class_BaseTemplateEngine.php b/inc/classes/main/template/class_BaseTemplateEngine.php index cb02e4d3..543bd88f 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 - 2014 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * @@ -124,7 +124,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { /** * All template groups except "general" */ - private $varGroups = array(); + private $variableGroups = array(); /** * Code begin @@ -178,7 +178,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ private function getVariableIndex ($variableName, $variableGroup = NULL) { // Replace all dashes to underscores to match variables with configuration entries - $variableName = trim($this->convertDashesToUnderscores($variableName)); + $variableName = trim(self::convertDashesToUnderscores($variableName)); // First everything is not found $found = FALSE; @@ -253,7 +253,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ protected function readVariable ($variableName, $variableGroup = NULL) { // Replace all dashes to underscores to match variables with configuration entries - $variableName = trim($this->convertDashesToUnderscores($variableName)); + $variableName = trim(self::convertDashesToUnderscores($variableName)); // First everything is not found $content = NULL; @@ -327,7 +327,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // Skip group 'general' if (($groupName != 'general') && ($add === TRUE)) { - $this->varGroups[$groupName] = 'OK'; + $this->variableGroups[$groupName] = 'OK'; } // END - if } @@ -341,7 +341,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ public function addGroupVariable ($variableName, $value) { // Replace all dashes to underscores to match variables with configuration entries - $variableName = trim($this->convertDashesToUnderscores($variableName)); + $variableName = trim(self::convertDashesToUnderscores($variableName)); // Debug message //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__.': group=' . $this->currGroup . ', variableName=' . $variableName . ', value=' . $value); @@ -359,13 +359,13 @@ class BaseTemplateEngine extends BaseFrameworkSystem { /** * Getter for variable value, throws a NoVariableException if the variable is not found * - * @param $varGroup Variable group to use + * @param $variableGroup Variable group to use * @param $index Index in variable array * @return $value Value to set */ - private function getVariableValue ($varGroup, $index) { + private function getVariableValue ($variableGroup, $index) { // Return it - return $this->varStack[$varGroup][$index]['value']; + return $this->varStack[$variableGroup][$index]['value']; } /** @@ -378,7 +378,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ private function modifyVariable ($variableName, $value) { // Replace all dashes to underscores to match variables with configuration entries - $variableName = trim($this->convertDashesToUnderscores($variableName)); + $variableName = trim(self::convertDashesToUnderscores($variableName)); // Get index for variable $index = $this->getVariableIndex($variableName); @@ -396,13 +396,13 @@ class BaseTemplateEngine extends BaseFrameworkSystem { /** * Sets a variable value for given variable group and index * - * @param $varGroup Variable group to use + * @param $variableGroup 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; + private function setVariableValue ($variableGroup, $index, $value) { + $this->varStack[$variableGroup][$index]['value'] = $value; } /** @@ -410,14 +410,14 @@ class BaseTemplateEngine extends BaseFrameworkSystem { * variable is already set. If so, the variable got modified, otherwise * added. * - * @param $varGroup Variable group to use + * @param $variableGroup Variable group to use * @param $variableName Variable to set * @param $value Value to set * @return void */ - protected function setVariable ($varGroup, $variableName, $value) { + protected function setVariable ($variableGroup, $variableName, $value) { // Replace all dashes to underscores to match variables with configuration entries - $variableName = trim($this->convertDashesToUnderscores($variableName)); + $variableName = trim(self::convertDashesToUnderscores($variableName)); // Get index for variable $index = $this->getVariableIndex($variableName); @@ -425,13 +425,13 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // Is the variable set? if ($index === FALSE) { // Is the stack there? - if (!isset($this->varStack[$varGroup])) { + if (!isset($this->varStack[$variableGroup])) { // Then initialize it here - $this->varStack[$varGroup] = array(); + $this->varStack[$variableGroup] = array(); } // END - if // Not found, add it - array_push($this->varStack[$varGroup], $this->generateVariableArray($variableName, $value)); + array_push($this->varStack[$variableGroup], $this->generateVariableArray($variableName, $value)); } else { // Then modify it $this->setVariableValue($this->currGroup, $index, $value); @@ -448,7 +448,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ private function generateVariableArray ($variableName, $value) { // Replace all dashes to underscores to match variables with configuration entries - $variableName = trim($this->convertDashesToUnderscores($variableName)); + $variableName = trim(self::convertDashesToUnderscores($variableName)); // Generate the temporary array $varData = array( @@ -471,6 +471,15 @@ class BaseTemplateEngine extends BaseFrameworkSystem { $this->templateType = (string) $templateType; } + /** + * Getter for template type + * + * @return $templateType The current template's type + */ + public final function getTemplateType () { + return $this->templateType; + } + /** * Setter for the last loaded template's FQFN * @@ -496,7 +505,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { * @param $templateBasePath The relative base path for all templates * @return void */ - public final function setTemplateBasePath ($templateBasePath) { + protected final function setTemplateBasePath ($templateBasePath) { // And set it $this->templateBasePath = (string) $templateBasePath; } @@ -528,7 +537,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { * templates * @return void */ - public final function setRawTemplateExtension ($templateExtension) { + protected final function setRawTemplateExtension ($templateExtension) { // And set it $this->templateExtension = (string) $templateExtension; } @@ -540,7 +549,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { * templates * @return void */ - public final function setCodeTemplateExtension ($codeExtension) { + protected final function setCodeTemplateExtension ($codeExtension) { // And set it $this->codeExtension = (string) $codeExtension; } @@ -574,20 +583,11 @@ class BaseTemplateEngine extends BaseFrameworkSystem { * templates * @return void */ - public final function setCompileOutputPath ($compileOutputPath) { + protected final function setCompileOutputPath ($compileOutputPath) { // And set it $this->compileOutputPath = (string) $compileOutputPath; } - /** - * Getter for template type - * - * @return $templateType The current template's type - */ - public final function getTemplateType () { - return $this->templateType; - } - /** * Unsets the given offset in the variable group * @@ -647,7 +647,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { } /** - * Getter for compiled templates + * Getter for compiled templates, must be public for e.g. Mailer classes. * * @return $compiledData Compiled template data */ @@ -662,7 +662,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { * @param $template The template we shall load * @param $extOther An other extension to use * @return void - * @throws FileIoException If the template was not found + * @throws FileNotFoundException If the template was not found */ protected function loadTemplate ($template, $extOther = '') { // Get extension for the template if empty @@ -702,9 +702,9 @@ class BaseTemplateEngine extends BaseFrameworkSystem { try { // Load the raw template data $this->loadRawTemplateData($fqfn); - } catch (FileIoException $e) { + } catch (FileNotFoundException $e) { // If we shall load a code-template we need to switch the file extension - if (($this->getTemplateType() != $this->getConfigInstance()->getConfigEntry('web_template_type')) && (empty($extOther))) { + if (($this->getTemplateType() != $this->getConfigInstance()->getConfigEntry('html_template_type')) && (empty($extOther))) { // Switch over to the code-template extension and try it again $ext = $this->getCodeTemplateExtension(); @@ -712,7 +712,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { $this->loadTemplate($template, $ext); } else { // Throw it again - throw new FileIoException($fqfn, FrameworkFileInputPointer::EXCEPTION_FILE_NOT_FOUND); + throw new FileNotFoundException($fqfn, self::EXCEPTION_FILE_NOT_FOUND); } } @@ -742,25 +742,25 @@ class BaseTemplateEngine extends BaseFrameworkSystem { * Try to assign an extracted template variable as a "content" or 'config' * variable. * - * @param $varName The variable's name (shall be content or config) + * @param $variableName The variable's name (shall be content or config) * by default * @param $variableName The variable we want to assign * @return void */ - private function assignTemplateVariable ($varName, $var) { + private function assignTemplateVariable ($variableName, $var) { // Replace all dashes to underscores to match variables with configuration entries - $variableName = trim($this->convertDashesToUnderscores($variableName)); + $variableName = trim(self::convertDashesToUnderscores($variableName)); // Debug message - //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: varName=' . $varName . ',variableName=' . $variableName); + //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: variableName=' . $variableName . ',variableName=' . $variableName); // Is it not a config variable? - if ($varName != 'config') { + if ($variableName != 'config') { // Regular template variables $this->assignVariable($variableName, ''); } else { // Configuration variables - $this->assignConfigVariable($variableName); + $this->assignConfigVariable($var); } } @@ -785,12 +785,12 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // Initialize all missing variables foreach ($variableMatches[3] as $key => $var) { // Variable name - $varName = $variableMatches[1][$key]; + $variableName = $variableMatches[1][$key]; // Workarround: Do not assign empty variables if (!empty($var)) { // Try to assign it, empty strings are being ignored - $this->assignTemplateVariable($varName, $var); + $this->assignTemplateVariable($variableName, $var); } // END - if } // END - foreach } // END - if @@ -830,13 +830,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:template=' . $template); // Template not found, but maybe variable assigned? - if ($this->getVariableIndex($template, 'config') !== FALSE) { - // Use that content here - $this->loadedRawData[$template] = $this->readVariable($template, 'config'); - - // Recursive protection: - array_push($this->loadedTemplates, $template); - } elseif ($this->getVariableIndex($template) !== FALSE) { + if ($this->getVariableIndex($template) !== FALSE) { // Use that content here $this->loadedRawData[$template] = $this->readVariable($template); @@ -852,7 +846,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // Remember this template for recursion detection // RECURSIVE PROTECTION! array_push($this->loadedTemplates, $template); - } catch (FileIoException $e) { + } catch (FileNotFoundException $e) { // Even this is not done... :/ array_push($this->rawTemplates, $template); } @@ -940,7 +934,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { foreach ($this->rawTemplates as $key => $template) { try { // Load the template - $this->loadWebTemplate($template); + $this->loadHtmlTemplate($template); // Remember it's contents $this->rawTemplates[$template] = $this->getRawTemplateData(); @@ -951,7 +945,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // Remember this template for recursion detection // RECURSIVE PROTECTION! array_push($this->loadedTemplates, $template); - } catch (FileIoException $e) { + } catch (FileNotFoundException $e) { // This template was never found. We silently ignore it unset($this->rawTemplates[$key]); } @@ -973,7 +967,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // Search for all variables foreach ($varMatches[1] as $key => $var) { // Replace all dashes to underscores to match variables with configuration entries - $var = trim($this->convertDashesToUnderscores($var)); + $var = trim(self::convertDashesToUnderscores($var)); // Debug message self::createDebugInstance(__CLASS__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:key=' . $key . ',var=' . $var); @@ -1092,15 +1086,15 @@ class BaseTemplateEngine extends BaseFrameworkSystem { } /** - * Load a specified web template into the engine + * Load a specified HTML template into the engine * * @param $template The web template we shall load which is located in * 'html' by default * @return void */ - public function loadWebTemplate ($template) { + public function loadHtmlTemplate ($template) { // Set template type - $this->setTemplateType($this->getConfigInstance()->getConfigEntry('web_template_type')); + $this->setTemplateType($this->getConfigInstance()->getConfigEntry('html_template_type')); // Load the special template $this->loadTemplate($template); @@ -1116,7 +1110,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ public final function assignVariable ($variableName, $value) { // Replace all dashes to underscores to match variables with configuration entries - $variableName = trim($this->convertDashesToUnderscores($variableName)); + $variableName = trim(self::convertDashesToUnderscores($variableName)); // Empty variable found? if (empty($variableName)) { @@ -1184,28 +1178,28 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ public function assignConfigVariable ($variableName) { // Replace all dashes to underscores to match variables with configuration entries - $variableName = trim($this->convertDashesToUnderscores($variableName)); + $variableName = trim(self::convertDashesToUnderscores($variableName)); // Sweet and simple... //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: variableName=' . $variableName . ',getConfigEntry()=' . $this->getConfigInstance()->getConfigEntry($variableName)); - $this->setVariable('config', $variableName, $this->getConfigInstance()->getConfigEntry($variableName)); + $this->assignVariable($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. + * Assigns a lot 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 + * @param $variables An array with variables to be assigned * @return void */ - public function injectConfigVariables (array $variables) { + public function assignMultipleVariables (array $variables) { // "Inject" all foreach ($variables as $name => $value) { // Set variable with name for 'config' group - $this->setVariable('config', $name, $value); + $this->assignVariable($name, $value); } // END - foreach } @@ -1238,7 +1232,22 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ public function loadCodeTemplate ($template) { // Set template type - $this->setTemplateType($this->getConfigInstance()->getConfigEntry('code_template_type')); + $this->setTemplateType($this->getConfigInstance()->getConfigEntry('code_' . self::getResponseTypeFromSystem() . '_template_type')); + + // Load the special template + $this->loadTemplate($template); + } + + /** + * Load a specified email template into the engine + * + * @param $template The email template we shall load which is + * located in 'emails' by default + * @return void + */ + public function loadEmailTemplate ($template) { + // Set template type + $this->setTemplateType($this->getConfigInstance()->getConfigEntry('email_template_type')); // Load the special template $this->loadTemplate($template); @@ -1277,7 +1286,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { $this->assignConfigVariable($value); } else { // Re-assign the value directly - $this->setVariable('config', $currVariable['name'], $value); + $this->assignVariable($currVariable['name'], $value); } } // END - foreach } @@ -1382,12 +1391,12 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ public function compileTemplate () { // Get code type to make things shorter - $codeType = $this->getConfigInstance()->getConfigEntry('code_template_type'); + $codeType = $this->getConfigInstance()->getConfigEntry('code_' . self::getResponseTypeFromSystem() . '_template_type'); // We will only work with template type "code" from configuration 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); + throw new UnexpectedTemplateTypeException(array($this, $this->getTemplateType(), $this->getConfigInstance()->getConfigEntry('code_' . self::getResponseTypeFromSystem() . '_template_type')), self::EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED); } // END - if // Get the raw data. @@ -1439,7 +1448,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // Is this view helper loaded? if (!isset($this->helpers[$helperName])) { // Create a class name - $className = $this->convertToClassName($helperName) . 'ViewHelper'; + $className = self::convertToClassName($helperName) . 'ViewHelper'; // Generate new instance $this->helpers[$helperName] = ObjectFactory::createObjectByName($className); @@ -1508,10 +1517,10 @@ class BaseTemplateEngine extends BaseFrameworkSystem { /** * Getter for variable group array * - * @return $vargroups All variable groups + * @return $variableGroups All variable groups */ public final function getVariableGroups () { - return $this->varGroups; + return $this->variableGroups; } /** @@ -1550,7 +1559,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // Get a XmlParser instance $parserInstance = ObjectFactory::createObjectByConfiguredName('xml_parser_class', array($this)); - // Check if we have XML compacting enabled + // Check if XML compacting is enabled if ($this->isXmlCompactingEnabled()) { // Yes, so get a decorator class for transparent compacting $parserInstance = ObjectFactory::createObjectByConfiguredName('deco_compacting_xml_parser_class', array($parserInstance));