]> git.mxchange.org Git - core.git/blob - framework/main/classes/decorator/template/class_XmlRewriterTemplateDecorator.php
63735c7930e9fa7293e5e477bba5c43abb0b4eba
[core.git] / framework / main / classes / decorator / template / class_XmlRewriterTemplateDecorator.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Template\Xml;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Generic\BaseDecorator;
7 use Org\Mxchange\CoreFramework\Response\Responseable;
8 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
9
10 /**
11  * A decorator for XML template engines which rewrites the XML for compacting
12  * it.
13  *
14  * @author              Roland Haeder <webmaster@shipsimu.org>
15  * @version             0.0.0
16  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
17  * @license             GNU GPL 3.0 or any newer version
18  * @link                http://www.shipsimu.org
19  *
20  * This program is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program. If not, see <http://www.gnu.org/licenses/>.
32  */
33 class XmlRewriterTemplateDecorator extends BaseDecorator implements CompileableTemplate {
34         /**
35          * Template engine instance
36          */
37         private $templateInstance = NULL;
38
39         /**
40          * Protected constructor
41          *
42          * @return      void
43          */
44         protected function __construct () {
45                 // Call parent constructor
46                 parent::__construct(__CLASS__);
47         }
48
49         /**
50          * Creates an instance of the class TemplateEngine and prepares it for usage
51          *
52          * @param       $innerTemplateInstance  A CompileableTemplate instance
53          * @return      $templateInstance       An instance of TemplateEngine
54          */
55         public static final function createXmlRewriterTemplateDecorator (CompileableTemplate $innerTemplateInstance) {
56                 // Get a new instance
57                 $templateInstance = new XmlRewriterTemplateDecorator();
58
59                 // Set the inner template engine
60                 $templateInstance->setTemplateInstance($innerTemplateInstance);
61
62                 // Return the prepared instance
63                 return $templateInstance;
64         }
65
66         /**
67          * Setter for template engine instances
68          *
69          * @param       $templateInstance       An instance of a template engine class
70          * @return      void
71          */
72         protected final function setTemplateInstance (CompileableTemplate $templateInstance) {
73                 $this->templateInstance = $templateInstance;
74         }
75
76         /**
77          * Getter for template engine instances
78          *
79          * @return      $templateInstance       An instance of a template engine class
80          */
81         protected final function getTemplateInstance () {
82                 return $this->templateInstance;
83         }
84
85         /**
86          * Settter for variable group
87          *
88          * @param       $groupName      Name of variable group
89          * @param       $add            Whether add this group
90          * @return      void
91          */
92         public function setVariableGroup (string $groupName, bool $add = true) {
93                 // Call the inner class' method
94                 $this->getTemplateInstance()->setVariableGroup($groupName, $add);
95         }
96
97         /**
98          * Adds a variable to current group
99          *
100          * @param       $variableName   Variable to set
101          * @param       $value  Value to store in variable
102          * @return      void
103          */
104         public function addGroupVariable (string $variableName, $value) {
105                 // Call the inner class' method
106                 $this->getTemplateInstance()->addGroupVariable($variableName, $value);
107         }
108
109         /**
110          * Getter for base path
111          *
112          * @return      $templateBasePath       The relative base path for all templates
113          */
114         public final function getTemplateBasePath () {
115                 // Call the inner class' method
116                 return $this->getTemplateInstance()->getTemplateBasePath();
117         }
118
119         /**
120          * Getter for generic base path
121          *
122          * @return      $templateBasePath       The relative base path for all templates
123          */
124         public final function getGenericBasePath () {
125                 // Call the inner class' method
126                 return $this->getTemplateInstance()->getGenericBasePath();
127         }
128
129         /**
130          * Getter for template extension
131          *
132          * @return      $templateExtension      The file extension for all uncompiled templates
133          */
134         public final function getRawTemplateExtension () {
135                 // Call the inner class' method
136                 return $this->getTemplateInstance()->getRawTemplateExtension();
137         }
138
139         /**
140          * Getter for given variable group
141          *
142          * @param       $variableGroup  Variable group to check
143          * @return      $varStack               Found variable group
144          */
145         public function getVarStack (string $variableGroup) {
146                 // Call the inner class' method
147                 return $this->getTemplateInstance()->getVarStack($variableGroup);
148         }
149
150         /**
151          * Getter for code-template extension
152          *
153          * @return      $codeExtension  The file extension for all code templates
154          */
155         public final function getCodeTemplateExtension () {
156                 // Call the inner class' method
157                 return $this->getTemplateInstance()->getCodeTemplateExtension();
158         }
159
160         /**
161          * Getter for template type
162          *
163          * @return      $templateType   The current template's type
164          */
165         public final function getTemplateType () {
166                 // Call the inner class' method
167                 return $this->getTemplateInstance()->getTemplateType();
168         }
169
170         /**
171          * Assign (add) a given variable with a value
172          *
173          * @param       $variableName   The variable we are looking for
174          * @param       $value  The value we want to store in the variable
175          * @return      void
176          */
177         public function assignVariable (string $variableName, $value) {
178                 // Call the inner class' method
179                 $this->getTemplateInstance()->assignVariable($variableName, $value);
180         }
181
182         /**
183          * Removes a given variable
184          *
185          * @param       $variableName   The variable we are looking for
186          * @param       $variableGroup  Name of variable group (default: 'general')
187          * @return      void
188          */
189         public function removeVariable (string $variableName, string $variableGroup = 'general') {
190                 // Call the inner class' method
191                 $this->getTemplateInstance()->removeVariable($variableName, $variableGroup);
192         }
193
194         /**
195          * Load a specified HTML template into the engine
196          *
197          * @param       $template       The web template we shall load which is located in
198          *                                              'html' by default
199          * @return      void
200          */
201         public function loadHtmlTemplate (string $template) {
202                 // Call the inner class' method
203                 $this->getTemplateInstance()->loadHtmlTemplate($template);
204         }
205
206         /**
207          * Assign a given congfiguration variable with a value
208          *
209          * @param       $variableName   The configuration variable we want to assign
210          * @return      void
211          */
212         public function assignConfigVariable (string $variableName) {
213                 // Call the inner class' method
214                 $this->getTemplateInstance()->assignConfigVariable($variableName);
215         }
216
217         /**
218          * Load a specified code template into the engine
219          *
220          * @param       $template       The code template we shall load which is
221          *                                              located in 'code' by default
222          * @return      void
223          */
224         public function loadCodeTemplate (string $template) {
225                 // Call the inner class' method
226                 $this->getTemplateInstance()->loadCodeTemplate($template);
227         }
228
229         /**
230          * Load a specified email template into the engine for later compilation
231          * with other code/web/email templates.
232          *
233          * @param       $template       The email template we shall load which is
234          *                                              located in "html" by default
235          * @return      void
236          */
237         public function loadEmailTemplate ($template) {
238                 // Call the inner class' method
239                 $this->getTemplateInstance()->loadEmailTemplate($template);
240         }
241
242         /**
243          * Compiles configuration place-holders in all variables. This 'walks'
244          * through the variable stack 'general'. It interprets all values from that
245          * variables as configuration entries after compiling them.
246          *
247          * @return      void
248          */
249         public function compileConfigInVariables () {
250                 // Call the inner class' method
251                 $this->getTemplateInstance()->compileConfigInVariables();
252         }
253
254         /**
255          * Compile all variables by inserting their respective values
256          *
257          * @return      void
258          */
259         public function compileVariables () {
260                 // Call the inner class' method
261                 $this->getTemplateInstance()->compileVariables();
262         }
263
264         /**
265          * Compile all required templates into the current loaded one
266          *
267          * @return      void
268          */
269         public function compileTemplate () {
270                 // Call the inner class' method
271                 $this->getTemplateInstance()->compileTemplate();
272         }
273
274         /**
275          * Assigns the last loaded raw template content with a given variable
276          *
277          * @param       $templateName   Name of the template we want to assign
278          * @param       $variableName   Name of the variable we want to assign
279          * @return      void
280          */
281         public function assignTemplateWithVariable (string $templateName, string $variableName) {
282                 // Call the inner class' method
283                 $this->getTemplateInstance()->assignTemplateWithVariable($templateName, $variableName);
284         }
285
286         /**
287          * Transfers the content of this template engine to a given response instance
288          *
289          * @param       $responseInstance       An instance of a Responseable class
290          * @return      void
291          */
292         public function transferToResponse (Responseable $responseInstance) {
293                 // Call the inner class' method
294                 $this->getTemplateInstance()->transportToResponse($responseInstance);
295         }
296
297         /**
298          * Assigns all the application data with template variables
299          *
300          * @return      void
301          */
302         public function assignApplicationData () {
303                 // Call the inner class' method
304                 $this->getTemplateInstance()->assignApplicationData();
305         }
306
307         /**
308          * "Compiles" a variable by replacing {?var?} with it's content
309          *
310          * @param       $rawCode                        Raw code to compile
311          * @param       $setMatchAsCode         Sets $match if readVariable() returns empty result
312          * @return      $rawCode                        Compile code with inserted variable value
313          */
314         public function compileRawCode (string $rawCode, bool $setMatchAsCode = false) {
315                 return $this->getTemplateInstance()->compileRawCode($rawCode, $setMatchAsCode);
316         }
317
318         /**
319          * Getter for variable group array
320          *
321          * @return      $variableGroups         All variable groups
322          */
323         public final function getVariableGroups () {
324                 // Call the inner class' method
325                 return $this->getTemplateInstance()->getVariableGroups();
326         }
327
328         /**
329          * Getter for raw template data
330          *
331          * @return      $rawTemplateData        The raw data from the template
332          */
333         public function getRawTemplateData () {
334                 // Call the inner class' method
335                 return $this->getTemplateInstance()->getRawTemplateData();
336         }
337
338         /**
339          * Renames a variable in code and in stack
340          *
341          * @param       $oldName        Old name of variable
342          * @param       $newName        New name of variable
343          * @return      void
344          */
345         public function renameVariable (string $oldName, string $newName) {
346                 // Call the inner class' method
347                 $this->getTemplateInstance()->renameVariable($oldName, $newName);
348         }
349
350         /**
351          * Renders the given XML content
352          *
353          * @param       $content        Valid XML content or if not set the current loaded raw content
354          * @return      void
355          * @throws      XmlParserException      If an XML error was found
356          */
357         public function renderXmlContent (string $content = NULL) {
358                 // Call the inner class' method
359                 $this->getTemplateInstance()->renderXmlContent($content);
360         }
361
362         /**
363          * Enables or disables language support
364          *
365          * @param       $languageSupport        New language support setting
366          * @return      void
367          */
368         public function enableLanguageSupport (bool $languageSupport = true) {
369                 // Call the inner class' method
370                 $this->getTemplateInstance()->enableLanguageSupport($languageSupport);
371         }
372
373         /**
374          * Checks whether language support is enabled
375          *
376          * @return      $languageSupport        Whether language support is enabled or disabled
377          */
378         public function isLanguageSupportEnabled () {
379                 // Call the inner class' method
380                 return $this->getTemplateInstance()->isLanguageSupportEnabled();
381         }
382
383         /**
384          * Enables or disables XML compacting
385          *
386          * @param       $xmlCompacting  New XML compacting setting
387          * @return      void
388          */
389         public function enableXmlCompacting (bool $xmlCompacting = true) {
390                 // Call the inner class' method
391                 $this->getTemplateInstance()->enableXmlCompacting($xmlCompacting);
392         }
393
394         /**
395          * Checks whether XML compacting is enabled
396          *
397          * @return      $xmlCompacting  Whether XML compacting is enabled or disabled
398          */
399         public function isXmlCompactingEnabled () {
400                 // Call the inner class' method
401                 return $this->getTemplateInstance()->isXmlCompactingEnabled();
402         }
403
404         /**
405          * Handles the start element of an XML resource
406          *
407          * @param       $resource               XML parser resource (currently ignored)
408          * @param       $element                The element we shall handle
409          * @param       $attributes             All attributes
410          * @return      void
411          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
412          */
413         public function startElement ($resource, string $element, array $attributes) {
414                 // Call the inner class' method
415                 $this->getTemplateInstance()->startElement($resource, $element, $attributes);
416         }
417
418         /**
419          * Ends the main or sub node by sending out the gathered data
420          *
421          * @param       $resource       An XML resource pointer (currently ignored)
422          * @param       $nodeName       Name of the node we want to finish
423          * @return      void
424          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
425          */
426         public function finishElement ($resource, string $nodeName) {
427                 // Call the inner class' method
428                 $this->getTemplateInstance()->finishElement($resource, $nodeName);
429         }
430
431         /**
432          * Currently not used
433          *
434          * @param       $resource               XML parser resource (currently ignored)
435          * @param       $characters             Characters to handle
436          * @return      void
437          * @todo        Find something useful with this!
438          */
439         public function characterHandler ($resource, $characters) {
440                 // Call the inner class' method but trim the characters before
441                 $this->getTemplateInstance()->characterHandler($resource, trim($characters));
442         }
443
444         /**
445          * Removes all comments, tabs and new-line charcters to compact the content
446          *
447          * @param       $uncompactedContent             The uncompacted content
448          * @return      $compactedContent               The compacted content
449          */
450         public function compactContent (string $uncompactedContent) {
451                 // Compact it ...
452                 $compactedContent = $this->getTemplateInstance()->compactContent($uncompactedContent);
453
454                 // ... and return it
455                 return $compactedContent;
456         }
457
458         /**
459          * Assigns a lot variables into the stack of currently loaded template.
460          * This method should only be used in very rare circumstances, e.g. when
461          * you have to copy a whole set of variables into the template engine.
462          * Before you use this method, please make sure you have considered all
463          * other possiblities.
464          *
465          * @param       $variables      An array with variables to be assigned
466          * @return      void
467          */
468         public function assignMultipleVariables (array $variables) {
469                 // Call the inner class' method but trim the characters before
470                 $this->getTemplateInstance()->assignMultipleVariables($variables);
471         }
472
473 }