Moved regex to variable.
[core.git] / inc / classes / main / template / class_BaseTemplateEngine.php
index 46667de55bcc0fe22a4b68e80ee344a9a1f92f04..e7592c008d7b2631f988a7cc12763a7db1189273 100644 (file)
@@ -2,11 +2,11 @@
 /**
  * A generic template engine
  *
- * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.ship-simu.org
+ * @link               http://www.shipsimu.org
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -106,6 +106,11 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         */
        private $regExpCodeTags = '/\{\?([a-z_]+)(:("[^"]+"|[^?}]+)+)?\?\}/';
 
+       /**
+        * A regular expression to find template comments like <!-- Comment here //-->
+        */
+       private $regExpComments = '/<!--[\w\W]*?(\/\/){0,1}-->/';
+
        /**
         * Loaded helpers
         */
@@ -178,7 +183,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                // First everything is not found
                $found = FALSE;
 
-               // If the stack is null, use the current group
+               // If the stack is NULL, use the current group
                if (is_null($variableGroup)) {
                        // Use current group
                        //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__.' currGroup=' . $this->currGroup . ' set as stack!');
@@ -253,7 +258,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                // First everything is not found
                $content = NULL;
 
-               // If the stack is null, use the current group
+               // If the stack is NULL, use the current group
                if (is_null($variableGroup)) {
                        // Use current group
                        //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__.' currGroup=' . $this->currGroup . ' set as stack!');
@@ -345,7 +350,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                $currVars = $this->readCurrentGroup();
 
                // Append our variable
-               $currVars[] = $this->generateVariableArray($variableName, $value);
+               array_push($currVars, $this->generateVariableArray($variableName, $value));
 
                // Add it to the stack
                $this->setVarStack($this->currGroup, $currVars);
@@ -419,8 +424,14 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
 
                // Is the variable set?
                if ($index === FALSE) {
+                       // Is the stack there?
+                       if (!isset($this->varStack[$varGroup])) {
+                               // Then initialize it here
+                               $this->varStack[$varGroup] = array();
+                       } // END - if
+
                        // Not found, add it
-                       $this->varStack[$varGroup][] = $this->generateVariableArray($variableName, $value);
+                       array_push($this->varStack[$varGroup], $this->generateVariableArray($variableName, $value));
                } else {
                        // Then modify it
                        $this->setVariableValue($this->currGroup, $index, $value);
@@ -718,7 +729,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                // Is language support enabled?
                if ($this->isLanguageSupportEnabled()) {
                        // Construct the FQFN for the template by honoring the current language
-                       $fqfn = sprintf("%s%s%s%s/%s/%s%s",
+                       $fqfn = sprintf('%s%s%s%s/%s/%s%s',
                                $this->getConfigInstance()->getConfigEntry('base_path'),
                                $this->getTemplateBasePath(),
                                $this->getGenericBasePath(),
@@ -729,7 +740,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                        );
                } else {
                        // Construct the FQFN for the template without language
-                       $fqfn = sprintf("%s%s%s%s/%s%s",
+                       $fqfn = sprintf('%s%s%s%s/%s%s',
                                $this->getConfigInstance()->getConfigEntry('base_path'),
                                $this->getTemplateBasePath(),
                                $this->getGenericBasePath(),
@@ -879,13 +890,13 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                                        $this->loadedRawData[$template] = $this->readVariable($template, 'config');
 
                                        // Recursive protection:
-                                       $this->loadedTemplates[] = $template;
+                                       array_push($this->loadedTemplates, $template);
                                } elseif ($this->getVariableIndex($template) !== FALSE) {
                                        // Use that content here
                                        $this->loadedRawData[$template] = $this->readVariable($template);
 
                                        // Recursive protection:
-                                       $this->loadedTemplates[] = $template;
+                                       array_push($this->loadedTemplates, $template);
                                } else {
                                        // Then try to search for code-templates
                                        try {
@@ -895,10 +906,10 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
 
                                                // Remember this template for recursion detection
                                                // RECURSIVE PROTECTION!
-                                               $this->loadedTemplates[] = $template;
+                                               array_push($this->loadedTemplates, $template);
                                        } catch (FileIoException $e) {
                                                // Even this is not done... :/
-                                               $this->rawTemplates[] = $template;
+                                               array_push($this->rawTemplates, $template);
                                        }
                                }
                        } // END - if
@@ -923,7 +934,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                } // END - if
 
                // Remember this template being compiled
-               $this->compiledTemplates[] = $template;
+               array_push($this->compiledTemplates, $template);
 
                // Compile the loaded code in five steps:
                //
@@ -994,7 +1005,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
 
                                        // Remember this template for recursion detection
                                        // RECURSIVE PROTECTION!
-                                       $this->loadedTemplates[] = $template;
+                                       array_push($this->loadedTemplates, $template);
                                } catch (FileIoException $e) {
                                        // This template was never found. We silently ignore it
                                        unset($this->rawTemplates[$key]);
@@ -1250,7 +1261,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                        $this->finalizeVariableCompilation();
 
                        // Prepare the eval() command for comiling the template
-                       $eval = sprintf("\$result = \"%s\";",
+                       $eval = sprintf('$result = "%s";',
                                addslashes($this->getRawTemplateData())
                        );
 
@@ -1270,14 +1281,14 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                                $evalRight = substr($evalRight, (strpos($evalRight, $this->codeEnd) + 2));
 
                                // And put all together
-                               $eval = sprintf("%s<%%php %s %%>%s", $evalLeft, $evalMiddle, $evalRight);
+                               $eval = sprintf('%s<%%php %s %%>%s', $evalLeft, $evalMiddle, $evalRight);
                        } // END - while
 
                        // Prepare PHP code for eval() command
                        $eval = str_replace(
-                               "<%php", "\";",
+                               '<%php', '";',
                                str_replace(
-                                       "%>",
+                                       '%>',
                                        "\n\$result .= \"",
                                        $eval
                                )
@@ -1289,7 +1300,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                        // Goes something wrong?
                        if ((!isset($result)) || (empty($result))) {
                                // Output eval command
-                               self::createDebugInstance(__CLASS__)->debugOutput(sprintf("Failed eval() code: <pre>%s</pre>", $this->markupCode($eval, TRUE)), TRUE);
+                               self::createDebugInstance(__CLASS__)->debugOutput(sprintf('Failed eval() code: <pre>%s</pre>', $this->markupCode($eval, TRUE)), TRUE);
 
                                // Output backtrace here
                                $this->debugBackTrace();
@@ -1586,7 +1597,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                $compactedContent = str_replace(chr(9), '', str_replace(chr(10), '', str_replace(chr(13), '', $uncompactedContent)));
 
                // Then regex all comments like <!-- //--> away
-               preg_match_all('/<!--[\w\W]*?(\/\/){0,1}-->/', $compactedContent, $matches);
+               preg_match_all($this->regExpComments, $compactedContent, $matches);
 
                // Do we have entries?
                if (isset($matches[0][0])) {