X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Ftemplate%2Fclass_TemplateEngine.php;h=412d26ac442936a6847cf3a32392c73eb3827a53;hb=12a993738a1d1bea29a886e06478beb145c275e5;hp=4c1500625a55fdf0b5d297927517d569997b9308;hpb=375d32104957e2401b7d2d5d20a48b32abc75831;p=hub.git diff --git a/inc/classes/main/template/class_TemplateEngine.php b/inc/classes/main/template/class_TemplateEngine.php index 4c1500625..412d26ac4 100644 --- a/inc/classes/main/template/class_TemplateEngine.php +++ b/inc/classes/main/template/class_TemplateEngine.php @@ -123,7 +123,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate parent::__construct(__CLASS__); // Set part description - $this->setObjectDescription("Template-Engine"); + $this->setObjectDescription("Web template engine"); // Create unique ID number $this->createUniqueID(); @@ -138,7 +138,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate * * @param $basePath The local base path for all templates * @param $langInstance An instance of LanguageSystem (default) - * @param $ioInstance An instance of FileIOHandler (default, middleware!) + * @param $ioInstance An instance of FileIoHandler (default, middleware!) * @return $tplInstance An instance of TemplateEngine * @throws BasePathIsEmptyException If the provided $basePath is empty * @throws InvalidBasePathStringException If $basePath is no string @@ -147,7 +147,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate * @throws BasePathReadProtectedException If $basePath is * read-protected */ - public final static function createTemplateEngine ($basePath, ManageableLanguage $langInstance, FileIOHandler $ioInstance) { + public final static function createTemplateEngine ($basePath, ManageableLanguage $langInstance, FileIoHandler $ioInstance) { // Get a new instance $tplInstance = new TemplateEngine(); @@ -177,14 +177,14 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate // Set the language and IO instances $tplInstance->setLanguageInstance($langInstance); - $tplInstance->setFileIOInstance($ioInstance); + $tplInstance->setFileIoInstance($ioInstance); // Set template extensions - $tplInstance->setRawTemplateExtension($cfgInstance->readConfig("raw_template_extension")); - $tplInstance->setCodeTemplateExtension($cfgInstance->readConfig("code_template_extension")); + $tplInstance->setRawTemplateExtension($cfgInstance->readConfig('raw_template_extension')); + $tplInstance->setCodeTemplateExtension($cfgInstance->readConfig('code_template_extension')); // Absolute output path for compiled templates - $tplInstance->setCompileOutputPath(PATH . $cfgInstance->readConfig("compile_output_path")); + $tplInstance->setCompileOutputPath(PATH . $cfgInstance->readConfig('compile_output_path')); // Return the prepared instance return $tplInstance; @@ -518,7 +518,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate $ext = $this->getRawTemplateExtension(); // If we shall load a code-template we need to switch the file extension - if ($this->getTemplateType() == $this->getConfigInstance()->readConfig("code_template_type")) { + if ($this->getTemplateType() == $this->getConfigInstance()->readConfig('code_template_type')) { // Switch over to the code-template extension $ext = $this->getCodeTemplateExtension(); } @@ -548,7 +548,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate */ private function loadRawTemplateData ($fqfn) { // Get a input/output instance from the middleware - $ioInstance = $this->getFileIOInstance(); + $ioInstance = $this->getFileIoInstance(); // Validate the instance if (is_null($ioInstance)) { @@ -596,9 +596,6 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate * * @param $rawData The raw template data we shall analyze * @return void - * @throws InvalidTemplateVariableNameException If a variable name - * in a template is - * invalid */ private function extractVariablesFromRawData ($rawData) { // Cast to string @@ -611,12 +608,6 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate if ((is_array($variableMatches)) && (count($variableMatches) == 4) && (count($variableMatches[0]) > 0)) { // Initialize all missing variables foreach ($variableMatches[3] as $key=>$var) { - // Is the variable name valid? - if (($variableMatches[1][$key] != $this->getConfigInstance()->readConfig("tpl_valid_var")) && ($variableMatches[1][$key] != "config")) { - // Invalid variable name - throw new InvalidTemplateVariableNameException(array($this, $this->getLastTemplate(), $variableMatches[1][$key], $this->getConfigInstance()), self::EXCEPTION_TEMPLATE_CONTAINS_INVALID_VAR); - } - // Try to assign it, empty strings are being ignored $this->assignTemplateVariable($variableMatches[1][$key], $var); } @@ -651,24 +642,24 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate // RECURSIVE PROTECTION! BE CAREFUL HERE! if ((!isset($this->loadedRawData[$template])) && (!in_array($template, $this->loadedTemplates))) { - // Then try to search for code-templates first - try { - // Load the code template and remember it's contents - $this->loadCodeTemplate($template); - $this->loadedRawData[$template] = $this->getRawTemplateData(); + // Template not found, but maybe variable assigned? + if ($this->isVariableAlreadySet($template) !== false) { + // Use that content here + $this->loadedRawData[$template] = $this->readVariable($template); - // Remember this template for recursion detection - // RECURSIVE PROTECTION! + // Recursive protection: $this->loadedTemplates[] = $template; - } catch (FilePointerNotOpenedException $e) { - // Template not found, but maybe variable assigned? - if ($this->isVariableAlreadySet($template) !== false) { - // Use that content here - $this->loadedRawData[$template] = $this->readVariable($template); - - // Recursive protection: + } else { + // Then try to search for code-templates + try { + // Load the code template and remember it's contents + $this->loadCodeTemplate($template); + $this->loadedRawData[$template] = $this->getRawTemplateData(); + + // Remember this template for recursion detection + // RECURSIVE PROTECTION! $this->loadedTemplates[] = $template; - } else { + } catch (FilePointerNotOpenedException $e) { // Even this is not done... :/ $this->rawTemplates[] = $template; } @@ -914,7 +905,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate */ public final function loadWebTemplate ($template) { // Set template type - $this->setTemplateType($this->getConfigInstance()->readConfig("web_template_type")); + $this->setTemplateType($this->getConfigInstance()->readConfig('web_template_type')); // Load the special template $this->loadTemplate($template); @@ -929,7 +920,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate */ public final function loadEmailTemplate ($template) { // Set template type - $this->setTemplateType($this->getConfigInstance()->readConfig("email_template_type")); + $this->setTemplateType($this->getConfigInstance()->readConfig('email_template_type')); // Load the special template $this->loadTemplate($template); @@ -944,7 +935,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate */ public final function loadCodeTemplate ($template) { // Set template type - $this->setTemplateType($this->getConfigInstance()->readConfig("code_template_type")); + $this->setTemplateType($this->getConfigInstance()->readConfig('code_template_type')); // Load the special template $this->loadTemplate($template); @@ -957,7 +948,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate */ public final function compileVariables () { // Initialize the $content array - $validVar = $this->getConfigInstance()->readConfig("tpl_valid_var"); + $validVar = $this->getConfigInstance()->readConfig('tpl_valid_var'); $dummy = array(); // Iterate through all variables @@ -982,22 +973,64 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate unset($idx); unset($currVariable); - // Finalize the compilation of template variables - $this->finalizeVariableCompilation(); + // Run the compilation twice to get content from helper classes in + $cnt = 0; + while ($cnt < 3) { + // Finalize the compilation of template variables + $this->finalizeVariableCompilation(); - // Prepare the eval() command for comiling the template - $eval = sprintf("\$result = \"%s\";", - addslashes($this->getRawTemplateData()) - ); + // Prepare the eval() command for comiling the template + $eval = sprintf("\$result = \"%s\";", + addslashes($this->getRawTemplateData()) + ); - // Debug message - if ((defined('DEBUG_EVAL')) && (is_object($this->getDebugInstance()))) $this->getDebugInstance()->output(sprintf("[%s:] Constructed PHP command:
%s

\n", - $this->__toString(), - htmlentities($eval) - )); + // This loop does remove the backslashes (\) in PHP parameters + // @TODO Make this some nicer... + while (strpos($eval, ""))); + $evalMiddle = stripslashes($evalMiddle); + + // Remove the middle part from right one + $evalRight = substr($evalRight, (strpos($evalRight, "?>") + 2)); + + // And put all together + $eval = sprintf("%s<%%php %s %%>%s", $evalLeft, $evalMiddle, $evalRight); + } // END - while + + // Prepare PHP code for eval() command + $eval = str_replace( + "<%php", "\";", + str_replace( + "%>", "\$result .= \"", $eval + ) + ); + + // Debug message + if ((defined('DEBUG_EVAL')) && (is_object($this->getDebugInstance()))) $this->getDebugInstance()->output(sprintf("[%s:] Constructed PHP command:
%s

\n", + $this->__toString(), + htmlentities($eval) + )); + + // Run the constructed command. This will "compile" all variables in + eval($eval); - // Run the constructed command. This will "compile" all variables in - eval($eval); + // Set raw template data + $this->setRawTemplateData($result); + $cnt++; + } // Set the new content $this->setCompiledData($result); @@ -1008,15 +1041,15 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate * * @return void * @throws UnexpectedTemplateTypeException If the template type is - * not "code" + * not "code" * @throws InvalidArrayCountException If an unexpected array - * count has been found + * count has been found */ public final function compileTemplate () { // We will only work with template type "code" from configuration - if ($this->getTemplateType() != $this->getConfigInstance()->readConfig("code_template_type")) { + if ($this->getTemplateType() != $this->getConfigInstance()->readConfig('code_template_type')) { // Abort here - throw new UnexpectedTemplateTypeException(array($this, $this->getTemplateType(), $this->getConfigInstance()->readConfig("code_template_type")), self::EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED); + throw new UnexpectedTemplateTypeException(array($this, $this->getTemplateType(), $this->getConfigInstance()->readConfig('code_template_type')), self::EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED); } // END - if // Get the raw data. @@ -1158,6 +1191,23 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate // Get the content and set it in the response class $responseInstance->writeToBody($this->getCompiledData()); } + + /** + * Assigns all the application data with template variables + * + * @param $appInstance A manageable application instance + * @return void + */ + public function assignApplicationData (ManageableApplication $appInstance) { + // Get long name and assign it + $this->assignVariable("app_full_name" , $appInstance->getAppName()); + + // Get short name and assign it + $this->assignVariable("app_short_name", $appInstance->getAppShortName()); + + // Get version number and assign it + $this->assignVariable("app_version" , $appInstance->getAppVersion()); + } } // [EOF]