3 namespace CoreFramework\Template\Xml;
5 // Import framework stuff
6 use CoreFramework\Manager\ManageableApplication;
7 use CoreFramework\Template\CompileableTemplate;
10 * A decorator for XML template engines which rewrites the XML for compacting
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class XmlRewriterTemplateDecorator extends BaseDecorator implements CompileableTemplate {
34 * Protected constructor
38 protected function __construct () {
39 // Call parent constructor
40 parent::__construct(__CLASS__);
44 * Creates an instance of the class TemplateEngine and prepares it for usage
46 * @param $innerTemplateInstance A CompileableTemplate instance
47 * @return $templateInstance An instance of TemplateEngine
49 public static final function createXmlRewriterTemplateDecorator (CompileableTemplate $innerTemplateInstance) {
51 $templateInstance = new XmlRewriterTemplateDecorator();
53 // Set the inner template engine
54 $templateInstance->setTemplateInstance($innerTemplateInstance);
56 // Return the prepared instance
57 return $templateInstance;
61 * Settter for variable group
63 * @param $groupName Name of variable group
64 * @param $add Whether add this group
67 public function setVariableGroup ($groupName, $add = TRUE) {
68 // Call the inner class' method
69 $this->getTemplateInstance()->setVariableGroup($groupName, $add);
73 * Adds a variable to current group
75 * @param $var Variable to set
76 * @param $value Value to store in variable
79 public function addGroupVariable ($var, $value) {
80 // Call the inner class' method
81 $this->getTemplateInstance()->addGroupVariable($var, $value);
85 * Getter for base path
87 * @return $templateBasePath The relative base path for all templates
89 public final function getTemplateBasePath () {
90 // Call the inner class' method
91 return $this->getTemplateInstance()->getTemplateBasePath();
95 * Getter for generic base path
97 * @return $templateBasePath The relative base path for all templates
99 public final function getGenericBasePath () {
100 // Call the inner class' method
101 return $this->getTemplateInstance()->getGenericBasePath();
105 * Getter for template extension
107 * @return $templateExtension The file extension for all uncompiled templates
109 public final function getRawTemplateExtension () {
110 // Call the inner class' method
111 return $this->getTemplateInstance()->getRawTemplateExtension();
115 * Getter for given variable group
117 * @param $variableGroup Variable group to check
118 * @return $varStack Found variable group
120 public function getVarStack ($variableGroup) {
121 // Call the inner class' method
122 return $this->getTemplateInstance()->getVarStack($variableGroup);
126 * Getter for code-template extension
128 * @return $codeExtension The file extension for all code templates
130 public final function getCodeTemplateExtension () {
131 // Call the inner class' method
132 return $this->getTemplateInstance()->getCodeTemplateExtension();
136 * Getter for template type
138 * @return $templateType The current template's type
140 public final function getTemplateType () {
141 // Call the inner class' method
142 return $this->getTemplateInstance()->getTemplateType();
146 * Assign (add) a given variable with a value
148 * @param $var The variable we are looking for
149 * @param $value The value we want to store in the variable
151 * @throws EmptyVariableException If the variable name is left empty
153 public function assignVariable ($var, $value) {
154 // Call the inner class' method
155 $this->getTemplateInstance()->assignVariable($var, $value);
159 * Removes a given variable
161 * @param $variableName The variable we are looking for
162 * @param $variableGroup Name of variable group (default: 'general')
165 public function removeVariable ($variableName, $variableGroup = 'general') {
166 // Call the inner class' method
167 $this->getTemplateInstance()->removeVariable($variableName, $variableGroup);
171 * Load a specified HTML template into the engine
173 * @param $template The web template we shall load which is located in
177 public function loadHtmlTemplate ($template) {
178 // Call the inner class' method
179 $this->getTemplateInstance()->loadHtmlTemplate($template);
183 * Assign a given congfiguration variable with a value
185 * @param $variableName The configuration variable we want to assign
188 public function assignConfigVariable ($variableName) {
189 // Call the inner class' method
190 $this->getTemplateInstance()->assignConfigVariable($variableName);
194 * Load a specified code template into the engine
196 * @param $template The code template we shall load which is
197 * located in 'code' by default
200 public function loadCodeTemplate ($template) {
201 // Call the inner class' method
202 $this->getTemplateInstance()->loadCodeTemplate($template);
206 * Load a specified email template into the engine for later compilation
207 * with other code/web/email templates.
209 * @param $template The email template we shall load which is
210 * located in "html" by default
213 public function loadEmailTemplate ($template) {
214 // Call the inner class' method
215 $this->getTemplateInstance()->loadEmailTemplate($template);
219 * Compiles configuration place-holders in all variables. This 'walks'
220 * through the variable stack 'general'. It interprets all values from that
221 * variables as configuration entries after compiling them.
225 public function compileConfigInVariables () {
226 // Call the inner class' method
227 $this->getTemplateInstance()->compileConfigInVariables();
231 * Compile all variables by inserting their respective values
235 public function compileVariables () {
236 // Call the inner class' method
237 $this->getTemplateInstance()->compileVariables();
241 * Compile all required templates into the current loaded one
245 public function compileTemplate () {
246 // Call the inner class' method
247 $this->getTemplateInstance()->compileTemplate();
251 * Assigns the last loaded raw template content with a given variable
253 * @param $templateName Name of the template we want to assign
254 * @param $variableName Name of the variable we want to assign
257 public function assignTemplateWithVariable ($templateName, $variableName) {
258 // Call the inner class' method
259 $this->getTemplateInstance()->assignTemplateWithVariable($templateName, $variableName);
263 * Transfers the content of this template engine to a given response instance
265 * @param $responseInstance An instance of a response class
268 public function transferToResponse (Responseable $responseInstance) {
269 // Call the inner class' method
270 $this->getTemplateInstance()->transportToResponse($responseInstance);
274 * Assigns all the application data with template variables
276 * @param $applicationInstance A manageable application instance
279 public function assignApplicationData (ManageableApplication $applicationInstance) {
280 // Call the inner class' method
281 $this->getTemplateInstance()->assignApplicationData($applicationInstance);
285 * "Compiles" a variable by replacing {?var?} with it's content
287 * @param $rawCode Raw code to compile
288 * @param $setMatchAsCode Sets $match if readVariable() returns empty result
289 * @return $rawCode Compile code with inserted variable value
291 public function compileRawCode ($rawCode, $setMatchAsCode = FALSE) {
292 return $this->getTemplateInstance()->compileRawCode($rawCode, $setMatchAsCode);
296 * Getter for variable group array
298 * @return $variableGroups All variable groups
300 public final function getVariableGroups () {
301 // Call the inner class' method
302 return $this->getTemplateInstance()->getVariableGroups();
306 * Getter for raw template data
308 * @return $rawTemplateData The raw data from the template
310 public function getRawTemplateData () {
311 // Call the inner class' method
312 return $this->getTemplateInstance()->getRawTemplateData();
316 * Renames a variable in code and in stack
318 * @param $oldName Old name of variable
319 * @param $newName New name of variable
322 public function renameVariable ($oldName, $newName) {
323 // Call the inner class' method
324 $this->getTemplateInstance()->renameVariable($oldName, $newName);
328 * Renders the given XML content
330 * @param $content Valid XML content or if not set the current loaded raw content
332 * @throws XmlParserException If an XML error was found
334 public function renderXmlContent ($content = NULL) {
335 // Call the inner class' method
336 $this->getTemplateInstance()->renderXmlContent($content);
340 * Enables or disables language support
342 * @param $languageSupport New language support setting
345 public function enableLanguageSupport ($languageSupport = TRUE) {
346 // Call the inner class' method
347 $this->getTemplateInstance()->enableLanguageSupport($languageSupport);
351 * Checks whether language support is enabled
353 * @return $languageSupport Whether language support is enabled or disabled
355 public function isLanguageSupportEnabled () {
356 // Call the inner class' method
357 return $this->getTemplateInstance()->isLanguageSupportEnabled();
361 * Enables or disables XML compacting
363 * @param $xmlCompacting New XML compacting setting
366 public function enableXmlCompacting ($xmlCompacting = TRUE) {
367 // Call the inner class' method
368 $this->getTemplateInstance()->enableXmlCompacting($xmlCompacting);
372 * Checks whether XML compacting is enabled
374 * @return $xmlCompacting Whether XML compacting is enabled or disabled
376 public function isXmlCompactingEnabled () {
377 // Call the inner class' method
378 return $this->getTemplateInstance()->isXmlCompactingEnabled();
382 * Handles the start element of an XML resource
384 * @param $resource XML parser resource (currently ignored)
385 * @param $element The element we shall handle
386 * @param $attributes All attributes
388 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
390 public function startElement ($resource, $element, array $attributes) {
391 // Call the inner class' method
392 $this->getTemplateInstance()->startElement($resource, $element, $attributes);
396 * Ends the main or sub node by sending out the gathered data
398 * @param $resource An XML resource pointer (currently ignored)
399 * @param $nodeName Name of the node we want to finish
401 * @throws XmlNodeMismatchException If current main node mismatches the closing one
403 public function finishElement ($resource, $nodeName) {
404 // Call the inner class' method
405 $this->getTemplateInstance()->finishElement($resource, $nodeName);
411 * @param $resource XML parser resource (currently ignored)
412 * @param $characters Characters to handle
414 * @todo Find something useful with this!
416 public function characterHandler ($resource, $characters) {
417 // Call the inner class' method but trim the characters before
418 $this->getTemplateInstance()->characterHandler($resource, trim($characters));
422 * Removes all comments, tabs and new-line charcters to compact the content
424 * @param $uncompactedContent The uncompacted content
425 * @return $compactedContent The compacted content
427 public function compactContent ($uncompactedContent) {
429 $compactedContent = $this->getTemplateInstance()->compactContent($uncompactedContent);
432 return $compactedContent;
436 * Assigns a lot variables into the stack of currently loaded template.
437 * This method should only be used in very rare circumstances, e.g. when
438 * you have to copy a whole set of variables into the template engine.
439 * Before you use this method, please make sure you have considered all
440 * other possiblities.
442 * @param $variables An array with variables to be assigned
445 public function assignMultipleVariables (array $variables) {
446 // Call the inner class' method but trim the characters before
447 $this->getTemplateInstance()->assignMultipleVariables($variables);