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