X-Git-Url: https://git.mxchange.org/?p=shipsimu.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Ftemplate%2Fclass_BaseTemplateEngine.php;h=7e1aa7c194f0fb6d1a015e4b640aa3f104396b3b;hp=bdeec74ac59fe13e35161021e184a4f61460f32f;hb=8ff12f905898b0c2b7ff8124c9749ad6fb9c44f3;hpb=e0317f461bd98d5c838b55ca31642d68ac8d5726 diff --git a/inc/classes/main/template/class_BaseTemplateEngine.php b/inc/classes/main/template/class_BaseTemplateEngine.php index bdeec74..7e1aa7c 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, this is free software + * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -69,11 +69,6 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ private $varStack = array(); - /** - * Configuration variables in a simple array - */ - private $configVariables = array(); - /** * Loaded templates for recursive protection and detection */ @@ -114,7 +109,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { /** * Current variable group */ - private $currGroup = "general"; + private $currGroup = 'general'; /** * All template groups except "general" @@ -122,9 +117,9 @@ class BaseTemplateEngine extends BaseFrameworkSystem { private $varGroups = array(); // Exception codes for the template engine - const EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED = 0x200; - const EXCEPTION_TEMPLATE_CONTAINS_INVALID_VAR = 0x201; - const EXCEPTION_INVALID_VIEW_HELPER = 0x202; + const EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED = 0x110; + const EXCEPTION_TEMPLATE_CONTAINS_INVALID_VAR = 0x111; + const EXCEPTION_INVALID_VIEW_HELPER = 0x112; /** * Protected constructor @@ -154,10 +149,12 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // Is the group there? if (isset($this->varStack[$this->currGroup])) { // Now search for it - foreach ($this->varStack[$this->currGroup] as $idx=>$currEntry) { + foreach ($this->varStack[$this->currGroup] as $idx => $currEntry) { + //* DEBUG: */ echo __METHOD__.":currGroup={$this->currGroup},idx={$idx},currEntry={$currEntry['name']},var={$var}
\n"; // Is the entry found? if ($currEntry['name'] == $var) { // Found! + //* DEBUG: */ echo __METHOD__.":FOUND!
\n"; $found = $idx; break; } // END - if @@ -174,7 +171,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { * @param $var The variable we are looking for * @return $content Content of the variable or null if not found */ - private function readVariable ($var) { + protected function readVariable ($var) { // First everything is not found $content = null; @@ -202,7 +199,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ private function addVariable ($var, $value) { // Set general variable group - $this->setVariableGroup("general"); + $this->setVariableGroup('general'); // Add it to the stack $this->addGroupVariable($var, $value); @@ -232,16 +229,16 @@ class BaseTemplateEngine extends BaseFrameworkSystem { * * @param $groupName Name of variable group * @param $add Wether add this group - * @return void + * @retur4n void */ public function setVariableGroup ($groupName, $add = true) { // Set group name //* DEBIG: */ echo __METHOD__.": currGroup=".$groupName."
\n"; $this->currGroup = $groupName; - // Skip group "general" - if (($groupName != "general") && ($add === true)) { - $this->varGroups[$groupName] = "OK"; + // Skip group 'general' + if (($groupName != 'general') && ($add === true)) { + $this->varGroups[$groupName] = 'OK'; } // END - if } @@ -461,7 +458,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { * @param $rawTemplateData The raw data from the template * @return void */ - private final function setRawTemplateData ($rawTemplateData) { + protected final function setRawTemplateData ($rawTemplateData) { // And store it in this class //* DEBUG: */ echo __METHOD__.":".$this->getUniqueId().": ".strlen($rawTemplateData)." Bytes set.
\n"; //* DEBUG: */ echo $this->currGroup." variables: ".count($this->varStack[$this->currGroup]).", groups=".count($this->varStack)."
\n"; @@ -491,6 +488,8 @@ class BaseTemplateEngine extends BaseFrameworkSystem { /** * Getter for compiled templates + * + * @return $compiledData Compiled template data */ public final function getCompiledData () { //* DEBUG: */ echo __METHOD__.":".$this->getUniqueId().": ".strlen($this->compiledData)." Bytes read.
\n"; @@ -550,7 +549,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { } /** - * Try to assign an extracted template variable as a "content" or "config" + * Try to assign an extracted template variable as a "content" or 'config' * variable. * * @param $varName The variable's name (shall be content orconfig) by @@ -559,7 +558,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ private function assignTemplateVariable ($varName, $var) { // Is it not a config variable? - if ($varName != "config") { + if ($varName != 'config') { // Regular template variables $this->assignVariable($var, ""); } else { @@ -584,7 +583,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // Did we find some variables? if ((is_array($variableMatches)) && (count($variableMatches) == 4) && (count($variableMatches[0]) > 0)) { // Initialize all missing variables - foreach ($variableMatches[3] as $key=>$var) { + foreach ($variableMatches[3] as $key => $var) { // Variable name $varName = $variableMatches[1][$key]; @@ -626,10 +625,17 @@ class BaseTemplateEngine extends BaseFrameworkSystem { if ((!isset($this->loadedRawData[$template])) && (!in_array($template, $this->loadedTemplates))) { // Template not found, but maybe variable assigned? + //* DEBUG: */ echo __METHOD__.":template={$template}
\n"; if ($this->isVariableAlreadySet($template) !== false) { // Use that content here $this->loadedRawData[$template] = $this->readVariable($template); + // Recursive protection: + $this->loadedTemplates[] = $template; + } elseif (isset($this->varStack['config'][$template])) { + // Use that content here + $this->loadedRawData[$template] = $this->varStack['config'][$template]; + // Recursive protection: $this->loadedTemplates[] = $template; } else { @@ -669,7 +675,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { if (in_array($template, $this->compiledTemplates)) { // Abort here... return; - } + } // END - if // Remember this template being compiled $this->compiledTemplates[] = $template; @@ -701,7 +707,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ private function insertAllTemplates (array $templateMatches) { // Run through all loaded codes - foreach ($this->loadedRawData as $template=>$code) { + foreach ($this->loadedRawData as $template => $code) { // Search for the template $foundIndex = array_search($template, $templateMatches[1]); @@ -717,9 +723,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // Set the new raw data $this->setRawTemplateData($rawData); - } // END - if - } // END - foreach } @@ -753,8 +757,8 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // This template was never found. We silently ignore it unset($this->rawTemplates[$key]); } - } - } + } // END - foreach + } // END - if } /** @@ -766,7 +770,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ private function assignAllVariables (array $varMatches) { // Search for all variables - foreach ($varMatches[1] as $key=>$var) { + foreach ($varMatches[1] as $key => $var) { // Detect leading equals if (substr($varMatches[2][$key], 0, 1) == "=") { @@ -796,7 +800,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { if (count($this->loadedRawData) > 0) { // Then compile all! - foreach ($this->loadedRawData as $template=>$code) { + foreach ($this->loadedRawData as $template => $code) { // Is this template already compiled? if (in_array($template, $this->compiledTemplates)) { @@ -836,7 +840,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ private function insertRawTemplates () { // Load all templates - foreach ($this->rawTemplates as $template=>$content) { + foreach ($this->rawTemplates as $template => $content) { // Set the template as a variable with the content $this->assignVariable($template, $content); } @@ -895,7 +899,8 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ public function assignConfigVariable ($var) { // Sweet and simple... - $this->configVariables[$var] = $this->getConfigInstance()->readConfig($var); + //* DEBUG: */ echo __METHOD__.":var={$var}
\n"; + $this->varStack['config'][$var] = $this->getConfigInstance()->readConfig($var); } /** @@ -916,7 +921,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { /** * Load a specified code template into the engine * - * @param $template The code template we shall load which is + * @param $template The code template we shall load which is * located in "code" by default * @return void */ @@ -944,14 +949,16 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // Transfer it's name/value combination to the $content array //* DEBUG: */ echo $currVariable['name']."=
".htmlentities($currVariable['value'])."
\n"; $dummy[$currVariable['name']] = $currVariable['value']; - }// END - if - // Set the new variable (don't remove the second dollar !) + // Set the new variable (don't remove the second dollar!) $$validVar = $dummy; // Prepare all configuration variables - $config = $this->configVariables; + $config = null; + if (isset($this->varStack['config'])) { + $config = $this->varStack['config']; + } // END - if // Remove some variables unset($idx); @@ -969,18 +976,12 @@ class BaseTemplateEngine extends BaseFrameworkSystem { ); // This loop does remove the backslashes (\) in PHP parameters - while (strpos($eval, ""))); @@ -1129,7 +1130,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { if (!class_exists($className)) { // Abort here! throw new ViewHelperNotFoundException(array($this, $helperName), self::EXCEPTION_INVALID_VIEW_HELPER); - } + } // END - if // Generate new instance $eval = sprintf("\$this->helpers[%s] = %s::create%s();", @@ -1140,7 +1141,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // Run the code eval($eval); - } + } // END - if // Return the requested instance return $this->helpers[$helperName]; @@ -1183,13 +1184,16 @@ class BaseTemplateEngine extends BaseFrameworkSystem { */ public function assignApplicationData (ManageableApplication $appInstance) { // Get long name and assign it - $this->assignVariable("app_full_name" , $appInstance->getAppName()); + $this->assignVariable('app_full_name' , $appInstance->getAppName()); // Get short name and assign it - $this->assignVariable("app_short_name", $appInstance->getAppShortName()); + $this->assignVariable('app_short_name', $appInstance->getAppShortName()); // Get version number and assign it - $this->assignVariable("app_version" , $appInstance->getAppVersion()); + $this->assignVariable('app_version' , $appInstance->getAppVersion()); + + // Assign extra application-depending data + $appInstance->assignExtraTemplateData($this); } /** @@ -1248,6 +1252,47 @@ class BaseTemplateEngine extends BaseFrameworkSystem { // Set the code back $this->setRawTemplateData($rawData); } + + /** + * Renders the given XML content + * + * @param $content Valid XML content or if not set the current loaded raw content + * @return void + * @throws XmlParserException If an XML error was found + */ + public final function renderXmlContent ($content = null) { + // Is the content set? + if (is_null($content)) { + // Get current content + $content = $this->getRawTemplateData(); + } // END - if + + // Convert all to UTF8 + $content = recode("html..utf8", $content); + + // Get an XML parser + $xmlParser = xml_parser_create(); + + // Force case-folding to on + xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, true); + + // Set object + xml_set_object($xmlParser, $this); + + // Set handler call-backs + xml_set_element_handler($xmlParser, 'startElement', 'endElement'); + xml_set_character_data_handler($xmlParser, 'characterHandler'); + + // Now parse the XML tree + if (!xml_parse($xmlParser, $content)) { + // Error found in XML! + //die("
".htmlentities($content)."
"); + throw new XmlParserException(array($this, $xmlParser), BaseHelper::EXCEPTION_XML_PARSER_ERROR); + } // END - if + + // Free the parser + xml_parser_free($xmlParser); + } } // [EOF]