]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/template/class_BaseTemplateEngine.php
Final fixes for variable inserting
[shipsimu.git] / inc / classes / main / template / class_BaseTemplateEngine.php
index 8a53c46859771174e3df40bd82c16e922b868da2..c0b8899b1a9d4f4f9aab0142380d2cdca3d178bf 100644 (file)
@@ -295,11 +295,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @return      void
         */
        private final function setTemplateType ($templateType) {
-               // Cast it
-               $templateType = (string) $templateType;
-
-               // And set it (only 2 letters)
-               $this->templateType = $templateType;
+               $this->templateType = (string) $templateType;
        }
 
        /**
@@ -309,9 +305,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @return      void
         */
        private final function setLastTemplate ($template) {
-               // Cast it to string
-               $template = (string) $template;
-               $this->lastTemplate = $template;
+               $this->lastTemplate = (string) $template;
        }
 
        /**
@@ -330,11 +324,8 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @return      void
         */
        public final function setBasePath ($basePath) {
-               // Cast it
-               $basePath = (string) $basePath;
-
                // And set it
-               $this->basePath = $basePath;
+               $this->basePath = (string) $basePath;
        }
 
        /**
@@ -355,11 +346,8 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @return      void
         */
        public final function setRawTemplateExtension ($templateExtension) {
-               // Cast it
-               $templateExtension = (string) $templateExtension;
-
                // And set it
-               $this->templateExtension = $templateExtension;
+               $this->templateExtension = (string) $templateExtension;
        }
 
        /**
@@ -370,11 +358,8 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @return      void
         */
        public final function setCodeTemplateExtension ($codeExtension) {
-               // Cast it
-               $codeExtension = (string) $codeExtension;
-
                // And set it
-               $this->codeExtension = $codeExtension;
+               $this->codeExtension = (string) $codeExtension;
        }
 
        /**
@@ -407,11 +392,8 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @return      void
         */
        public final function setCompileOutputPath ($compileOutputPath) {
-               // Cast it
-               $compileOutputPath = (string) $compileOutputPath;
-
                // And set it
-               $this->compileOutputPath = $compileOutputPath;
+               $this->compileOutputPath = (string) $compileOutputPath;
        }
 
        /**
@@ -480,11 +462,20 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @return      void
         */
        private final function setRawTemplateData ($rawTemplateData) {
-               // Cast it to string
-               $rawTemplateData = (string) $rawTemplateData;
-
                // And store it in this class
-               $this->rawTemplateData = $rawTemplateData;
+               //* DEBUG: */ echo __METHOD__.":".$this->getUniqueId().": ".strlen($rawTemplateData)." Bytes set.<br />\n";
+               //* DEBUG: */ echo $this->currGroup." variables: ".count($this->varStack[$this->currGroup]).", groups=".count($this->varStack)."<br />\n";
+               $this->rawTemplateData = (string) $rawTemplateData;
+       }
+
+       /**
+        * Getter for raw template data
+        *
+        * @return      $rawTemplateData        The raw data from the template
+        */
+       public final function getRawTemplateData () {
+               //* DEBUG: */ echo __METHOD__.":".$this->getUniqueId().": ".strlen($this->rawTemplateData)." Bytes read.<br />\n";
+               return $this->rawTemplateData;
        }
 
        /**
@@ -493,11 +484,17 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @return      void
         */
        private final function setCompiledData ($compiledData) {
-               // Cast it to string
-               $compiledData = (string) $compiledData;
-
                // And store it in this class
-               $this->compiledData = $compiledData;
+               //* DEBUG: */ echo __METHOD__.":".$this->getUniqueId().": ".strlen($compiledData)." Bytes set.<br />\n";
+               $this->compiledData = (string) $compiledData;
+       }
+
+       /**
+        * Getter for compiled templates
+        */
+       public final function getCompiledData () {
+               //* DEBUG: */ echo __METHOD__.":".$this->getUniqueId().": ".strlen($this->compiledData)." Bytes read.<br />\n";
+               return $this->compiledData;
        }
 
        /**
@@ -507,9 +504,6 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @return      void
         */
        private function loadTemplate ($template) {
-               // Cast it to string
-               $template = (string) $template;
-
                // Get extension for the template
                $ext = $this->getRawTemplateExtension();
 
@@ -524,7 +518,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                        $this->getBasePath(),
                        $this->getLanguageInstance()->getLanguageCode(),
                        $this->getTemplateType(),
-                       $template,
+                       (string) $template,
                        $ext
                );
 
@@ -872,41 +866,33 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        private function finalizeVariableCompilation () {
                // Get the content
                $content = $this->getRawTemplateData();
+               //* DEBUG: */ echo __METHOD__.": content before=".strlen($content)." (".md5($content).")<br />\n";
 
                // Walk through all variables
                foreach ($this->varStack['general'] as $currEntry) {
+                       //* DEBUG: */ echo __METHOD__.": name=".$currEntry['name'].", value=<pre>".htmlentities($currEntry['value'])."</pre>\n";
                        // Replace all [$var] or {?$var?} with the content
-                       //* DEBUG: */ echo "name=".$currEntry['name'].", value=<pre>".htmlentities($currEntry['value'])."</pre>\n";
+                       // Old behaviour, will become obsolete!
                        $content = str_replace("\$content[".$currEntry['name']."]", $currEntry['value'], $content);
+
+                       // Yet another old way
                        $content = str_replace("[".$currEntry['name']."]", $currEntry['value'], $content);
+
+                       // The new behaviour
                        $content = str_replace("{?".$currEntry['name']."?}", $currEntry['value'], $content);
                } // END - for
 
+               //* DEBUG: */ echo __METHOD__.": content after=".strlen($content)." (".md5($content).")<br />\n";
+
                // Set the content back
                $this->setRawTemplateData($content);
        }
 
-       /**
-        * Getter for raw template data
-        *
-        * @return      $rawTemplateData        The raw data from the template
-        */
-       public final function getRawTemplateData () {
-               return $this->rawTemplateData;
-       }
-
-       /**
-        * Getter for compiled templates
-        */
-       public final function getCompiledData () {
-               return $this->compiledData;
-       }
-
        /**
         * Load a specified web template into the engine
         *
-        * @param               $template               The web template we shall load which is
-        *                                              located in "html" by default
+        * @param       $template       The web template we shall load which is located in
+        *                                              "html" by default
         * @return      void
         */
        public function loadWebTemplate ($template) {
@@ -931,8 +917,8 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        /**
         * Load a specified email template into the engine
         *
-        * @param               $template               The email template we shall load which is
-        *                                              located in "emails" by default
+        * @param       $template       The email template we shall load which is located in
+        *                                              "emails" by default
         * @return      void
         */
        public function loadEmailTemplate ($template) {
@@ -1023,8 +1009,10 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                                $eval = sprintf("%s<%%php %s %%>%s", $evalLeft, $evalMiddle, $evalRight);
                        } // END - while
 
-                       // Prepare PHP code for eval() command
+                       // Get length for check if PHP code was found
                        $evalLength = strlen($eval);
+
+                       // Prepare PHP code for eval() command
                        $eval = str_replace(
                                "<%php", "\";",
                                str_replace(
@@ -1032,10 +1020,11 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                                )
                        );
 
-                       // Did something change?
+                       // Was PHP code found in template?
                        if (strlen($eval) != $evalLength) {
                                // Run the constructed command. This will "compile" all variables in
                                @eval($eval);
+                               //* DEBUG: */ print("<pre>".htmlentities($eval)."</pre>");
                        } // END - if
 
                        // Goes something wrong?
@@ -1044,16 +1033,19 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                                $this->debugOutput(sprintf("Failed eval() code: <pre>%s</pre>", $this->markupCode($eval, true)), true);
 
                                // Output backtrace here
-                               $this->debugBacktrace();
+                               $this->debugBackTrace();
                        } // END - if
 
                        // Set raw template data
                        $this->setRawTemplateData($result);
                        $cnt++;
-               }
+               } // END - while
+
+               // Final variable assignment
+               $this->finalizeVariableCompilation();
 
                // Set the new content
-               $this->setCompiledData($result);
+               $this->setCompiledData($this->getRawTemplateData());
        }
 
        /**
@@ -1065,7 +1057,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @throws      InvalidArrayCountException              If an unexpected array
         *                                                                                      count has been found
         */
-       public final function compileTemplate () {
+       public function compileTemplate () {
                // We will only work with template type "code" from configuration
                if ($this->getTemplateType() != $this->getConfigInstance()->readConfig('code_template_type')) {
                        // Abort here
@@ -1112,11 +1104,11 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        }
 
        /**
-        * Output the compiled page to the outside world. In case of web templates
-        * this would be vaild (X)HTML code. And in case of email templates this
-        * would store a prepared email body inside the template engine.
+        * A old deprecated method
         *
         * @return      void
+        * @deprecated
+        * @see         BaseTemplateEngine::transferToResponse
         */
        public function output () {
                // Check which type of template we have
@@ -1260,6 +1252,25 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        public final function getVariableGroups () {
                return $this->varGroups;
        }
+
+       /**
+        * Renames a variable in code and in stack
+        *
+        * @param       $oldName        Old name of variable
+        * @param       $newName        New name of variable
+        * @return      void
+        */
+       public function renameVariable ($oldName, $newName) {
+               //* DEBUG: */ echo __METHOD__.": oldName={$oldName}, newName={$newName}<br />\n";
+               // Get raw template code
+               $rawData = $this->getRawTemplateData();
+
+               // Replace it
+               $rawData = str_replace($oldName, $newName, $rawData);
+
+               // Set the code back
+               $this->setRawTemplateData($rawData);
+       }
 }
 
 // [EOF]