Registration stub added, naming convention applied, support for PHP code (keep it...
[shipsimu.git] / inc / classes / main / template / class_TemplateEngine.php
index 4c1500625a55fdf0b5d297927517d569997b9308..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();
 
@@ -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"));
@@ -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)) {
@@ -612,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);
                        }
                }
        }
@@ -990,6 +991,40 @@ class TemplateEngine extends BaseFrameworkSystem implements CompileableTemplate
                        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')) && (is_object($this->getDebugInstance()))) $this->getDebugInstance()->output(sprintf("[%s:] Constructed PHP command: <pre><em>%s</em></pre><br />\n",
                        $this->__toString(),
@@ -1158,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]