]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/template/class_TemplateEngine.php
getConfigInstance() is now protected, factories added
[shipsimu.git] / inc / classes / main / template / class_TemplateEngine.php
index 16fbd3c8f567b500551e81c7cfaba0e9ffc84352..4c1500625a55fdf0b5d297927517d569997b9308 100644 (file)
@@ -167,7 +167,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                }
 
                // Get configuration instance
-               $cfgInstance = $tplInstance->getConfigInstance();
+               $cfgInstance = FrameworkConfiguration::getInstance();
 
                // Set the base path
                $tplInstance->setBasePath($basePath);
@@ -547,13 +547,6 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
         *                                                              required method
         */
        private function loadRawTemplateData ($fqfn) {
-               // Debug message
-               if ((defined('DEBUG_TEMPLATE')) && (is_object($this->getDebugInstance()))) $this->getDebugInstance()->output(sprintf("[%s:] Template <strong>%s</strong> vom Typ <strong>%s</strong> wird geladen.<br />\n",
-                       $this->__toString(),
-                       $template,
-                       $this->getTemplateType()
-               ));
-
                // Get a input/output instance from the middleware
                $ioInstance = $this->getFileIOInstance();
 
@@ -572,12 +565,6 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                // Load the raw template
                $rawTemplateData = $ioInstance->loadFileContents($fqfn);
 
-               // Debug message
-               if ((defined('DEBUG_TEMPLATE')) && (is_object($this->getDebugInstance()))) $this->getDebugInstance()->output(sprintf("[%s:] <strong>%s</strong> Byte Rohdaten geladen.<br />\n",
-                       $this->__toString(),
-                       strlen($rawTemplateData)
-               ));
-
                // Store the template's contents into this class
                $this->setRawTemplateData($rawTemplateData);
 
@@ -877,6 +864,31 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                }
        }
 
+       /**
+        * Finalizes the compilation of all template variables
+        *
+        * @return      void
+        */
+       private function finalizeVariableCompilation () {
+               // Get the content
+               $content = $this->getRawTemplateData();
+
+               // Walk through all variables
+               for ($idx = $this->varStack->getIterator(); $idx->valid(); $idx->next()) {
+                       // Get current entry
+                       $currEntry = $idx->current();
+
+                       // Replace all [$var] or {?$var?} with the content
+                       //* DEBUG: */ echo "name=".$currEntry['name'].", value=<pre>".htmlentities($currEntry['value'])."</pre>\n";
+                       $content = str_replace("\$content[".$currEntry['name']."]", $currEntry['value'], $content);
+                       $content = str_replace("[".$currEntry['name']."]", $currEntry['value'], $content);
+                       $content = str_replace("{?".$currEntry['name']."?}", $currEntry['value'], $content);
+               } // END - for
+
+               // Set the content back
+               $this->setRawTemplateData($content);
+       }
+
        /**
         * Getter for raw template data
         *
@@ -950,12 +962,17 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
 
                // Iterate through all variables
                for ($idx = $this->varStack->getIterator(); $idx->valid(); $idx->next()) {
+
                        // Get current variable from the stack
                        $currVariable = $idx->current();
 
                        // Transfer it's name/value combination to the $content array
+                       //* DEBUG: */ echo $currVariable['name']."=<pre>".htmlentities($currVariable['value'])."</pre>\n";
                        $dummy[$currVariable['name']] = $currVariable['value'];
-               }
+
+               }// END - if
+
+               // Set the new variable (don't remove the second dollar !)
                $$validVar = $dummy;
 
                // Prepare all configuration variables
@@ -965,19 +982,25 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                unset($idx);
                unset($currVariable);
 
+               // Finalize the compilation of template variables
+               $this->finalizeVariableCompilation();
+
                // Prepare the eval() command for comiling the template
-               $eval = sprintf("\$this->setCompiledData(\"%s\");",
+               $eval = sprintf("\$result = \"%s\";",
                        addslashes($this->getRawTemplateData())
                );
 
                // Debug message
-               if (((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) && (is_object($this->getDebugInstance()))) $this->getDebugInstance()->output(sprintf("[%s:] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
+               if ((defined('DEBUG_EVAL')) && (is_object($this->getDebugInstance()))) $this->getDebugInstance()->output(sprintf("[%s:] Constructed PHP command: <pre><em>%s</em></pre><br />\n",
                        $this->__toString(),
                        htmlentities($eval)
                ));
 
                // Run the constructed command. This will "compile" all variables in
                eval($eval);
+
+               // Set the new content
+               $this->setCompiledData($result);
        }
 
        /**
@@ -994,7 +1017,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                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);
-               }
+               } // END - if
 
                // Get the raw data.
                $rawData = $this->getRawTemplateData();
@@ -1014,7 +1037,6 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                        //
                        // The main analysis
                        $this->analyzeTemplate($templateMatches);
-                       $this->debugInstance();
 
                        // Compile raw template data
                        $this->compileRawTemplateData($templateMatches);
@@ -1024,9 +1046,14 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
 
                        // Are some raw templates found and loaded?
                        if (count($this->rawTemplates) > 0) {
+
                                // Insert all raw templates
                                $this->insertRawTemplates();
-                       }
+
+                               // Remove the raw template content as well
+                               $this->setRawTemplateData("");
+
+                       } // END - if
 
                } // END - if($templateMatches ...
        }