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