]> git.mxchange.org Git - core.git/blobdiff - inc/classes/main/template/class_BaseTemplateEngine.php
Improved some debug messages with method's name.
[core.git] / inc / classes / main / template / class_BaseTemplateEngine.php
index 4948b28f7890ba1723f05a36649e5a82706ad1c8..e2cb11f6221fa76b6ee0db993efa4d48496b5c69 100644 (file)
@@ -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
         */
@@ -583,58 +588,6 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                return $this->templateType;
        }
 
-       /**
-        * Assign (add) a given variable with a value
-        *
-        * @param       $variableName   The variable we are looking for
-        * @param       $value                  The value we want to store in the variable
-        * @return      void
-        * @throws      EmptyVariableException  If the variable name is left empty
-        */
-       public final function assignVariable ($variableName, $value) {
-               // Replace all dashes to underscores to match variables with configuration entries
-               $variableName = trim($this->convertDashesToUnderscores($variableName));
-
-               // Empty variable found?
-               if (empty($variableName)) {
-                       // Throw an exception
-                       throw new EmptyVariableException(array($this, 'variableName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
-               } // END - if
-
-               // First search for the variable if it was already added
-               $index = $this->getVariableIndex($variableName);
-
-               // Was it found?
-               if ($index === FALSE) {
-                       // Add it to the stack
-                       //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':ADD: ' . $variableName . '[' . gettype($value) . ']=' . $value);
-                       $this->addVariable($variableName, $value);
-               } elseif (!empty($value)) {
-                       // Modify the stack entry
-                       //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':MOD: ' . $variableName . '[' . gettype($value) . ']=' . $value);
-                       $this->modifyVariable($variableName, $value);
-               }
-       }
-
-       /**
-        * Removes a given variable
-        *
-        * @param       $variableName   The variable we are looking for
-        * @param       $variableGroup  Name of variable group (default: 'general')
-        * @return      void
-        */
-       public final function removeVariable ($variableName, $variableGroup = 'general') {
-               // First search for the variable if it was already added
-               $index = $this->getVariableIndex($variableName, $variableGroup);
-
-               // Was it found?
-               if ($index !== FALSE) {
-                       // Remove this variable
-                       //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':UNSET: variableGroup=' . $variableGroup . ',variableName=' . $variableName . ',index=' . $index);
-                       $this->unsetVariableStackOffset($index, $variableGroup);
-               } // END - if
-       }
-
        /**
         * Unsets the given offset in the variable group
         *
@@ -724,7 +677,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(),
@@ -735,7 +688,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(),
@@ -1156,6 +1109,76 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                $this->loadTemplate($template);
        }
 
+       /**
+        * Assign (add) a given variable with a value
+        *
+        * @param       $variableName   The variable we are looking for
+        * @param       $value                  The value we want to store in the variable
+        * @return      void
+        * @throws      EmptyVariableException  If the variable name is left empty
+        */
+       public final function assignVariable ($variableName, $value) {
+               // Replace all dashes to underscores to match variables with configuration entries
+               $variableName = trim($this->convertDashesToUnderscores($variableName));
+
+               // Empty variable found?
+               if (empty($variableName)) {
+                       // Throw an exception
+                       throw new EmptyVariableException(array($this, 'variableName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
+               } // END - if
+
+               // First search for the variable if it was already added
+               $index = $this->getVariableIndex($variableName);
+
+               // Was it found?
+               if ($index === FALSE) {
+                       // Add it to the stack
+                       //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':ADD: ' . $variableName . '[' . gettype($value) . ']=' . $value);
+                       $this->addVariable($variableName, $value);
+               } elseif (!empty($value)) {
+                       // Modify the stack entry
+                       //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':MOD: ' . $variableName . '[' . gettype($value) . ']=' . $value);
+                       $this->modifyVariable($variableName, $value);
+               }
+       }
+
+       /**
+        * Removes a given variable
+        *
+        * @param       $variableName   The variable we are looking for
+        * @param       $variableGroup  Name of variable group (default: 'general')
+        * @return      void
+        */
+       public final function removeVariable ($variableName, $variableGroup = 'general') {
+               // First search for the variable if it was already added
+               $index = $this->getVariableIndex($variableName, $variableGroup);
+
+               // Was it found?
+               if ($index !== FALSE) {
+                       // Remove this variable
+                       //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':UNSET: variableGroup=' . $variableGroup . ',variableName=' . $variableName . ',index=' . $index);
+                       $this->unsetVariableStackOffset($index, $variableGroup);
+               } // END - if
+       }
+
+       /**
+        * Assigns the last loaded raw template content with a given variable
+        *
+        * @param       $templateName   Name of the template we want to assign
+        * @param       $variableName   Name of the variable we want to assign
+        * @return      void
+        */
+       public function assignTemplateWithVariable ($templateName, $variableName) {
+               // Get the content from last loaded raw template
+               $content = $this->getRawTemplateData();
+
+               // Assign the variable
+               $this->assignVariable($variableName, $content);
+
+               // Purge raw content
+               $this->setRawTemplateData('');
+       }
+
        /**
         * Assign a given congfiguration variable with a value
         *
@@ -1171,6 +1194,44 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                $this->setVariable('config', $variableName, $this->getConfigInstance()->getConfigEntry($variableName));
        }
 
+       /**
+        * Injects an array of config variables into the stack of currently loaded
+        * template. This method should only be used in very rare circumstances,
+        * e.g. when you have to copy a whole set of variables into the template
+        * engine. Before you use this method, please make sure you have considered
+        * all other possiblities.
+        *
+        * @param       $variables      An array with variables to be injected
+        * @return      void
+        */
+       public function injectConfigVariables (array $variables) {
+               // "Inject" all
+               foreach ($variables as $name => $value) {
+                       // Set variable with name for 'config' group
+                       $this->setVariable('config', $name, $value);
+               } // END - foreach
+       }
+
+       /**
+        * Assigns all the application data with template variables
+        *
+        * @param       $applicationInstance    A manageable application instance
+        * @return      void
+        */
+       public function assignApplicationData (ManageableApplication $applicationInstance) {
+               // Get long name and assign it
+               $this->assignVariable('app_full_name' , $applicationInstance->getAppName());
+
+               // Get short name and assign it
+               $this->assignVariable('app_short_name', $applicationInstance->getAppShortName());
+
+               // Get version number and assign it
+               $this->assignVariable('app_version'   , $applicationInstance->getAppVersion());
+
+               // Assign extra application-depending data
+               $applicationInstance->assignExtraTemplateData($this);
+       }
+
        /**
         * Load a specified code template into the engine
         *
@@ -1256,7 +1317,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())
                        );
 
@@ -1276,14 +1337,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
                                )
@@ -1295,7 +1356,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();
@@ -1394,24 +1455,6 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                return $this->helpers[$helperName];
        }
 
-       /**
-        * Assigns the last loaded raw template content with a given variable
-        *
-        * @param       $templateName   Name of the template we want to assign
-        * @param       $variableName   Name of the variable we want to assign
-        * @return      void
-        */
-       public function assignTemplateWithVariable ($templateName, $variableName) {
-               // Get the content from last loaded raw template
-               $content = $this->getRawTemplateData();
-
-               // Assign the variable
-               $this->assignVariable($variableName, $content);
-
-               // Purge raw content
-               $this->setRawTemplateData('');
-       }
-
        /**
         * Transfers the content of this template engine to a given response instance
         *
@@ -1423,26 +1466,6 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                $responseInstance->writeToBody($this->getCompiledData());
        }
 
-       /**
-        * Assigns all the application data with template variables
-        *
-        * @param       $applicationInstance    A manageable application instance
-        * @return      void
-        */
-       public function assignApplicationData (ManageableApplication $applicationInstance) {
-               // Get long name and assign it
-               $this->assignVariable('app_full_name' , $applicationInstance->getAppName());
-
-               // Get short name and assign it
-               $this->assignVariable('app_short_name', $applicationInstance->getAppShortName());
-
-               // Get version number and assign it
-               $this->assignVariable('app_version'   , $applicationInstance->getAppVersion());
-
-               // Assign extra application-depending data
-               $applicationInstance->assignExtraTemplateData($this);
-       }
-
        /**
         * "Compiles" a variable by replacing {?var?} with it's content
         *
@@ -1592,7 +1615,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])) {