]> 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 980f56c9426b1e6eab6b451a96240bf46f41f59c..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);
@@ -254,8 +254,8 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
        private function addVariable ($var, $value) {
                // Add it to the stack
                $this->varStack->append(array(
-                       'name'  => $var,
-                       'value' => $value
+                       'name'  => trim($var),
+                       'value' => trim($value)
                ));
        }
 
@@ -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);
 
@@ -812,7 +799,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                        if ((substr($varMatches[2][$key], 0, 1) == "\"") && (substr($varMatches[2][$key], -1, 1) == "\"")) {
                                // Free string detected! Which we can assign directly
                                $this->assignVariable($var, $varMatches[3][$key]);
-                       } else {
+                       } elseif (!empty($varMatches[2][$key])) {
                                // Non-string found so we need some deeper analysis...
                                // @TODO Unfinished work or don't die here.
                                die("Deeper analysis not yet implemented!");
@@ -864,6 +851,44 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                } // END - if (count($this->loadedRawData) ...
        }
 
+       /**
+        * Inserts all raw templates into their respective variables
+        *
+        * @return      void
+        */
+       private function insertRawTemplates () {
+               // Load all templates
+               foreach ($this->rawTemplates as $template=>$content) {
+                       // Set the template as a variable with the content
+                       $this->assignVariable($template, $content);
+               }
+       }
+
+       /**
+        * 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
         *
@@ -937,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
@@ -952,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);
        }
 
        /**
@@ -981,11 +1017,9 @@ 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. Thanks to Flobee(R) for given me a hint using the
-               // modifier "m" in regular expressions. I had implemented a regex here
-               // like this: (\n|\r)
+               // Get the raw data.
                $rawData = $this->getRawTemplateData();
 
                // Remove double spaces and trim leading/trailing spaces
@@ -1012,8 +1046,15 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
 
                        // Are some raw templates found and loaded?
                        if (count($this->rawTemplates) > 0) {
-                               die("NOT YET IMPLEMENTED");
-                       }
+
+                               // Insert all raw templates
+                               $this->insertRawTemplates();
+
+                               // Remove the raw template content as well
+                               $this->setRawTemplateData("");
+
+                       } // END - if
+
                } // END - if($templateMatches ...
        }
 
@@ -1033,20 +1074,20 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                        break;
 
                default: // Unknown type found
+                       // Construct message
+                       $msg = sprintf("[%s:] Unknown/unsupported template type <strong>%s</strong> detected.",
+                               $this->__toString(),
+                               $this->getTemplateType()
+                       );
+
                        if ((is_object($this->getDebugInstance())) && (method_exists($this->getDebugInstance(), 'output'))) {
                                // Use debug output handler
-                               $this->getDebugInstance()->output(sprintf("[%s:] Unbekannter Template-Typ <strong>%s</strong> erkannt.",
-                                       $this->__toString(),
-                                       $this->getTemplateType()
-                               ));
+                               $this->getDebugInstance()->output($msg);
                                die();
                        } else {
                                // Put directly out
                                // DO NOT REWRITE THIS TO app_die() !!!
-                               die(sprintf("[%s:] Unbekannter Template-Typ <strong>%s</strong> erkannt.",
-                                       $this->__toString(),
-                                       $this->getTemplateType()
-                               ));
+                               die($msg);
                        }
                        break;
                }
@@ -1082,7 +1123,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                        );
 
                        // Run the code
-                       @eval($eval);
+                       eval($eval);
                }
 
                // Return the requested instance