3 namespace Org\Mxchange\CoreFramework\Template\Xml;
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;
11 * A decorator for XML template engines which rewrites the XML for compacting
14 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
33 class XmlRewriterTemplateDecorator extends BaseDecorator implements CompileableTemplate {
35 * Template engine instance
37 private $templateInstance = NULL;
40 * Protected constructor
44 protected function __construct () {
45 // Call parent constructor
46 parent::__construct(__CLASS__);
50 * Creates an instance of the class TemplateEngine and prepares it for usage
52 * @param $innerTemplateInstance A CompileableTemplate instance
53 * @return $templateInstance An instance of TemplateEngine
55 public static final function createXmlRewriterTemplateDecorator (CompileableTemplate $innerTemplateInstance) {
57 $templateInstance = new XmlRewriterTemplateDecorator();
59 // Set the inner template engine
60 $templateInstance->setTemplateInstance($innerTemplateInstance);
62 // Return the prepared instance
63 return $templateInstance;
67 * Setter for template engine instances
69 * @param $templateInstance An instance of a template engine class
72 protected final function setTemplateInstance (CompileableTemplate $templateInstance) {
73 $this->templateInstance = $templateInstance;
77 * Getter for template engine instances
79 * @return $templateInstance An instance of a template engine class
81 protected final function getTemplateInstance () {
82 return $this->templateInstance;
86 * Settter for variable group
88 * @param $groupName Name of variable group
89 * @param $add Whether add this group
92 public function setVariableGroup (string $groupName, bool $add = true) {
93 // Call the inner class' method
94 $this->getTemplateInstance()->setVariableGroup($groupName, $add);
98 * Adds a variable to current group
100 * @param $variableName Variable to set
101 * @param $value Value to store in variable
104 public function addGroupVariable (string $variableName, $value) {
105 // Call the inner class' method
106 $this->getTemplateInstance()->addGroupVariable($variableName, $value);
110 * Getter for base path
112 * @return $templateBasePath The relative base path for all templates
114 public final function getTemplateBasePath () {
115 // Call the inner class' method
116 return $this->getTemplateInstance()->getTemplateBasePath();
120 * Getter for generic base path
122 * @return $templateBasePath The relative base path for all templates
124 public final function getGenericBasePath () {
125 // Call the inner class' method
126 return $this->getTemplateInstance()->getGenericBasePath();
130 * Getter for template extension
132 * @return $templateExtension The file extension for all uncompiled templates
134 public final function getRawTemplateExtension () {
135 // Call the inner class' method
136 return $this->getTemplateInstance()->getRawTemplateExtension();
140 * Getter for given variable group
142 * @param $variableGroup Variable group to check
143 * @return $varStack Found variable group
145 public function getVarStack (string $variableGroup) {
146 // Call the inner class' method
147 return $this->getTemplateInstance()->getVarStack($variableGroup);
151 * Getter for code-template extension
153 * @return $codeExtension The file extension for all code templates
155 public final function getCodeTemplateExtension () {
156 // Call the inner class' method
157 return $this->getTemplateInstance()->getCodeTemplateExtension();
161 * Getter for template type
163 * @return $templateType The current template's type
165 public final function getTemplateType () {
166 // Call the inner class' method
167 return $this->getTemplateInstance()->getTemplateType();
171 * Assign (add) a given variable with a value
173 * @param $variableName The variable we are looking for
174 * @param $value The value we want to store in the variable
177 public function assignVariable (string $variableName, $value) {
178 // Call the inner class' method
179 $this->getTemplateInstance()->assignVariable($variableName, $value);
183 * Removes a given variable
185 * @param $variableName The variable we are looking for
186 * @param $variableGroup Name of variable group (default: 'general')
189 public function removeVariable (string $variableName, string $variableGroup = 'general') {
190 // Call the inner class' method
191 $this->getTemplateInstance()->removeVariable($variableName, $variableGroup);
195 * Load a specified HTML template into the engine
197 * @param $template The web template we shall load which is located in
201 public function loadHtmlTemplate (string $template) {
202 // Call the inner class' method
203 $this->getTemplateInstance()->loadHtmlTemplate($template);
207 * Assign a given congfiguration variable with a value
209 * @param $variableName The configuration variable we want to assign
212 public function assignConfigVariable (string $variableName) {
213 // Call the inner class' method
214 $this->getTemplateInstance()->assignConfigVariable($variableName);
218 * Load a specified code template into the engine
220 * @param $template The code template we shall load which is
221 * located in 'code' by default
224 public function loadCodeTemplate (string $template) {
225 // Call the inner class' method
226 $this->getTemplateInstance()->loadCodeTemplate($template);
230 * Load a specified email template into the engine for later compilation
231 * with other code/web/email templates.
233 * @param $template The email template we shall load which is
234 * located in "html" by default
237 public function loadEmailTemplate ($template) {
238 // Call the inner class' method
239 $this->getTemplateInstance()->loadEmailTemplate($template);
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.
249 public function compileConfigInVariables () {
250 // Call the inner class' method
251 $this->getTemplateInstance()->compileConfigInVariables();
255 * Compile all variables by inserting their respective values
259 public function compileVariables () {
260 // Call the inner class' method
261 $this->getTemplateInstance()->compileVariables();
265 * Compile all required templates into the current loaded one
269 public function compileTemplate () {
270 // Call the inner class' method
271 $this->getTemplateInstance()->compileTemplate();
275 * Assigns the last loaded raw template content with a given variable
277 * @param $templateName Name of the template we want to assign
278 * @param $variableName Name of the variable we want to assign
281 public function assignTemplateWithVariable (string $templateName, string $variableName) {
282 // Call the inner class' method
283 $this->getTemplateInstance()->assignTemplateWithVariable($templateName, $variableName);
287 * Transfers the content of this template engine to a given response instance
289 * @param $responseInstance An instance of a Responseable class
292 public function transferToResponse (Responseable $responseInstance) {
293 // Call the inner class' method
294 $this->getTemplateInstance()->transportToResponse($responseInstance);
298 * Assigns all the application data with template variables
302 public function assignApplicationData () {
303 // Call the inner class' method
304 $this->getTemplateInstance()->assignApplicationData();
308 * "Compiles" a variable by replacing {?var?} with it's content
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
314 public function compileRawCode (string $rawCode, bool $setMatchAsCode = false) {
315 return $this->getTemplateInstance()->compileRawCode($rawCode, $setMatchAsCode);
319 * Getter for variable group array
321 * @return $variableGroups All variable groups
323 public final function getVariableGroups () {
324 // Call the inner class' method
325 return $this->getTemplateInstance()->getVariableGroups();
329 * Getter for raw template data
331 * @return $rawTemplateData The raw data from the template
333 public function getRawTemplateData () {
334 // Call the inner class' method
335 return $this->getTemplateInstance()->getRawTemplateData();
339 * Renames a variable in code and in stack
341 * @param $oldName Old name of variable
342 * @param $newName New name of variable
345 public function renameVariable (string $oldName, string $newName) {
346 // Call the inner class' method
347 $this->getTemplateInstance()->renameVariable($oldName, $newName);
351 * Renders the given XML content
353 * @param $content Valid XML content or if not set the current loaded raw content
355 * @throws XmlParserException If an XML error was found
357 public function renderXmlContent (string $content = NULL) {
358 // Call the inner class' method
359 $this->getTemplateInstance()->renderXmlContent($content);
363 * Enables or disables language support
365 * @param $languageSupport New language support setting
368 public function enableLanguageSupport (bool $languageSupport = true) {
369 // Call the inner class' method
370 $this->getTemplateInstance()->enableLanguageSupport($languageSupport);
374 * Checks whether language support is enabled
376 * @return $languageSupport Whether language support is enabled or disabled
378 public function isLanguageSupportEnabled () {
379 // Call the inner class' method
380 return $this->getTemplateInstance()->isLanguageSupportEnabled();
384 * Enables or disables XML compacting
386 * @param $xmlCompacting New XML compacting setting
389 public function enableXmlCompacting (bool $xmlCompacting = true) {
390 // Call the inner class' method
391 $this->getTemplateInstance()->enableXmlCompacting($xmlCompacting);
395 * Checks whether XML compacting is enabled
397 * @return $xmlCompacting Whether XML compacting is enabled or disabled
399 public function isXmlCompactingEnabled () {
400 // Call the inner class' method
401 return $this->getTemplateInstance()->isXmlCompactingEnabled();
405 * Handles the start element of an XML resource
407 * @param $resource XML parser resource (currently ignored)
408 * @param $element The element we shall handle
409 * @param $attributes All attributes
411 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
413 public function startElement ($resource, string $element, array $attributes) {
414 // Call the inner class' method
415 $this->getTemplateInstance()->startElement($resource, $element, $attributes);
419 * Ends the main or sub node by sending out the gathered data
421 * @param $resource An XML resource pointer (currently ignored)
422 * @param $nodeName Name of the node we want to finish
424 * @throws XmlNodeMismatchException If current main node mismatches the closing one
426 public function finishElement ($resource, string $nodeName) {
427 // Call the inner class' method
428 $this->getTemplateInstance()->finishElement($resource, $nodeName);
434 * @param $resource XML parser resource (currently ignored)
435 * @param $characters Characters to handle
437 * @todo Find something useful with this!
439 public function characterHandler ($resource, $characters) {
440 // Call the inner class' method but trim the characters before
441 $this->getTemplateInstance()->characterHandler($resource, trim($characters));
445 * Removes all comments, tabs and new-line charcters to compact the content
447 * @param $uncompactedContent The uncompacted content
448 * @return $compactedContent The compacted content
450 public function compactContent (string $uncompactedContent) {
452 $compactedContent = $this->getTemplateInstance()->compactContent($uncompactedContent);
455 return $compactedContent;
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.
465 * @param $variables An array with variables to be assigned
468 public function assignMultipleVariables (array $variables) {
469 // Call the inner class' method but trim the characters before
470 $this->getTemplateInstance()->assignMultipleVariables($variables);