]> git.mxchange.org Git - core.git/blob - framework/main/classes/template/class_BaseTemplateEngine.php
Rewrite continued:
[core.git] / framework / main / classes / template / class_BaseTemplateEngine.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Template\Engine;
4
5 // Import framework stuff
6 use CoreFramework\Bootstrap\FrameworkBootstrap;
7 use CoreFramework\Factory\ObjectFactory;
8 use CoreFramework\Generic\EmptyVariableException;
9 use CoreFramework\Manager\ManageableApplication;
10 use CoreFramework\Object\BaseFrameworkSystem;
11 use CoreFramework\Response\Responseable;
12
13 /**
14  * A generic template engine
15  *
16  * @author              Roland Haeder <webmaster@shipsimu.org>
17  * @version             0.0.0
18  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
19  * @license             GNU GPL 3.0 or any newer version
20  * @link                http://www.shipsimu.org
21  *
22  * This program is free software: you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation, either version 3 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  * GNU General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program. If not, see <http://www.gnu.org/licenses/>.
34  */
35 class BaseTemplateEngine extends BaseFrameworkSystem {
36         /**
37          * The local path name where all templates and sub folders for special
38          * templates are stored. We will internally determine the language plus
39          * "html" for web templates or "emails" for email templates
40          */
41         private $templateBasePath = '';
42
43         /**
44          * Template type
45          */
46         private $templateType = 'html';
47
48         /**
49          * The extension for web and email templates (not compiled templates)
50          */
51         private $templateExtension = '.tpl';
52
53         /**
54          * The extension for code templates (not compiled templates)
55          */
56         private $codeExtension = '.ctp';
57
58         /**
59          * Path relative to $templateBasePath and language code for compiled code-templates
60          */
61         private $compileOutputPath = 'templates/_compiled/';
62
63         /**
64          * The path name for all templates
65          */
66         private $genericBasePath = 'templates/';
67
68         /**
69          * The raw (maybe uncompiled) template
70          */
71         private $rawTemplateData = '';
72
73         /**
74          * Template data with compiled-in variables
75          */
76         private $compiledData = '';
77
78         /**
79          * The last loaded template's FQFN for debugging the engine
80          */
81         private $lastTemplate = '';
82
83         /**
84          * The variable stack for the templates
85          */
86         private $varStack = array();
87
88         /**
89          * Loaded templates for recursive protection and detection
90          */
91         private $loadedTemplates = array();
92
93         /**
94          * Compiled templates for recursive protection and detection
95          */
96         private $compiledTemplates = array();
97
98         /**
99          * Loaded raw template data
100          */
101         private $loadedRawData = NULL;
102
103         /**
104          * Raw templates which are linked in code templates
105          */
106         private $rawTemplates = NULL;
107
108         /**
109          * A regular expression for variable=value pairs
110          */
111         private $regExpVarValue = '/([\w_]+)(="([^"]*)"|=([\w_]+))?/';
112
113         /**
114          * A regular expression for filtering out code tags
115          *
116          * E.g.: {?template:variable=value;var2=value2;[...]?}
117          */
118         private $regExpCodeTags = '/\{\?([a-z_]+)(:("[^"]+"|[^?}]+)+)?\?\}/';
119
120         /**
121          * A regular expression to find template comments like <!-- Comment here //-->
122          */
123         private $regExpComments = '/<!--[\w\W]*?(\/\/){0,1}-->/';
124
125         /**
126          * Loaded helpers
127          */
128         private $helpers = array();
129
130         /**
131          * Current variable group
132          */
133         private $currGroup = 'general';
134
135         /**
136          * All template groups except "general"
137          */
138         private $variableGroups = array();
139
140         /**
141          * Code begin
142          */
143         private $codeBegin = '<?php';
144
145         /**
146          * Code end
147          */
148         private $codeEnd = '?>';
149
150         /**
151          * Language support is enabled by default
152          */
153         private $languageSupport = true;
154
155         /**
156          * XML compacting is disabled by default
157          */
158         private $xmlCompacting = false;
159
160         // Exception codes for the template engine
161         const EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED   = 0x110;
162         const EXCEPTION_TEMPLATE_CONTAINS_INVALID_VAR = 0x111;
163         const EXCEPTION_INVALID_VIEW_HELPER           = 0x112;
164         const EXCEPTION_VARIABLE_IS_MISSING           = 0x113;
165
166         /**
167          * Protected constructor
168          *
169          * @param       $className      Name of the class
170          * @return      void
171          */
172         protected function __construct ($className) {
173                 // Call parent constructor
174                 parent::__construct($className);
175
176                 // Init file I/O instance
177                 $ioInstance = ObjectFactory::createObjectByConfiguredName('file_io_class');
178
179                 // Set it
180                 $this->setFileIoInstance($ioInstance);
181         }
182
183         /**
184          * Search for a variable in the stack
185          *
186          * @param       $variableName   The variable we are looking for
187          * @param       $variableGroup  Optional variable group to look in
188          * @return      $index                  false means not found, >=0 means found on a specific index
189          */
190         private function getVariableIndex ($variableName, $variableGroup = NULL) {
191                 // Replace all dashes to underscores to match variables with configuration entries
192                 $variableName = trim(self::convertDashesToUnderscores($variableName));
193
194                 // First everything is not found
195                 $found = false;
196
197                 // If the stack is NULL, use the current group
198                 if (is_null($variableGroup)) {
199                         // Use current group
200                         //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(__METHOD__.' currGroup=' . $this->currGroup . ' set as stack!');
201                         $variableGroup = $this->currGroup;
202                 } // END - if
203
204                 // Is the group there?
205                 if ($this->isVarStackSet($variableGroup)) {
206                         // Now search for it
207                         foreach ($this->getVarStack($variableGroup) as $index => $currEntry) {
208                                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(__METHOD__.':currGroup=' . $variableGroup . ',idx=' . $index . ',currEntry=' . $currEntry['name'] . ',variableName=' . $variableName);
209                                 // Is the entry found?
210                                 if ($currEntry['name'] == $variableName) {
211                                         // Found!
212                                         //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(__METHOD__.':FOUND!');
213                                         $found = $index;
214                                         break;
215                                 } // END - if
216                         } // END - foreach
217                 } // END - if
218
219                 // Return the current position
220                 return $found;
221         }
222
223         /**
224          * Checks whether the given variable group is set
225          *
226          * @param       $variableGroup  Variable group to check
227          * @return      $isSet                  Whether the given variable group is set
228          */
229         protected final function isVarStackSet ($variableGroup) {
230                 // Check it
231                 $isSet = isset($this->varStack[$variableGroup]);
232
233                 // Return result
234                 return $isSet;
235         }
236
237         /**
238          * Getter for given variable group
239          *
240          * @param       $variableGroup  Variable group to check
241          * @return      $varStack               Found variable group
242          */
243         public final function getVarStack ($variableGroup) {
244                 return $this->varStack[$variableGroup];
245         }
246
247         /**
248          * Setter for given variable group
249          *
250          * @param       $variableGroup  Variable group to check
251          * @param       $varStack               Variable stack to check
252          * @return      void
253          */
254         protected final function setVarStack ($variableGroup, array $varStack) {
255                 $this->varStack[$variableGroup]  = $varStack;
256         }
257
258         /**
259          * Return a content of a variable or null if not found
260          *
261          * @param       $variableName   The variable we are looking for
262          * @param       $variableGroup  Optional variable group to look in
263          * @return      $content                Content of the variable or null if not found
264          */
265         protected function readVariable ($variableName, $variableGroup = NULL) {
266                 // Replace all dashes to underscores to match variables with configuration entries
267                 $variableName = trim(self::convertDashesToUnderscores($variableName));
268
269                 // First everything is not found
270                 $content = NULL;
271
272                 // If the stack is NULL, use the current group
273                 if (is_null($variableGroup)) {
274                         // Use current group
275                         //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(__METHOD__.' currGroup=' . $this->currGroup . ' set as stack!');
276                         $variableGroup = $this->currGroup;
277                 } // END - if
278
279                 // Get variable index
280                 $found = $this->getVariableIndex($variableName, $variableGroup);
281
282                 // Is the variable found?
283                 if ($found !== false) {
284                         // Read it
285                         $content = $this->getVariableValue($variableGroup, $found);
286                 } // END - if
287
288                 // Return the current position
289                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(__METHOD__.': variableGroup=' . $variableGroup . ',variableName=' . $variableName . ', content[' . gettype($content) . ']=' . $content);
290                 return $content;
291         }
292
293         /**
294          * Add a variable to the stack
295          *
296          * @param       $variableName   Name of variable to add
297          * @param       $value                  Value we want to store in the variable
298          * @return      void
299          */
300         private function addVariable ($variableName, $value) {
301                 // Set general variable group
302                 $this->setVariableGroup('general');
303
304                 // Add it to the stack
305                 $this->addGroupVariable($variableName, $value);
306         }
307
308         /**
309          * Returns all variables of current group or empty array
310          *
311          * @return      $result         Whether array of found variables or empty array
312          */
313         private function readCurrentGroup () {
314                 // Default is not found
315                 $result = array();
316
317                 // Is the group there?
318                 if ($this->isVarStackSet($this->currGroup)) {
319                         // Then use it
320                         $result = $this->getVarStack($this->currGroup);
321                 } // END - if
322
323                 // Return result
324                 return $result;
325         }
326
327         /**
328          * Settter for variable group
329          *
330          * @param       $groupName      Name of variable group
331          * @param       $add            Whether add this group
332          * @return      void
333          */
334         public function setVariableGroup ($groupName, $add = true) {
335                 // Set group name
336                 //* DEBIG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(__METHOD__.': currGroup=' . $groupName);
337                 $this->currGroup = $groupName;
338
339                 // Skip group 'general'
340                 if (($groupName != 'general') && ($add === true)) {
341                         $this->variableGroups[$groupName] = 'OK';
342                 } // END - if
343         }
344
345
346         /**
347          * Adds a variable to current group
348          *
349          * @param       $variableName   Variable to set
350          * @param       $value                  Value to store in variable
351          * @return      void
352          */
353         public function addGroupVariable ($variableName, $value) {
354                 // Replace all dashes to underscores to match variables with configuration entries
355                 $variableName = trim(self::convertDashesToUnderscores($variableName));
356
357                 // Debug message
358                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(__METHOD__.': group=' . $this->currGroup . ', variableName=' . $variableName . ', value=' . $value);
359
360                 // Get current variables in group
361                 $currVars = $this->readCurrentGroup();
362
363                 // Append our variable
364                 array_push($currVars, $this->generateVariableArray($variableName, $value));
365
366                 // Add it to the stack
367                 $this->setVarStack($this->currGroup, $currVars);
368         }
369
370         /**
371          * Getter for variable value, throws a NoVariableException if the variable is not found
372          *
373          * @param       $variableGroup  Variable group to use
374          * @param       $index          Index in variable array
375          * @return      $value          Value to set
376          */
377         private function getVariableValue ($variableGroup, $index) {
378                 // Return it
379                 return $this->varStack[$variableGroup][$index]['value'];
380         }
381
382         /**
383          * Modify an entry on the stack
384          *
385          * @param       $variableName   The variable we are looking for
386          * @param       $value                  The value we want to store in the variable
387          * @return      void
388          * @throws      NoVariableException     If the given variable is not found
389          */
390         private function modifyVariable ($variableName, $value) {
391                 // Replace all dashes to underscores to match variables with configuration entries
392                 $variableName = trim(self::convertDashesToUnderscores($variableName));
393
394                 // Get index for variable
395                 $index = $this->getVariableIndex($variableName);
396
397                 // Is the variable set?
398                 if ($index === false) {
399                         // Unset variables cannot be modified
400                         throw new NoVariableException(array($this, $variableName, $value), self::EXCEPTION_VARIABLE_IS_MISSING);
401                 } // END - if
402
403                 // Then modify it
404                 $this->setVariableValue($this->currGroup, $index, $value);
405         }
406
407         /**
408          * Sets a variable value for given variable group and index
409          *
410          * @param       $variableGroup  Variable group to use
411          * @param       $index          Index in variable array
412          * @param       $value          Value to set
413          * @return      void
414          */
415         private function setVariableValue ($variableGroup, $index, $value) {
416                 $this->varStack[$variableGroup][$index]['value'] = $value;
417         }
418
419         /**
420          * Sets a variable within given group. This method does detect if the
421          * variable is already set. If so, the variable got modified, otherwise
422          * added.
423          *
424          * @param       $variableGroup          Variable group to use
425          * @param       $variableName   Variable to set
426          * @param       $value                  Value to set
427          * @return      void
428          */
429         protected function setVariable ($variableGroup, $variableName, $value) {
430                 // Replace all dashes to underscores to match variables with configuration entries
431                 $variableName = trim(self::convertDashesToUnderscores($variableName));
432
433                 // Get index for variable
434                 $index = $this->getVariableIndex($variableName);
435
436                 // Is the variable set?
437                 if ($index === false) {
438                         // Is the stack there?
439                         if (!isset($this->varStack[$variableGroup])) {
440                                 // Then initialize it here
441                                 $this->varStack[$variableGroup] = array();
442                         } // END - if
443
444                         // Not found, add it
445                         array_push($this->varStack[$variableGroup], $this->generateVariableArray($variableName, $value));
446                 } else {
447                         // Then modify it
448                         $this->setVariableValue($this->currGroup, $index, $value);
449                 }
450         }
451
452         /**
453          * "Generates" (better returns) an array for all variables for given
454          * variable/value pay.
455          *
456          * @param       $variableName   Variable to set
457          * @param       $value                  Value to set
458          * @return      $varData                Variable data array
459          */
460         private function generateVariableArray ($variableName, $value) {
461                 // Replace all dashes to underscores to match variables with configuration entries
462                 $variableName = trim(self::convertDashesToUnderscores($variableName));
463
464                 // Generate the temporary array
465                 $varData = array(
466                         'name'  => $variableName,
467                         'value' => $value
468                 );
469
470                 // And return it
471                 return $varData;
472         }
473
474         /**
475          * Setter for template type. Only 'html', 'emails' and 'compiled' should
476          * be sent here
477          *
478          * @param       $templateType   The current template's type
479          * @return      void
480          */
481         protected final function setTemplateType ($templateType) {
482                 $this->templateType = (string) $templateType;
483         }
484
485         /**
486          * Getter for template type
487          *
488          * @return      $templateType   The current template's type
489          */
490         public final function getTemplateType () {
491                 return $this->templateType;
492         }
493
494         /**
495          * Setter for the last loaded template's FQFN
496          *
497          * @param       $template       The last loaded template
498          * @return      void
499          */
500         private final function setLastTemplate ($template) {
501                 $this->lastTemplate = (string) $template;
502         }
503
504         /**
505          * Getter for the last loaded template's FQFN
506          *
507          * @return      $template       The last loaded template
508          */
509         private final function getLastTemplate () {
510                 return $this->lastTemplate;
511         }
512
513         /**
514          * Setter for base path
515          *
516          * @param               $templateBasePath               The relative base path for all templates
517          * @return      void
518          */
519         protected final function setTemplateBasePath ($templateBasePath) {
520                 // And set it
521                 $this->templateBasePath = (string) $templateBasePath;
522         }
523
524         /**
525          * Getter for base path
526          *
527          * @return      $templateBasePath               The relative base path for all templates
528          */
529         public final function getTemplateBasePath () {
530                 // And set it
531                 return $this->templateBasePath;
532         }
533
534         /**
535          * Getter for generic base path
536          *
537          * @return      $templateBasePath               The relative base path for all templates
538          */
539         public final function getGenericBasePath () {
540                 // And set it
541                 return $this->genericBasePath;
542         }
543
544         /**
545          * Setter for template extension
546          *
547          * @param               $templateExtension      The file extension for all uncompiled
548          *                                                      templates
549          * @return      void
550          */
551         protected final function setRawTemplateExtension ($templateExtension) {
552                 // And set it
553                 $this->templateExtension = (string) $templateExtension;
554         }
555
556         /**
557          * Setter for code template extension
558          *
559          * @param               $codeExtension          The file extension for all uncompiled
560          *                                                      templates
561          * @return      void
562          */
563         protected final function setCodeTemplateExtension ($codeExtension) {
564                 // And set it
565                 $this->codeExtension = (string) $codeExtension;
566         }
567
568         /**
569          * Getter for template extension
570          *
571          * @return      $templateExtension      The file extension for all uncompiled
572          *                                                      templates
573          */
574         public final function getRawTemplateExtension () {
575                 // And set it
576                 return $this->templateExtension;
577         }
578
579         /**
580          * Getter for code-template extension
581          *
582          * @return      $codeExtension          The file extension for all code-
583          *                                                      templates
584          */
585         public final function getCodeTemplateExtension () {
586                 // And set it
587                 return $this->codeExtension;
588         }
589
590         /**
591          * Setter for path of compiled templates
592          *
593          * @param       $compileOutputPath      The local base path for all compiled
594          *                                                              templates
595          * @return      void
596          */
597         protected final function setCompileOutputPath ($compileOutputPath) {
598                 // And set it
599                 $this->compileOutputPath = (string) $compileOutputPath;
600         }
601
602         /**
603          * Unsets the given offset in the variable group
604          *
605          * @param       $index                  Index to unset
606          * @param       $variableGroup  Variable group (default: currGroup)
607          * @return      void
608          */
609         protected final function unsetVariableStackOffset ($index, $variableGroup = NULL) {
610                 // Is the variable group not set?
611                 if (is_null($variableGroup)) {
612                         // Then set it to current
613                         $variableGroup = $this->currGroup;
614                 } // END - if
615
616                 // Is the entry there?
617                 if (!isset($this->varStack[$variableGroup][$index])) {
618                         // Abort here, we need fixing!
619                         $this->debugInstance();
620                 } // END - if
621
622                 // Remove it
623                 unset($this->varStack[$variableGroup][$index]);
624         }
625
626         /**
627          * Private setter for raw template data
628          *
629          * @param       $rawTemplateData        The raw data from the template
630          * @return      void
631          */
632         protected final function setRawTemplateData ($rawTemplateData) {
633                 // And store it in this class
634                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(__METHOD__.': ' . strlen($rawTemplateData) . ' Bytes set.');
635                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(__METHOD__.': ' . $this->currGroup . ' variables: ' . count($this->getVarStack($this->currGroup)) . ', groups=' . count($this->varStack));
636                 $this->rawTemplateData = (string) $rawTemplateData;
637         }
638
639         /**
640          * Getter for raw template data
641          *
642          * @return      $rawTemplateData        The raw data from the template
643          */
644         public final function getRawTemplateData () {
645                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: ' . strlen($this->rawTemplateData) . ' Bytes read.');
646                 return $this->rawTemplateData;
647         }
648
649         /**
650          * Private setter for compiled templates
651          *
652          * @return      void
653          */
654         private final function setCompiledData ($compiledData) {
655                 // And store it in this class
656                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: ' . strlen($compiledData) . ' Bytes set.');
657                 $this->compiledData = (string) $compiledData;
658         }
659
660         /**
661          * Getter for compiled templates, must be public for e.g. Mailer classes.
662          *
663          * @return      $compiledData   Compiled template data
664          */
665         public final function getCompiledData () {
666                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: ' . strlen($this->compiledData) . ' Bytes read.');
667                 return $this->compiledData;
668         }
669
670         /**
671          * Private loader for all template types
672          *
673          * @param       $template       The template we shall load
674          * @param       $extOther       An other extension to use
675          * @return      void
676          * @throws      FileNotFoundException   If the template was not found
677          */
678         protected function loadTemplate ($template, $extOther = '') {
679                 // Get extension for the template if empty
680                 if (empty($extOther)) {
681                         // None provided, so get the raw one
682                         $ext = $this->getRawTemplateExtension();
683                 } else {
684                         // Then use it!
685                         $ext = (string) $extOther;
686                 }
687
688                 /*
689                  * Construct the FQFN for the template without language as language is
690                  * now entirely done by php_intl. These old thing with language-based
691                  * template paths comes from an older time.
692                  */
693                 $fqfn = sprintf('%s%s%s%s/%s%s',
694                         $this->getConfigInstance()->getConfigEntry('application_base_path'),
695                         $this->getTemplateBasePath(),
696                         $this->getGenericBasePath(),
697                         $this->getTemplateType(),
698                         (string) $template,
699                         $ext
700                 );
701
702                 // First try this
703                 try {
704                         // Load the raw template data
705                         $this->loadRawTemplateData($fqfn);
706                 } catch (FileNotFoundException $e) {
707                         // If we shall load a code-template we need to switch the file extension
708                         if (($this->getTemplateType() != $this->getConfigInstance()->getConfigEntry('html_template_type')) && (empty($extOther))) {
709                                 // Switch over to the code-template extension and try it again
710                                 $ext = $this->getCodeTemplateExtension();
711
712                                 // Try it again...
713                                 $this->loadTemplate($template, $ext);
714                         } else {
715                                 // Throw it again
716                                 throw new FileNotFoundException($fqfn, self::EXCEPTION_FILE_NOT_FOUND);
717                         }
718                 }
719
720         }
721
722         /**
723          * A private loader for raw template names
724          *
725          * @param       $fqfn   The full-qualified file name for a template
726          * @return      void
727          */
728         private function loadRawTemplateData ($fqfn) {
729                 // Some debug code to look on the file which is being loaded
730                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: FQFN=' . $fqfn);
731
732                 // Load the raw template
733                 $rawTemplateData = $this->getFileIoInstance()->loadFileContents($fqfn);
734
735                 // Store the template's contents into this class
736                 $this->setRawTemplateData($rawTemplateData);
737
738                 // Remember the template's FQFN
739                 $this->setLastTemplate($fqfn);
740         }
741
742         /**
743          * Try to assign an extracted template variable as a "content" or 'config'
744          * variable.
745          *
746          * @param       $variableName           The variable's name (shall be content or config)
747          *                                                      by default
748          * @param       $variableName   The variable we want to assign
749          * @return      void
750          */
751         private function assignTemplateVariable ($variableName, $var) {
752                 // Replace all dashes to underscores to match variables with configuration entries
753                 $variableName = trim(self::convertDashesToUnderscores($variableName));
754
755                 // Debug message
756                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: variableName=' . $variableName . ',variableName=' . $variableName);
757
758                 // Is it not a config variable?
759                 if ($variableName != 'config') {
760                         // Regular template variables
761                         $this->assignVariable($variableName, '');
762                 } else {
763                         // Configuration variables
764                         $this->assignConfigVariable($var);
765                 }
766         }
767
768         /**
769          * Extract variables from a given raw data stream
770          *
771          * @param       $rawData        The raw template data we shall analyze
772          * @return      void
773          */
774         private function extractVariablesFromRawData ($rawData) {
775                 // Cast to string
776                 $rawData = (string) $rawData;
777
778                 // Search for variables
779                 preg_match_all('/\$(\w+)(\[(\w+)\])?/', $rawData, $variableMatches);
780
781                 // Debug message
782                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:rawData(' . strlen($rawData) . ')=' . $rawData . ',variableMatches=' . print_r($variableMatches, true));
783
784                 // Did we find some variables?
785                 if ((is_array($variableMatches)) && (count($variableMatches) == 4) && (count($variableMatches[0]) > 0)) {
786                         // Initialize all missing variables
787                         foreach ($variableMatches[3] as $key => $var) {
788                                 // Variable name
789                                 $variableName = $variableMatches[1][$key];
790
791                                 // Workarround: Do not assign empty variables
792                                 if (!empty($var)) {
793                                         // Try to assign it, empty strings are being ignored
794                                         $this->assignTemplateVariable($variableName, $var);
795                                 } // END - if
796                         } // END - foreach
797                 } // END - if
798         }
799
800         /**
801          * Main analysis of the loaded template
802          *
803          * @param       $templateMatches        Found template place-holders, see below
804          * @return      void
805          *
806          *---------------------------------
807          * Structure of $templateMatches:
808          *---------------------------------
809          * [0] => Array - An array with all full matches
810          * [1] => Array - An array with left part (before the ':') of a match
811          * [2] => Array - An array with right part of a match including ':'
812          * [3] => Array - An array with right part of a match excluding ':'
813          */
814         private function analyzeTemplate (array $templateMatches) {
815                 // Backup raw template data
816                 $backup = $this->getRawTemplateData();
817
818                 // Initialize some arrays
819                 if (is_null($this->loadedRawData)) {
820                         // Initialize both
821                         $this->loadedRawData = array();
822                         $this->rawTemplates = array();
823                 } // END - if
824
825                 // Load all requested templates
826                 foreach ($templateMatches[1] as $template) {
827                         // Load and compile only templates which we have not yet loaded
828                         // RECURSIVE PROTECTION! BE CAREFUL HERE!
829                         if ((!isset($this->loadedRawData[$template])) && (!in_array($template, $this->loadedTemplates))) {
830                                 // Debug message
831                                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:template=' . $template);
832
833                                 // Template not found, but maybe variable assigned?
834                                 if ($this->getVariableIndex($template) !== false) {
835                                         // Use that content here
836                                         $this->loadedRawData[$template] = $this->readVariable($template);
837
838                                         // Recursive protection:
839                                         array_push($this->loadedTemplates, $template);
840                                 } else {
841                                         // Then try to search for code-templates
842                                         try {
843                                                 // Load the code template and remember it's contents
844                                                 $this->loadCodeTemplate($template);
845                                                 $this->loadedRawData[$template] = $this->getRawTemplateData();
846
847                                                 // Remember this template for recursion detection
848                                                 // RECURSIVE PROTECTION!
849                                                 array_push($this->loadedTemplates, $template);
850                                         } catch (FileNotFoundException $e) {
851                                                 // Even this is not done... :/
852                                                 array_push($this->rawTemplates, $template);
853                                         }
854                                 }
855                         } // END - if
856                 } // END - foreach
857
858                 // Restore the raw template data
859                 $this->setRawTemplateData($backup);
860         }
861
862         /**
863          * Compile a given raw template code and remember it for later usage
864          *
865          * @param       $code           The raw template code
866          * @param       $template       The template's name
867          * @return      void
868          */
869         private function compileCode ($code, $template) {
870                 // Is this template already compiled?
871                 if (in_array($template, $this->compiledTemplates)) {
872                         // Abort here...
873                         return;
874                 } // END - if
875
876                 // Remember this template being compiled
877                 array_push($this->compiledTemplates, $template);
878
879                 // Compile the loaded code in five steps:
880                 //
881                 // 1. Backup current template data
882                 $backup = $this->getRawTemplateData();
883
884                 // 2. Set the current template's raw data as the new content
885                 $this->setRawTemplateData($code);
886
887                 // 3. Compile the template data
888                 $this->compileTemplate();
889
890                 // 4. Remember it's contents
891                 $this->loadedRawData[$template] = $this->getRawTemplateData();
892
893                 // 5. Restore the previous raw content from backup variable
894                 $this->setRawTemplateData($backup);
895         }
896
897         /**
898          * Insert all given and loaded templates by running through all loaded
899          * codes and searching for their place-holder in the main template
900          *
901          * @param       $templateMatches        See method analyzeTemplate()
902          * @return      void
903          */
904         private function insertAllTemplates (array $templateMatches) {
905                 // Run through all loaded codes
906                 foreach ($this->loadedRawData as $template => $code) {
907
908                         // Search for the template
909                         $foundIndex = array_search($template, $templateMatches[1]);
910
911                         // Lookup the matching template replacement
912                         if (($foundIndex !== false) && (isset($templateMatches[0][$foundIndex]))) {
913
914                                 // Get the current raw template
915                                 $rawData = $this->getRawTemplateData();
916
917                                 // Replace the space holder with the template code
918                                 $rawData = str_replace($templateMatches[0][$foundIndex], $code, $rawData);
919
920                                 // Set the new raw data
921                                 $this->setRawTemplateData($rawData);
922                         } // END - if
923                 } // END - foreach
924         }
925
926         /**
927          * Load all extra raw templates
928          *
929          * @return      void
930          */
931         private function loadExtraRawTemplates () {
932                 // Are there some raw templates we need to load?
933                 if (count($this->rawTemplates) > 0) {
934                         // Try to load all raw templates
935                         foreach ($this->rawTemplates as $key => $template) {
936                                 try {
937                                         // Load the template
938                                         $this->loadHtmlTemplate($template);
939
940                                         // Remember it's contents
941                                         $this->rawTemplates[$template] = $this->getRawTemplateData();
942
943                                         // Remove it from the loader list
944                                         unset($this->rawTemplates[$key]);
945
946                                         // Remember this template for recursion detection
947                                         // RECURSIVE PROTECTION!
948                                         array_push($this->loadedTemplates, $template);
949                                 } catch (FileNotFoundException $e) {
950                                         // This template was never found. We silently ignore it
951                                         unset($this->rawTemplates[$key]);
952                                 }
953                         } // END - foreach
954                 } // END - if
955         }
956
957         /**
958          * Assign all found template variables
959          *
960          * @param       $varMatches             An array full of variable/value pairs.
961          * @return      void
962          * @todo        Unfinished work or don't die here.
963          */
964         private function assignAllVariables (array $varMatches) {
965                 // Debug message
966                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:varMatches()=' . count($varMatches));
967
968                 // Search for all variables
969                 foreach ($varMatches[1] as $key => $var) {
970                         // Replace all dashes to underscores to match variables with configuration entries
971                         $var = trim(self::convertDashesToUnderscores($var));
972
973                         // Debug message
974                         self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:key=' . $key . ',var=' . $var);
975
976                         // Detect leading equals
977                         if (substr($varMatches[2][$key], 0, 1) == '=') {
978                                 // Remove and cast it
979                                 $varMatches[2][$key] = (string) substr($varMatches[2][$key], 1);
980                         } // END - if
981
982                         // Do we have some quotes left and right side? Then it is free text
983                         if ((substr($varMatches[2][$key], 0, 1) == '"') && (substr($varMatches[2][$key], -1, 1) == '"')) {
984                                 // Free string detected! Which we can assign directly
985                                 $this->assignVariable($var, $varMatches[3][$key]);
986                         } elseif (!empty($varMatches[2][$key])) {
987                                 // @TODO Non-string found so we need some deeper analysis...
988                                 ApplicationEntryPoint::app_exit('Deeper analysis not yet implemented!');
989                         }
990                 } // END - foreach
991         }
992
993         /**
994          * Compiles all loaded raw templates
995          *
996          * @param       $templateMatches        See method analyzeTemplate() for details
997          * @return      void
998          */
999         private function compileRawTemplateData (array $templateMatches) {
1000                 // Debug message
1001                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:loadedRawData()= ' .count($this->loadedRawData));
1002
1003                 // Are some code-templates found which we need to compile?
1004                 if (count($this->loadedRawData) > 0) {
1005                         // Then compile all!
1006                         foreach ($this->loadedRawData as $template => $code) {
1007                                 // Debug message
1008                                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:template=' . $template . ',code(' . strlen($code) . ')=' . $code);
1009
1010                                 // Is this template already compiled?
1011                                 if (in_array($template, $this->compiledTemplates)) {
1012                                         // Then skip it
1013                                         //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: Template ' . $template . ' already compiled. SKIPPED!');
1014                                         continue;
1015                                 } // END - if
1016
1017                                 // Search for the template
1018                                 $foundIndex = array_search($template, $templateMatches[1]);
1019
1020                                 // Lookup the matching variable data
1021                                 if (($foundIndex !== false) && (isset($templateMatches[3][$foundIndex]))) {
1022                                         // Split it up with another reg. exp. into variable=value pairs
1023                                         preg_match_all($this->regExpVarValue, $templateMatches[3][$foundIndex], $varMatches);
1024                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:varMatches=' . print_r($varMatches, true));
1025
1026                                         // Assign all variables
1027                                         $this->assignAllVariables($varMatches);
1028                                 } // END - if (isset($templateMatches ...
1029
1030                                 // Compile the loaded template
1031                                 $this->compileCode($code, $template);
1032                         } // END - foreach ($this->loadedRawData ...
1033
1034                         // Insert all templates
1035                         $this->insertAllTemplates($templateMatches);
1036                 } // END - if (count($this->loadedRawData) ...
1037         }
1038
1039         /**
1040          * Inserts all raw templates into their respective variables
1041          *
1042          * @return      void
1043          */
1044         private function insertRawTemplates () {
1045                 // Load all templates
1046                 foreach ($this->rawTemplates as $template => $content) {
1047                         // Set the template as a variable with the content
1048                         $this->assignVariable($template, $content);
1049                 }
1050         }
1051
1052         /**
1053          * Finalizes the compilation of all template variables
1054          *
1055          * @return      void
1056          */
1057         private function finalizeVariableCompilation () {
1058                 // Get the content
1059                 $content = $this->getRawTemplateData();
1060                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: content before=' . strlen($content) . ' (' . md5($content) . ')');
1061
1062                 // Do we have the stack?
1063                 if (!$this->isVarStackSet('general')) {
1064                         // Abort here silently
1065                         // @TODO This silent abort should be logged, maybe.
1066                         return;
1067                 } // END - if
1068
1069                 // Walk through all variables
1070                 foreach ($this->getVarStack('general') as $currEntry) {
1071                         //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: name=' . $currEntry['name'] . ', value=<pre>' . htmlentities($currEntry['value']) . '</pre>');
1072                         // Replace all [$var] or {?$var?} with the content
1073                         // @TODO Old behaviour, will become obsolete!
1074                         $content = str_replace('$content[' . $currEntry['name'] . ']', $currEntry['value'], $content);
1075
1076                         // @TODO Yet another old way
1077                         $content = str_replace('[' . $currEntry['name'] . ']', $currEntry['value'], $content);
1078
1079                         // The new behaviour
1080                         $content = str_replace('{?' . $currEntry['name'] . '?}', $currEntry['value'], $content);
1081                 } // END - for
1082
1083                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: content after=' . strlen($content) . ' (' . md5($content) . ')');
1084
1085                 // Set the content back
1086                 $this->setRawTemplateData($content);
1087         }
1088
1089         /**
1090          * Load a specified HTML template into the engine
1091          *
1092          * @param       $template       The web template we shall load which is located in
1093          *                                              'html' by default
1094          * @return      void
1095          */
1096         public function loadHtmlTemplate ($template) {
1097                 // Set template type
1098                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('html_template_type'));
1099
1100                 // Load the special template
1101                 $this->loadTemplate($template);
1102         }
1103
1104         /**
1105          * Assign (add) a given variable with a value
1106          *
1107          * @param       $variableName   The variable we are looking for
1108          * @param       $value                  The value we want to store in the variable
1109          * @return      void
1110          * @throws      EmptyVariableException  If the variable name is left empty
1111          */
1112         public final function assignVariable ($variableName, $value) {
1113                 // Replace all dashes to underscores to match variables with configuration entries
1114                 $variableName = trim(self::convertDashesToUnderscores($variableName));
1115
1116                 // Empty variable found?
1117                 if (empty($variableName)) {
1118                         // Throw an exception
1119                         throw new EmptyVariableException(array($this, 'variableName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
1120                 } // END - if
1121
1122                 // First search for the variable if it was already added
1123                 $index = $this->getVariableIndex($variableName);
1124
1125                 // Was it found?
1126                 if ($index === false) {
1127                         // Add it to the stack
1128                         //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:ADD: ' . $variableName . '[' . gettype($value) . ']=' . $value);
1129                         $this->addVariable($variableName, $value);
1130                 } elseif (!empty($value)) {
1131                         // Modify the stack entry
1132                         //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:MOD: ' . $variableName . '[' . gettype($value) . ']=' . $value);
1133                         $this->modifyVariable($variableName, $value);
1134                 }
1135         }
1136
1137         /**
1138          * Removes a given variable
1139          *
1140          * @param       $variableName   The variable we are looking for
1141          * @param       $variableGroup  Name of variable group (default: 'general')
1142          * @return      void
1143          */
1144         public final function removeVariable ($variableName, $variableGroup = 'general') {
1145                 // First search for the variable if it was already added
1146                 $index = $this->getVariableIndex($variableName, $variableGroup);
1147
1148                 // Was it found?
1149                 if ($index !== false) {
1150                         // Remove this variable
1151                         //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:UNSET: variableGroup=' . $variableGroup . ',variableName=' . $variableName . ',index=' . $index);
1152                         $this->unsetVariableStackOffset($index, $variableGroup);
1153                 } // END - if
1154         }
1155
1156         /**
1157          * Assigns the last loaded raw template content with a given variable
1158          *
1159          * @param       $templateName   Name of the template we want to assign
1160          * @param       $variableName   Name of the variable we want to assign
1161          * @return      void
1162          */
1163         public function assignTemplateWithVariable ($templateName, $variableName) {
1164                 // Get the content from last loaded raw template
1165                 $content = $this->getRawTemplateData();
1166
1167                 // Assign the variable
1168                 $this->assignVariable($variableName, $content);
1169
1170                 // Purge raw content
1171                 $this->setRawTemplateData('');
1172         }
1173
1174         /**
1175          * Assign a given congfiguration variable with a value
1176          *
1177          * @param       $variableName   The configuration variable we want to assign
1178          * @return      void
1179          */
1180         public function assignConfigVariable ($variableName) {
1181                 // Replace all dashes to underscores to match variables with configuration entries
1182                 $variableName = trim(self::convertDashesToUnderscores($variableName));
1183
1184                 // Sweet and simple...
1185                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: variableName=' . $variableName . ',getConfigEntry()=' . $this->getConfigInstance()->getConfigEntry($variableName));
1186                 $this->assignVariable($variableName, $this->getConfigInstance()->getConfigEntry($variableName));
1187         }
1188
1189         /**
1190          * Assigns a lot variables into the stack of currently loaded template.
1191          * This method should only be used in very rare circumstances, e.g. when
1192          * you have to copy a whole set of variables into the template engine.
1193          * Before you use this method, please make sure you have considered all
1194          * other possiblities.
1195          *
1196          * @param       $variables      An array with variables to be assigned
1197          * @return      void
1198          */
1199         public function assignMultipleVariables (array $variables) {
1200                 // "Inject" all
1201                 foreach ($variables as $name => $value) {
1202                         // Set variable with name for 'config' group
1203                         $this->assignVariable($name, $value);
1204                 } // END - foreach
1205         }
1206
1207         /**
1208          * Assigns all the application data with template variables
1209          *
1210          * @param       $applicationInstance    A manageable application instance
1211          * @return      void
1212          */
1213         public function assignApplicationData (ManageableApplication $applicationInstance) {
1214                 // Get long name and assign it
1215                 $this->assignVariable('app_full_name' , $applicationInstance->getAppName());
1216
1217                 // Get short name and assign it
1218                 $this->assignVariable('app_short_name', $applicationInstance->getAppShortName());
1219
1220                 // Get version number and assign it
1221                 $this->assignVariable('app_version'   , $applicationInstance->getAppVersion());
1222
1223                 // Assign extra application-depending data
1224                 $applicationInstance->assignExtraTemplateData($this);
1225         }
1226
1227         /**
1228          * Load a specified code template into the engine
1229          *
1230          * @param       $template       The code template we shall load which is
1231          *                                              located in 'code' by default
1232          * @return      void
1233          */
1234         public function loadCodeTemplate ($template) {
1235                 // Set template type
1236                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('code_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_template_type'));
1237
1238                 // Load the special template
1239                 $this->loadTemplate($template);
1240         }
1241
1242         /**
1243          * Load a specified email template into the engine
1244          *
1245          * @param       $template       The email template we shall load which is
1246          *                                              located in 'emails' by default
1247          * @return      void
1248          */
1249         public function loadEmailTemplate ($template) {
1250                 // Set template type
1251                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('email_template_type'));
1252
1253                 // Load the special template
1254                 $this->loadTemplate($template);
1255         }
1256
1257         /**
1258          * Compiles configuration place-holders in all variables. This 'walks'
1259          * through the variable group 'general'. It interprets all values from that
1260          * variables as configuration entries after compiling them.
1261          *
1262          * @return      void
1263          */
1264         public final function compileConfigInVariables () {
1265                 // Do we have the stack?
1266                 if (!$this->isVarStackSet('general')) {
1267                         // Abort here silently
1268                         //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: Aborted, variable stack general not found!');
1269                         return;
1270                 } // END - if
1271
1272                 // Iterate through all general variables
1273                 foreach ($this->getVarStack('general') as $index => $currVariable) {
1274                         // Compile the value
1275                         $value = $this->compileRawCode($this->readVariable($currVariable['name']), true);
1276
1277                         // Debug message
1278                         //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: name=' . $currVariable['name'] . ',value=' . $value);
1279
1280                         // Remove it from stack
1281                         $this->removeVariable($currVariable['name'], 'general');
1282                         //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: value='. $value . ',name=' . $currVariable['name'] . ',index=' . $index);
1283
1284                         // Is it a configuration key?
1285                         if ($this->getConfigInstance()->isConfigurationEntrySet($value)) {
1286                                 // The value itself is a configuration entry
1287                                 $this->assignConfigVariable($value);
1288                         } else {
1289                                 // Re-assign the value directly
1290                                 $this->assignVariable($currVariable['name'], $value);
1291                         }
1292                 } // END - foreach
1293         }
1294
1295         /**
1296          * Compile all variables by inserting their respective values
1297          *
1298          * @return      void
1299          * @todo        Make this code some nicer...
1300          */
1301         public final function compileVariables () {
1302                 // Initialize the $content array
1303                 $validVar = $this->getConfigInstance()->getConfigEntry('tpl_valid_var');
1304                 $dummy = array();
1305
1306                 // Iterate through all general variables
1307                 foreach ($this->getVarStack('general') as $currVariable) {
1308                         // Transfer it's name/value combination to the $content array
1309                         //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:' . $currVariable['name'] . '=<pre>' . htmlentities($currVariable['value']).'</pre>');
1310                         $dummy[$currVariable['name']] = $currVariable['value'];
1311                 }// END - if
1312
1313                 // Set the new variable (don't remove the second dollar!)
1314                 $$validVar = $dummy;
1315
1316                 // Remove some variables
1317                 unset($index);
1318                 unset($currVariable);
1319
1320                 // Run the compilation three times to get content from helper classes in
1321                 $cnt = 0;
1322                 while ($cnt < 3) {
1323                         // Finalize the compilation of template variables
1324                         $this->finalizeVariableCompilation();
1325
1326                         // Prepare the eval() command for comiling the template
1327                         $eval = sprintf('$result = "%s";',
1328                                 addslashes($this->getRawTemplateData())
1329                         );
1330
1331                         // This loop does remove the backslashes (\) in PHP parameters
1332                         while (strpos($eval, $this->codeBegin) !== false) {
1333                                 // Get left part before "<?"
1334                                 $evalLeft = substr($eval, 0, strpos($eval, $this->codeBegin));
1335
1336                                 // Get all from right of "<?"
1337                                 $evalRight = substr($eval, (strpos($eval, $this->codeBegin) + 5));
1338
1339                                 // Cut middle part out and remove escapes
1340                                 $evalMiddle = trim(substr($evalRight, 0, strpos($evalRight, $this->codeEnd)));
1341                                 $evalMiddle = stripslashes($evalMiddle);
1342
1343                                 // Remove the middle part from right one
1344                                 $evalRight = substr($evalRight, (strpos($evalRight, $this->codeEnd) + 2));
1345
1346                                 // And put all together
1347                                 $eval = sprintf('%s<%%php %s %%>%s', $evalLeft, $evalMiddle, $evalRight);
1348                         } // END - while
1349
1350                         // Prepare PHP code for eval() command
1351                         $eval = str_replace(
1352                                 '<%php', '";',
1353                                 str_replace(
1354                                         '%>',
1355                                         "\n\$result .= \"",
1356                                         $eval
1357                                 )
1358                         );
1359
1360                         // Run the constructed command. This will "compile" all variables in
1361                         eval($eval);
1362
1363                         // Goes something wrong?
1364                         if ((!isset($result)) || (empty($result))) {
1365                                 // Output eval command
1366                                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Failed eval() code: <pre>%s</pre>', $this->markupCode($eval, true)), true);
1367
1368                                 // Output backtrace here
1369                                 $this->debugBackTrace();
1370                         } // END - if
1371
1372                         // Set raw template data
1373                         $this->setRawTemplateData($result);
1374                         $cnt++;
1375                 } // END - while
1376
1377                 // Final variable assignment
1378                 $this->finalizeVariableCompilation();
1379
1380                 // Set the new content
1381                 $this->setCompiledData($this->getRawTemplateData());
1382         }
1383
1384         /**
1385          * Compile all required templates into the current loaded one
1386          *
1387          * @return      void
1388          * @throws      UnexpectedTemplateTypeException If the template type is
1389          *                                                                                      not "code"
1390          * @throws      InvalidArrayCountException              If an unexpected array
1391          *                                                                                      count has been found
1392          */
1393         public function compileTemplate () {
1394                 // Get code type to make things shorter
1395                 $codeType = $this->getConfigInstance()->getConfigEntry('code_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_template_type');
1396
1397                 // We will only work with template type "code" from configuration
1398                 if (substr($this->getTemplateType(), 0, strlen($codeType)) != $codeType) {
1399                         // Abort here
1400                         throw new UnexpectedTemplateTypeException(array($this, $this->getTemplateType(), $this->getConfigInstance()->getConfigEntry('code_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_template_type')), self::EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED);
1401                 } // END - if
1402
1403                 // Get the raw data.
1404                 $rawData = $this->getRawTemplateData();
1405
1406                 // Remove double spaces and trim leading/trailing spaces
1407                 $rawData = trim(str_replace('  ', ' ', $rawData));
1408
1409                 // Search for raw variables
1410                 $this->extractVariablesFromRawData($rawData);
1411
1412                 // Search for code-tags which are {? ?}
1413                 preg_match_all($this->regExpCodeTags, $rawData, $templateMatches);
1414
1415                 // Debug message
1416                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:templateMatches=' . print_r($templateMatches , true));
1417
1418                 // Analyze the matches array
1419                 if ((is_array($templateMatches)) && (count($templateMatches) == 4) && (count($templateMatches[0]) > 0)) {
1420                         // Entries are found:
1421                         //
1422                         // The main analysis
1423                         $this->analyzeTemplate($templateMatches);
1424
1425                         // Compile raw template data
1426                         $this->compileRawTemplateData($templateMatches);
1427
1428                         // Are there some raw templates left for loading?
1429                         $this->loadExtraRawTemplates();
1430
1431                         // Are some raw templates found and loaded?
1432                         if (count($this->rawTemplates) > 0) {
1433                                 // Insert all raw templates
1434                                 $this->insertRawTemplates();
1435
1436                                 // Remove the raw template content as well
1437                                 $this->setRawTemplateData('');
1438                         } // END - if
1439                 } // END - if($templateMatches ...
1440         }
1441
1442         /**
1443          * Loads a given view helper (by name)
1444          *
1445          * @param       $helperName             The helper's name
1446          * @return      void
1447          */
1448         protected function loadViewHelper ($helperName) {
1449                 // Is this view helper loaded?
1450                 if (!isset($this->helpers[$helperName])) {
1451                         // Create a class name
1452                         $className = self::convertToClassName($helperName) . 'ViewHelper';
1453
1454                         // Generate new instance
1455                         $this->helpers[$helperName] = ObjectFactory::createObjectByName($className);
1456                 } // END - if
1457
1458                 // Return the requested instance
1459                 return $this->helpers[$helperName];
1460         }
1461
1462         /**
1463          * Transfers the content of this template engine to a given response instance
1464          *
1465          * @param       $responseInstance       An instance of a Responseable class
1466          * @return      void
1467          */
1468         public function transferToResponse (Responseable $responseInstance) {
1469                 // Get the content and set it in response class
1470                 $responseInstance->writeToBody($this->getCompiledData());
1471         }
1472
1473         /**
1474          * "Compiles" a variable by replacing {?var?} with it's content
1475          *
1476          * @param       $rawCode                        Raw code to compile
1477          * @param       $setMatchAsCode         Sets $match if readVariable() returns empty result
1478          * @return      $rawCode        Compile code with inserted variable value
1479          */
1480         public function compileRawCode ($rawCode, $setMatchAsCode=false) {
1481                 // Find the variables
1482                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:rawCode=<pre>' . htmlentities($rawCode) . '</pre>');
1483                 preg_match_all($this->regExpVarValue, $rawCode, $varMatches);
1484
1485                 // Compile all variables
1486                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:<pre>' . print_r($varMatches, true) . '</pre>');
1487                 foreach ($varMatches[0] as $match) {
1488                         // Add variable tags around it
1489                         $varCode = '{?' . $match . '?}';
1490
1491                         // Debug message
1492                         //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:varCode=' . $varCode);
1493
1494                         // Is the variable found in code? (safes some calls)
1495                         if (strpos($rawCode, $varCode) !== false) {
1496                                 // Debug message
1497                                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: match=' . $match . ',rawCode[' . gettype($rawCode) . ']=' . $rawCode);
1498
1499                                 // Use $match as new value or $value from read variable?
1500                                 if ($setMatchAsCode === true) {
1501                                         // Insert match
1502                                         $rawCode = str_replace($varCode, $match, $rawCode);
1503                                 } else {
1504                                         // Read the variable
1505                                         $value = $this->readVariable($match);
1506
1507                                         // Insert value
1508                                         $rawCode = str_replace($varCode, $value, $rawCode);
1509                                 }
1510                         } // END - if
1511                 } // END - foreach
1512
1513                 // Return the compiled data
1514                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:rawCode=<pre>' . htmlentities($rawCode) . '</pre>');
1515                 return $rawCode;
1516         }
1517
1518         /**
1519          * Getter for variable group array
1520          *
1521          * @return      $variableGroups All variable groups
1522          */
1523         public final function getVariableGroups () {
1524                 return $this->variableGroups;
1525         }
1526
1527         /**
1528          * Renames a variable in code and in stack
1529          *
1530          * @param       $oldName        Old name of variable
1531          * @param       $newName        New name of variable
1532          * @return      void
1533          */
1534         public function renameVariable ($oldName, $newName) {
1535                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: oldName=' . $oldName . ', newName=' . $newName);
1536                 // Get raw template code
1537                 $rawData = $this->getRawTemplateData();
1538
1539                 // Replace it
1540                 $rawData = str_replace($oldName, $newName, $rawData);
1541
1542                 // Set the code back
1543                 $this->setRawTemplateData($rawData);
1544         }
1545
1546         /**
1547          * Renders the given XML content
1548          *
1549          * @param       $content        Valid XML content or if not set the current loaded raw content
1550          * @return      void
1551          * @throws      XmlParserException      If an XML error was found
1552          */
1553         public function renderXmlContent ($content = NULL) {
1554                 // Is the content set?
1555                 if (is_null($content)) {
1556                         // Get current content
1557                         $content = $this->getRawTemplateData();
1558                 } // END - if
1559
1560                 // Get a XmlParser instance
1561                 $parserInstance = ObjectFactory::createObjectByConfiguredName('xml_parser_class', array($this));
1562
1563                 // Check if XML compacting is enabled
1564                 if ($this->isXmlCompactingEnabled()) {
1565                         // Yes, so get a decorator class for transparent compacting
1566                         $parserInstance = ObjectFactory::createObjectByConfiguredName('deco_compacting_xml_parser_class', array($parserInstance));
1567                 } // END - if
1568
1569                 // Parse the XML document
1570                 $parserInstance->parseXmlContent($content);
1571         }
1572
1573         /**
1574          * Enables or disables language support
1575          *
1576          * @param       $languageSupport        New language support setting
1577          * @return      void
1578          */
1579         public final function enableLanguageSupport ($languageSupport = true) {
1580                 $this->languageSupport = (bool) $languageSupport;
1581         }
1582
1583         /**
1584          * Checks whether language support is enabled
1585          *
1586          * @return      $languageSupport        Whether language support is enabled or disabled
1587          */
1588         public final function isLanguageSupportEnabled () {
1589                 return $this->languageSupport;
1590         }
1591
1592         /**
1593          * Enables or disables XML compacting
1594          *
1595          * @param       $xmlCompacting  New XML compacting setting
1596          * @return      void
1597          */
1598         public final function enableXmlCompacting ($xmlCompacting = true) {
1599                 $this->xmlCompacting = (bool) $xmlCompacting;
1600         }
1601
1602         /**
1603          * Checks whether XML compacting is enabled
1604          *
1605          * @return      $xmlCompacting  Whether XML compacting is enabled or disabled
1606          */
1607         public final function isXmlCompactingEnabled () {
1608                 return $this->xmlCompacting;
1609         }
1610
1611         /**
1612          * Removes all commentd, tabs and new-line characters to compact the content
1613          *
1614          * @param       $uncompactedContent             The uncompacted content
1615          * @return      $compactedContent               The compacted content
1616          */
1617         public function compactContent ($uncompactedContent) {
1618                 // First, remove all tab/new-line/revert characters
1619                 $compactedContent = str_replace(chr(9), '', str_replace(chr(10), '', str_replace(chr(13), '', $uncompactedContent)));
1620
1621                 // Then regex all comments like <!-- //--> away
1622                 preg_match_all($this->regExpComments, $compactedContent, $matches);
1623
1624                 // Do we have entries?
1625                 if (isset($matches[0][0])) {
1626                         // Remove all
1627                         foreach ($matches[0] as $match) {
1628                                 // Remove the match
1629                                 $compactedContent = str_replace($match, '', $compactedContent);
1630                         } // END - foreach
1631                 } // END - if
1632
1633                 // Set the content again
1634                 $this->setRawTemplateData($compactedContent);
1635
1636                 // Return compacted content
1637                 return $compactedContent;
1638         }
1639
1640 }