Made readVariable() protected again, if you need access to this method, implement...
[core.git] / inc / classes / main / template / class_BaseTemplateEngine.php
index b64477c4f10b41704cfbe582ba49b11ef1cfbfde..231d438235b7fd0d8767e0578b7917fa716bbe4c 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,22 +171,26 @@ 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;
 
                // If the stack is null, use the current group
-               if (is_null($stack)) $stack = $this->currGroup;
+               if (is_null($stack)) {
+                       // Use current group
+                       //* DEBUG: */ print __METHOD__.' currGroup=' . $this->currGroup . ' set as stack!<br />' . chr(10);
+                       $stack = $this->currGroup;
+               } // END - if
 
                // Is the group there?
                if ($this->isVarStackSet($stack)) {
                        // Now search for it
                        foreach ($this->getVarStack($stack) as $index => $currEntry) {
-                               //* DEBUG: */ echo __METHOD__.":currGroup={$stack},idx={$index},currEntry={$currEntry['name']},var={$var}<br />\n";
+                               //* DEBUG: */ print __METHOD__.":currGroup={$stack},idx={$index},currEntry={$currEntry['name']},var={$var}<br />\n";
                                // Is the entry found?
                                if ($currEntry['name'] == $var) {
                                        // Found!
-                                       //* DEBUG: */ echo __METHOD__.":FOUND!<br />\n";
+                                       //* DEBUG: */ print __METHOD__.":FOUND!<br />\n";
                                        $found = $index;
                                        break;
                                } // END - if
@@ -192,10 +202,10 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        }
 
        /**
-        * Checks wether the given variable stack is set
+        * Checks whether the given variable stack is set
         *
         * @param       $stack  Variable stack to check
-        * @return      $isSet  Wether the given variable stack is set
+        * @return      $isSet  Whether the given variable stack is set
         */
        protected final function isVarStackSet ($stack) {
                // Check it
@@ -233,12 +243,16 @@ 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;
+               if (is_null($stack)) {
+                       // Use current group
+                       //* DEBUG: */ print __METHOD__.' currGroup=' . $this->currGroup . ' set as stack!<br />' . chr(10);
+                       $stack = $this->currGroup;
+               } // END - if
 
                // Get variable index
                $found = $this->getVariableIndex($var, $stack);
@@ -250,7 +264,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                } // END - if
 
                // Return the current position
-               //* DEBUG: */ echo __METHOD__.": group=".$stack.",var=".$var.", content[".gettype($content)."]=".$content."<br />\n";
+               //* DEBUG: */ print __METHOD__.': group='.$stack.',var='.$var.', content['.gettype($content).']='.$content.'<br />' . chr(10);
                return $content;
        }
 
@@ -272,7 +286,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        /**
         * Returns all variables of current group or empty array
         *
-        * @return      $result         Wether array of found variables or empty array
+        * @return      $result         Whether array of found variables or empty array
         */
        private function readCurrentGroup () {
                // Default is not found
@@ -292,12 +306,12 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * Settter for variable group
         *
         * @param       $groupName      Name of variable group
-        * @param       $add            Wether add this group
+        * @param       $add            Whether add this group
         * @return      void
         */
        public function setVariableGroup ($groupName, $add = true) {
                // Set group name
-               //* DEBIG: */ echo __METHOD__.": currGroup=".$groupName."<br />\n";
+               //* DEBIG: */ print __METHOD__.": currGroup=".$groupName."<br />\n";
                $this->currGroup = $groupName;
 
                // Skip group 'general'
@@ -315,7 +329,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @return      void
         */
        public function addGroupVariable ($var, $value) {
-               //* DEBUG: */ echo __METHOD__.": group=".$this->currGroup.", var=".$var.", value=".$value."<br />\n";
+               //* DEBUG: */ print __METHOD__.": group=".$this->currGroup.", var=".$var.", value=".$value."<br />\n";
 
                // Get current variables in group
                $currVars = $this->readCurrentGroup();
@@ -619,8 +633,8 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         */
        protected final function setRawTemplateData ($rawTemplateData) {
                // And store it in this class
-               //* DEBUG: */ echo __METHOD__.':'.$this->getUniqueId().': '.strlen($rawTemplateData).' Bytes set.<br />\n';
-               //* DEBUG: */ echo $this->currGroup.' variables: '.count($this->getVarStack($this->currGroup)).', groups='.count($this->varStack).'<br />\n';
+               //* DEBUG: */ print __METHOD__.': '.strlen($rawTemplateData).' Bytes set.' . chr(10);
+               //* DEBUG: */ print $this->currGroup.' variables: '.count($this->getVarStack($this->currGroup)).', groups='.count($this->varStack).'' . chr(10);
                $this->rawTemplateData = (string) $rawTemplateData;
        }
 
@@ -630,7 +644,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @return      $rawTemplateData        The raw data from the template
         */
        public final function getRawTemplateData () {
-               //* DEBUG: */ echo __METHOD__.':'.$this->getUniqueId().': '.strlen($this->rawTemplateData).' Bytes read.<br />\n';
+               //* DEBUG: */ print __METHOD__.': '.strlen($this->rawTemplateData).' Bytes read.' . chr(10);
                return $this->rawTemplateData;
        }
 
@@ -641,7 +655,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         */
        private final function setCompiledData ($compiledData) {
                // And store it in this class
-               //* DEBUG: */ echo __METHOD__.':'.$this->getUniqueId().': '.strlen($compiledData).' Bytes set.<br />\n';
+               //* DEBUG: */ print __METHOD__.': '.strlen($compiledData).' Bytes set.' . chr(10);
                $this->compiledData = (string) $compiledData;
        }
 
@@ -651,7 +665,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @return      $compiledData   Compiled template data
         */
        public final function getCompiledData () {
-               //* DEBUG: */ echo __METHOD__.':'.$this->getUniqueId().': '.strlen($this->compiledData).' Bytes read.<br />\n';
+               //* DEBUG: */ print __METHOD__.': '.strlen($this->compiledData).' Bytes read.' . chr(10);
                return $this->compiledData;
        }
 
@@ -728,7 +742,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                $ioInstance = $this->getFileIoInstance();
 
                // Some debug code to look on the file which is being loaded
-               //* DEBUG: */ echo __METHOD__.": FQFN=".$fqfn."<br />\n";
+               //* DEBUG: */ print __METHOD__.": FQFN=".$fqfn."<br />\n";
 
                // Load the raw template
                $rawTemplateData = $ioInstance->loadFileContents($fqfn);
@@ -807,7 +821,11 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                $backup = $this->getRawTemplateData();
 
                // Initialize some arrays
-               if (is_null($this->loadedRawData)) { $this->loadedRawData = array(); $this->rawTemplates = array(); }
+               if (is_null($this->loadedRawData)) {
+                       // Initialize both
+                       $this->loadedRawData = array();
+                       $this->rawTemplates = array();
+               } // END - if
 
                // Load all requested templates
                foreach ($templateMatches[1] as $template) {
@@ -817,16 +835,16 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                        if ((!isset($this->loadedRawData[$template])) && (!in_array($template, $this->loadedTemplates))) {
 
                                // Template not found, but maybe variable assigned?
-                               //* DEBUG: */ echo __METHOD__.":template={$template}<br />\n";
-                               if ($this->getVariableIndex($template) !== false) {
+                               //* DEBUG: */ print __METHOD__.":template={$template}<br />\n";
+                               if ($this->getVariableIndex($template, 'config') !== false) {
                                        // Use that content here
-                                       $this->loadedRawData[$template] = $this->readVariable($template);
+                                       $this->loadedRawData[$template] = $this->readVariable($template, 'config');
 
                                        // Recursive protection:
                                        $this->loadedTemplates[] = $template;
-                               } elseif ($this->getVariableIndex($template, 'config')) {
+                               } elseif ($this->getVariableIndex($template) !== false) {
                                        // Use that content here
-                                       $this->loadedRawData[$template] = $this->readVariable($template, 'config');
+                                       $this->loadedRawData[$template] = $this->readVariable($template);
 
                                        // Recursive protection:
                                        $this->loadedTemplates[] = $template;
@@ -993,7 +1011,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                                if (in_array($template, $this->compiledTemplates)) {
                                        // Then skip it
                                        continue;
-                               }
+                               } // END - if
 
                                // Search for the template
                                $foundIndex = array_search($template, $templateMatches[1]);
@@ -1041,7 +1059,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        private function finalizeVariableCompilation () {
                // Get the content
                $content = $this->getRawTemplateData();
-               //* DEBUG: */ echo __METHOD__.': content before='.strlen($content).' ('.md5($content).')<br />\n';
+               //* DEBUG: */ print __METHOD__.': content before='.strlen($content).' ('.md5($content).')' . chr(10);
 
                // Do we have the stack?
                if (!$this->isVarStackSet('general')) {
@@ -1052,7 +1070,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
 
                // Walk through all variables
                foreach ($this->getVarStack('general') as $currEntry) {
-                       //* DEBUG: */ echo __METHOD__.': name='.$currEntry['name'].', value=<pre>'.htmlentities($currEntry['value']).'</pre>\n';
+                       //* DEBUG: */ print __METHOD__.': name='.$currEntry['name'].', value=<pre>'.htmlentities($currEntry['value']).'</pre>\n';
                        // Replace all [$var] or {?$var?} with the content
                        // @TODO Old behaviour, will become obsolete!
                        $content = str_replace('$content[' . $currEntry['name'] . ']', $currEntry['value'], $content);
@@ -1064,7 +1082,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                        $content = str_replace('{?' . $currEntry['name'] . '?}', $currEntry['value'], $content);
                } // END - for
 
-               //* DEBUG: */ echo __METHOD__.': content after='.strlen($content).' ('.md5($content).')<br />\n';
+               //* DEBUG: */ print __METHOD__.': content after='.strlen($content).' ('.md5($content).')' . chr(10);
 
                // Set the content back
                $this->setRawTemplateData($content);
@@ -1093,7 +1111,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         */
        public function assignConfigVariable ($var) {
                // Sweet and simple...
-               //* DEBUG: */ echo __METHOD__.':var={$var}<br />\n';
+               //* DEBUG: */ print __METHOD__.': var=' . $var . ',getConfigEntry()=' . $this->getConfigInstance()->getConfigEntry($var) . chr(10);
                $this->setVariable('config', $var, $this->getConfigInstance()->getConfigEntry($var));
        }
 
@@ -1128,7 +1146,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                } // END - if
 
                // Iterate through all general variables
-               foreach ($this->getVarStack('general') as $index=>$currVariable) {
+               foreach ($this->getVarStack('general') as $index => $currVariable) {
                        // Compile the value
                        $value = $this->compileRawCode($this->readVariable($currVariable['name']), true);
 
@@ -1136,6 +1154,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                        $this->removeVariable($index, 'general');
 
                        // Re-assign the variable
+                       //* DEBUG: */ print __METHOD__.': value='. $value . ',name=' . $currVariable['name'] . ',index=' . $index . chr(10);
                        $this->assignConfigVariable($value);
                } // END - foreach
        }
@@ -1162,7 +1181,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
@@ -1245,8 +1264,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
@@ -1303,7 +1325,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                // Is this view helper loaded?
                if (!isset($this->helpers[$helperName])) {
                        // Create a class name
-                       $className = "{$helperName}ViewHelper";
+                       $className = $helperName . 'ViewHelper';
 
                        // Generate new instance
                        $this->helpers[$helperName] = ObjectFactory::createObjectByName($className);
@@ -1345,21 +1367,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);
        }
 
        /**
@@ -1371,11 +1393,11 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         */
        public function compileRawCode ($rawCode, $setMatchAsCode=false) {
                // Find the variables
-               //* DEBUG: */ echo __METHOD__.":rawCode=<pre>".htmlentities($rawCode)."</pre>\n";
+               //* DEBUG: */ print __METHOD__.":rawCode=<pre>".htmlentities($rawCode)."</pre>\n";
                preg_match_all($this->regExpVarValue, $rawCode, $varMatches);
 
                // Compile all variables
-               //* DEBUG: */ echo __METHOD__.":<pre>".print_r($varMatches, true)."</pre>\n";
+               //* DEBUG: */ print __METHOD__.":<pre>".print_r($varMatches, true)."</pre>\n";
                foreach ($varMatches[0] as $match) {
                        // Add variable tags around it
                        $varCode = '{?' . $match . '?}';
@@ -1384,7 +1406,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                        if (strpos($rawCode, $varCode) !== false) {
                                // Replace the variable with it's value, if found
                                $value = $this->readVariable($match);
-                               //* DEBUG: */ echo __METHOD__.": match=".$match.",value[".gettype($value)."]=".$value."<br />\n";
+                               //* DEBUG: */ print __METHOD__.": match=".$match.",value[".gettype($value)."]=".$value."<br />\n";
                                if (($setMatchAsCode === true) && (is_null($value))) {
                                        // Insert match
                                        $rawCode = str_replace($varCode, $match, $rawCode);
@@ -1396,7 +1418,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
                } // END - foreach
 
                // Return the compiled data
-               //* DEBUG: */ echo __METHOD__.":rawCode=<pre>".htmlentities($rawCode)."</pre>\n";
+               //* DEBUG: */ print __METHOD__.":rawCode=<pre>".htmlentities($rawCode)."</pre>\n";
                return $rawCode;
        }
 
@@ -1417,7 +1439,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
         * @return      void
         */
        public function renameVariable ($oldName, $newName) {
-               //* DEBUG: */ echo __METHOD__.": oldName={$oldName}, newName={$newName}<br />\n";
+               //* DEBUG: */ print __METHOD__.": oldName={$oldName}, newName={$newName}<br />\n";
                // Get raw template code
                $rawData = $this->getRawTemplateData();
 
@@ -1435,7 +1457,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
@@ -1466,9 +1488,9 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        }
 
        /**
-        * Checks wether language support is enabled
+        * Checks whether language support is enabled
         *
-        * @return      $languageSupport        Wether language support is enabled or disabled
+        * @return      $languageSupport        Whether language support is enabled or disabled
         */
        public final function isLanguageSupportEnabled () {
                return $this->languageSupport;
@@ -1485,9 +1507,9 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        }
 
        /**
-        * Checks wether XML compacting is enabled
+        * Checks whether XML compacting is enabled
         *
-        * @return      $xmlCompacting  Wether XML compacting is enabled or disabled
+        * @return      $xmlCompacting  Whether XML compacting is enabled or disabled
         */
        public final function isXmlCompactingEnabled () {
                return $this->xmlCompacting;
@@ -1496,27 +1518,30 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
        /**
         * Removes all commentd, tabs and new-line characters to compact the content
         *
-        * @param       $content        The uncompacted content
-        * @return      $content        The compacted content
+        * @param       $uncompactedContent             The uncompacted content
+        * @return      $compactedContent               The compacted content
         */
-       public function compactContent ($content) {
+       public function compactContent ($uncompactedContent) {
                // First, remove all tab/new-line/revert characters
-               $content = str_replace("\t", '', str_replace("\n", '', str_replace("\r", '', $content)));
+               $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}-->/', $content, $matches);
+               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
-                               $content = str_replace($match, '', $content);
+                               $compactedContent = str_replace($match, '', $compactedContent);
                        } // END - foreach
                } // END - if
 
+               // Set the content again
+               $this->setRawTemplateData($compactedContent);
+
                // Return compacted content
-               return $content;
+               return $compactedContent;
        }
 }