]> git.mxchange.org Git - core.git/blobdiff - inc/classes/main/template/class_BaseTemplateEngine.php
Some cleanups, more usage of ObjectFactory:
[core.git] / inc / classes / main / template / class_BaseTemplateEngine.php
index ecc71c22a7de080950dc59ca6e3fd2527d37c3ee..3e265df6488b99b950e6043a911d4fbe19542753 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -87,12 +87,12 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        /**
         * Loaded raw template data
         */
-       private $loadedRawData = null;
+       private $loadedRawData = NULL;
 
        /**
         * Raw templates which are linked in code templates
         */
-       private $rawTemplates = null;
+       private $rawTemplates = NULL;
 
        /**
         * A regular expression for variable=value pairs
@@ -156,6 +156,12 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        protected function __construct ($className) {
                // Call parent constructor
                parent::__construct($className);
+
+               // Init file I/O instance
+               $ioInstance = ObjectFactory::createObjectByConfiguredName('file_io_class');
+
+               // Set it
+               $this->setFileIoInstance($ioInstance);
        }
 
        /**
@@ -165,7 +171,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @param       $stack  Optional variable stack to look in
         * @return      $index  FALSE means not found, >=0 means found on a specific index
         */
-       private function getVariableIndex ($var, $stack = null) {
+       private function getVariableIndex ($var, $stack = NULL) {
                // First everything is not found
                $found = false;
 
@@ -233,9 +239,9 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @param       $stack          Optional variable stack to look in
         * @return      $content        Content of the variable or null if not found
         */
-       protected function readVariable ($var, $stack = null) {
+       protected function readVariable ($var, $stack = NULL) {
                // First everything is not found
-               $content = null;
+               $content = NULL;
 
                // If the stack is null, use the current group
                if (is_null($stack)) $stack = $this->currGroup;
@@ -590,8 +596,25 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                // Was it found?
                if ($index !== false) {
                        // Remove this variable
-                       $this->varStack->offsetUnset($index);
+                       $this->unsetVariableStackOffset($index);
+               } // END - if
+       }
+
+       /**
+        * Unsets the given offset in the variable stack
+        *
+        * @param       $index  Index to unset
+        * @return      void
+        */
+       protected final function unsetVariableStackOffset ($index) {
+               // Is the entry there?
+               if (!isset($this->varStack[$this->currGroup][$index])) {
+                       // Abort here, we need fixing!
+                       $this->debugInstance();
                } // END - if
+
+               // Remove it
+               unset($this->varStack[$this->currGroup][$index]);
        }
 
        /**
@@ -958,6 +981,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
 
                } // for ($varMatches ...
        }
+
        /**
         * Compiles all loaded raw templates
         *
@@ -1144,7 +1168,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                $$validVar = $dummy;
 
                // Prepare all configuration variables
-               $config = null;
+               $config = NULL;
                if ($this->isVarStackSet('config')) {
                        $config = $this->getVarStack('config');
                } // END - if
@@ -1227,8 +1251,11 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         *                                                                                      count has been found
         */
        public function compileTemplate () {
+               // Get code type to make things shorter
+               $codeType = $this->getConfigInstance()->getConfigEntry('code_template_type');
+
                // We will only work with template type "code" from configuration
-               if ($this->getTemplateType() != $this->getConfigInstance()->getConfigEntry('code_template_type')) {
+               if (substr($this->getTemplateType(), 0, strlen($codeType)) != $codeType) {
                        // Abort here
                        throw new UnexpectedTemplateTypeException(array($this, $this->getTemplateType(), $this->getConfigInstance()->getConfigEntry('code_template_type')), self::EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED);
                } // END - if
@@ -1327,21 +1354,21 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        /**
         * Assigns all the application data with template variables
         *
-        * @param       $appInstance    A manageable application instance
+        * @param       $applicationInstance    A manageable application instance
         * @return      void
         */
-       public function assignApplicationData (ManageableApplication $appInstance) {
+       public function assignApplicationData (ManageableApplication $applicationInstance) {
                // Get long name and assign it
-               $this->assignVariable('app_full_name' , $appInstance->getAppName());
+               $this->assignVariable('app_full_name' , $applicationInstance->getAppName());
 
                // Get short name and assign it
-               $this->assignVariable('app_short_name', $appInstance->getAppShortName());
+               $this->assignVariable('app_short_name', $applicationInstance->getAppShortName());
 
                // Get version number and assign it
-               $this->assignVariable('app_version'   , $appInstance->getAppVersion());
+               $this->assignVariable('app_version'   , $applicationInstance->getAppVersion());
 
                // Assign extra application-depending data
-               $appInstance->assignExtraTemplateData($this);
+               $applicationInstance->assignExtraTemplateData($this);
        }
 
        /**
@@ -1417,7 +1444,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @return      void
         * @throws      XmlParserException      If an XML error was found
         */
-       public function renderXmlContent ($content = null) {
+       public function renderXmlContent ($content = NULL) {
                // Is the content set?
                if (is_null($content)) {
                        // Get current content
@@ -1474,6 +1501,35 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        public final function isXmlCompactingEnabled () {
                return $this->xmlCompacting;
        }
+
+       /**
+        * Removes all commentd, tabs and new-line characters to compact the content
+        *
+        * @param       $uncompactedContent             The uncompacted content
+        * @return      $compactedContent               The compacted content
+        */
+       public function compactContent ($uncompactedContent) {
+               // First, remove all tab/new-line/revert characters
+               $compactedContent = str_replace("\t", '', str_replace("\n", '', str_replace("\r", '', $uncompactedContent)));
+
+               // Then regex all comments like <!-- //--> away
+               preg_match_all('/<!--[\w\W]*?(\/\/){0,1}-->/', $compactedContent, $matches);
+
+               // Do we have entries?
+               if (isset($matches[0][0])) {
+                       // Remove all
+                       foreach ($matches[0] as $match) {
+                               // Remove the match
+                               $compactedContent = str_replace($match, '', $compactedContent);
+                       } // END - foreach
+               } // END - if
+
+               // Set the content again
+               $this->setRawTemplateData($compactedContent);
+
+               // Return compacted content
+               return $compactedContent;
+       }
 }
 
 // [EOF]