]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/template/class_TemplateEngine.php
Registration stub added, naming convention applied, support for PHP code (keep it...
[shipsimu.git] / inc / classes / main / template / class_TemplateEngine.php
index 6e09d8943f7ed32ee4e0b9259f9c37352a9a46fd..f929b8a1b8296373d6f9862c4943d1825426cf9d 100644 (file)
@@ -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();
 
@@ -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);
@@ -177,7 +177,7 @@ 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"));
@@ -547,15 +547,8 @@ 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();
+               $ioInstance = $this->getFileIoInstance();
 
                // Validate the instance
                if (is_null($ioInstance)) {
@@ -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);
 
@@ -625,13 +612,14 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                        // 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")) {
+                               // @TODO Find a better way than ignoring our instance variable $this
+                               if (($variableMatches[1][$key] != $this->getConfigInstance()->readConfig("tpl_valid_var")) && ($variableMatches[1][$key] != "config") && ($variableMatches[1][$key] != "this")) {
                                        // Invalid variable name
                                        throw new InvalidTemplateVariableNameException(array($this, $this->getLastTemplate(), $variableMatches[1][$key], $this->getConfigInstance()), self::EXCEPTION_TEMPLATE_CONTAINS_INVALID_VAR);
+                               } elseif ($variableMatches[1][$key] != "this") {
+                                       // Try to assign it, empty strings are being ignored
+                                       $this->assignTemplateVariable($variableMatches[1][$key], $var);
                                }
-
-                               // Try to assign it, empty strings are being ignored
-                               $this->assignTemplateVariable($variableMatches[1][$key], $var);
                        }
                }
        }
@@ -888,15 +876,14 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
 
                // 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
@@ -981,6 +968,7 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                        $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
@@ -999,18 +987,55 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                $this->finalizeVariableCompilation();
 
                // Prepare the eval() command for comiling the template
-               $eval = sprintf("\$this->setCompiledData(\"%s\");",
+               $eval = sprintf("\$result = \"%s\";",
                        addslashes($this->getRawTemplateData())
                );
 
+               // This loop does remove the backslashes (\) in PHP parameters
+               // @TODO Make this some nicer...
+               while (strpos($eval, "<?") !== false) {
+                       // Get left part before "<?"
+                       $evalLeft = substr($eval, 0, strpos($eval, "<?"));
+
+                       // Get all from right of "<?"
+                       $evalRight = substr($eval, (strpos($eval, "<?") + 2));
+
+                       // Is this a full PHP tag?
+                       if (substr(strtolower($evalRight), 0, 3) == "php") {
+                               // Remove "php" string from full PHP tag
+                               $evalRight = substr($evalRight, 3);
+                       }
+
+                       // Cut middle part out and remove escapes
+                       $evalMiddle = trim(substr($evalRight, 0, strpos($evalRight, "?>")));
+                       $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);
+               }
+
+               // Prepare PHP code for eval() command
+               $eval = str_replace(
+                       "<%php", "\";",
+                       str_replace(
+                               "%>", "\$result = \"", $eval
+                       )
+               );
+
                // 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);
        }
 
        /**
@@ -1168,6 +1193,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]