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