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