3 * A Menu template engine class
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
26 * Main nodes in the XML tree ('menu' is ignored)
28 private $mainNodes = array(
33 * Sub nodes in the XML tree
35 private $subNodes = array(
61 * Variables for a menu entry
63 private $menuEntryVariables = array(
74 * Variables for a menu block
76 private $menuBlockVariables = array(
81 // Content is taken from menuEntries
89 * Rendered menu entries
91 private $menuEntries = array();
94 * Rendered menu blocks
96 private $menuBlocks = array();
101 private $menuInstance = NULL;
106 private $curr = array();
109 * Content from dependency
111 private $dependencyContent = array();
114 * Protected constructor
118 protected function __construct () {
119 // Call parent constructor
120 parent::__construct(__CLASS__);
124 * Creates an instance of the class TemplateEngine and prepares it for usage
126 * @param $menuInstance A RenderableMenu instance
127 * @return $templateInstance An instance of TemplateEngine
128 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
129 * @throws InvalidBasePathStringException If $templateBasePath is no string
130 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
131 * directory or not found
132 * @throws BasePathReadProtectedException If $templateBasePath is
135 public static final function createMenuTemplateEngine (RenderableMenu $menuInstance) {
136 // Get a new instance
137 $templateInstance = new MenuTemplateEngine();
139 // Get the application instance from registry
140 $applicationInstance = Registry::getRegistry()->getInstance('app');
142 // Determine base path
143 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
145 // Is the base path valid?
146 if (empty($templateBasePath)) {
147 // Base path is empty
148 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
149 } elseif (!is_string($templateBasePath)) {
151 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
152 } elseif (!is_dir($templateBasePath)) {
154 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
155 } elseif (!is_readable($templateBasePath)) {
157 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
161 $templateInstance->setTemplateBasePath($templateBasePath);
163 // Set template extensions
164 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
165 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('menu_template_extension'));
167 // Absolute output path for compiled templates
168 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
170 // Set the menu instance
171 $templateInstance->setMenuInstance($menuInstance);
173 // Init a variable stacker
174 $stackInstance = ObjectFactory::createObjectByConfiguredName('menu_stacker_class');
177 $templateInstance->setStackInstance($stackInstance);
179 // Return the prepared instance
180 return $templateInstance;
184 * Load a specified menu template into the engine
186 * @param $template The menu template we shall load which is
187 * located in 'menu' by default
190 public function loadMenuTemplate ($template) {
192 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
194 // Load the special template
195 $this->loadTemplate($template);
199 * Getter for current main node
201 * @return $currMainNode Current main node
203 public final function getCurrMainNode () {
204 return $this->curr['main_node'];
208 * Setter for current main node
210 * @param $element Element name to set as current main node
211 * @return $currMainNode Current main node
213 private final function setCurrMainNode ($element) {
214 $this->curr['main_node'] = (string) $element;
218 * Getter for main node array
220 * @return $mainNodes Array with valid main node names
222 public final function getMainNodes () {
223 return $this->mainNodes;
227 * Getter for sub node array
229 * @return $subNodes Array with valid sub node names
231 public final function getSubNodes () {
232 return $this->subNodes;
236 * Handles the start element of an XML resource
238 * @param $resource XML parser resource (currently ignored)
239 * @param $element The element we shall handle
240 * @param $attributes All attributes
242 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
244 public function startElement ($resource, $element, array $attributes) {
245 // Initial method name which will never be called...
246 $methodName = 'initMenu';
248 // Make the element name lower-case
249 $element = strtolower($element);
251 // Is the element a main node?
252 //* DEBUG: */ echo "START: >".$element."<<br />\n";
253 if (in_array($element, $this->getMainNodes())) {
254 // Okay, main node found!
255 $methodName = 'start' . self::convertToClassName($element);
258 $this->setCurrMainNode($element);
259 } elseif (in_array($element, $this->getSubNodes())) {
261 $methodName = 'start' . self::convertToClassName($element);
262 } elseif ($element != 'menu') {
263 // Invalid node name found
264 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
268 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
269 call_user_func_array(array($this, $methodName), $attributes);
273 * Ends the main or sub node by sending out the gathered data
275 * @param $resource An XML resource pointer (currently ignored)
276 * @param $nodeName Name of the node we want to finish
278 * @throws XmlNodeMismatchException If current main node mismatches the closing one
280 public function finishElement ($resource, $nodeName) {
281 // Make all lower-case
282 $nodeName = strtolower($nodeName);
284 // Does this match with current main node?
285 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
286 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
288 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
291 // Construct method name
292 $methodName = 'finish' . self::convertToClassName($nodeName);
294 // Call the corresponding method
295 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
296 call_user_func_array(array($this, $methodName), array());
302 * @param $resource XML parser resource (currently ignored)
303 * @param $characters Characters to handle
305 * @todo Find something useful with this!
307 public function characterHandler ($resource, $characters) {
308 // Trim all spaces away
309 $characters = trim($characters);
311 // Is this string empty?
312 if (empty($characters)) {
313 // Then skip it silently
317 // Assign the found characters to variable and use the last entry from
319 parent::assignVariable($this->getStackInstance()->getNamed('current_node'), $characters);
323 * Handles the template dependency for given node
325 * @param $node The node we should load a dependency template
326 * @param $templateDependency A template to load to satisfy dependencies
329 private function handleTemplateDependency ($node, $templateDependency) {
330 // Is the template dependency set?
331 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
332 // Get a temporay menu template instance
333 $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getMenuInstance()));
336 $templateInstance->loadMenuTemplate($templateDependency);
338 // Parse the XML content
339 $templateInstance->renderXmlContent();
341 // Save the parsed raw content in our dependency array
342 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
347 * Intializes the menu
349 * @param $templateDependency A template to load to satisfy dependencies
351 * @todo Add cache creation here
353 private function initMenu ($templateDependency = '') {
354 // Get web template engine
355 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('html_template_class', array($this->getApplicationInstance())));
357 // Handle the dependency template
358 $this->handleTemplateDependency('menu', $templateDependency);
360 // Push the node name on the stacker
361 $this->getStackInstance()->pushNamed('current_node', 'menu');
365 * Starts the menu entries
367 * @param $templateDependency A template to load to satisfy dependencies
370 private function startEntryList () {
371 // Push the node name on the stacker
372 $this->getStackInstance()->pushNamed('current_node', 'entry-list');
376 * Starts the menu block header
380 private function startBlockHeader () {
381 // Push the node name on the stacker
382 $this->getStackInstance()->pushNamed('current_node', 'block-header');
386 * Starts the menu block footer
390 private function startBlockFooter () {
391 // Push the node name on the stacker
392 $this->getStackInstance()->pushNamed('current_node', 'block-footer');
396 * Starts the menu property 'block-list'
400 private function startBlockList () {
401 // Push the node name on the stacker
402 $this->getStackInstance()->pushNamed('current_node', 'block-list');
406 * Starts the menu property 'block'
410 private function startBlock () {
411 // Push the node name on the stacker
412 $this->getStackInstance()->pushNamed('current_node', 'block');
416 * Starts the menu property 'title'
420 private function startTitle () {
421 // Push the node name on the stacker
422 $this->getStackInstance()->pushNamed('current_node', 'title');
426 * Starts the menu property 'title-id'
430 private function startTitleId () {
431 // Push the node name on the stacker
432 $this->getStackInstance()->pushNamed('current_node', 'title-id');
436 * Starts the menu property 'title-class'
440 private function startTitleClass () {
441 // Push the node name on the stacker
442 $this->getStackInstance()->pushNamed('current_node', 'title-class');
446 * Starts the menu property 'title-text'
450 private function startTitleText () {
451 // Push the node name on the stacker
452 $this->getStackInstance()->pushNamed('current_node', 'title-text');
456 * Starts the menu property 'entry'
460 private function startEntry () {
461 // Push the node name on the stacker
462 $this->getStackInstance()->pushNamed('current_node', 'entry');
466 * Starts the menu property 'entry-id'
470 private function startEntryId () {
471 // Push the node name on the stacker
472 $this->getStackInstance()->pushNamed('current_node', 'entry-id');
476 * Starts the menu property 'anchor'
480 private function startAnchor () {
481 // Push the node name on the stacker
482 $this->getStackInstance()->pushNamed('current_node', 'anchor');
486 * Starts the menu property 'anchor-id'
490 private function startAnchorId () {
491 // Push the node name on the stacker
492 $this->getStackInstance()->pushNamed('current_node', 'anchor-id');
496 * Starts the menu property 'anchor-text'
500 private function startAnchorText () {
501 // Push the node name on the stacker
502 $this->getStackInstance()->pushNamed('current_node', 'anchor-text');
506 * Starts the menu property 'anchor-title'
510 private function startAnchorTitle () {
511 // Push the node name on the stacker
512 $this->getStackInstance()->pushNamed('current_node', 'anchor-title');
516 * Starts the menu property 'anchor-href'
520 private function startAnchorHref () {
521 // Push the node name on the stacker
522 $this->getStackInstance()->pushNamed('current_node', 'anchor-href');
526 * Starts the menu property 'footer-id'
530 private function startFooterId () {
531 // Push the node name on the stacker
532 $this->getStackInstance()->pushNamed('current_node', 'footer-id');
536 * Starts the menu property 'footer-class'
540 private function startFooterClass () {
541 // Push the node name on the stacker
542 $this->getStackInstance()->pushNamed('current_node', 'footer-class');
546 * Starts the menu property 'footer-text'
550 private function startFooterText () {
551 // Push the node name on the stacker
552 $this->getStackInstance()->pushNamed('current_node', 'footer-text');
556 * Finishes the title node by added another template to the menu
560 private function finishTitle () {
561 // Pop the last entry
562 $this->getStackInstance()->popNamed('current_node');
566 * Finishes the title-id node by
570 private function finishTitleId () {
571 // Pop the last entry
572 $this->getStackInstance()->popNamed('current_node');
576 * Finishes the title-class node
580 private function finishTitleClass () {
581 // Pop the last entry
582 $this->getStackInstance()->popNamed('current_node');
586 * Finishes the title-class node
590 private function finishTitleText () {
591 // Pop the last entry
592 $this->getStackInstance()->popNamed('current_node');
596 * Finishes the footer-text node
600 private function finishFooterText () {
601 // Pop the last entry
602 $this->getStackInstance()->popNamed('current_node');
606 * Finishes the footer-class node
610 private function finishFooterClass () {
611 // Pop the last entry
612 $this->getStackInstance()->popNamed('current_node');
616 * Finishes the footer-id node
620 private function finishFooterId () {
621 // Pop the last entry
622 $this->getStackInstance()->popNamed('current_node');
626 * Finishes the anchor-href node
630 private function finishAnchorHref () {
631 // Pop the last entry
632 $this->getStackInstance()->popNamed('current_node');
636 * Finishes the anchor-title node
640 private function finishAnchorTitle () {
641 // Pop the last entry
642 $this->getStackInstance()->popNamed('current_node');
646 * Finishes the anchor-text node
650 private function finishAnchorText () {
651 // Pop the last entry
652 $this->getStackInstance()->popNamed('current_node');
656 * Finishes the anchor-id node
660 private function finishAnchorId () {
661 // Pop the last entry
662 $this->getStackInstance()->popNamed('current_node');
666 * Finishes the anchor node
670 private function finishAnchor () {
671 // Pop the last entry
672 $this->getStackInstance()->popNamed('current_node');
676 * Finishes the entry-id node
680 private function finishEntryId () {
681 // Pop the last entry
682 $this->getStackInstance()->popNamed('current_node');
686 * Finishes the entry node
690 private function finishEntry () {
691 // Pop the last entry
692 $this->getStackInstance()->popNamed('current_node');
694 // Render this menu entry
695 $this->renderMenuEntry();
699 * Finishes the block node
703 private function finishBlock () {
704 // Pop the last entry
705 $this->getStackInstance()->popNamed('current_node');
707 // Render this menu block
708 $this->renderMenuBlock();
712 * Finishes the block-list node
716 private function finishBlockList () {
717 // Pop the last entry
718 $this->getStackInstance()->popNamed('current_node');
722 * Finishes the menu entries
726 private function finishEntryList () {
727 // Pop the last entry
728 $this->getStackInstance()->popNamed('current_node');
732 * Finishes the menu block header
736 private function finishBlockHeader () {
737 // Pop the last entry
738 $this->getStackInstance()->popNamed('current_node');
742 * Finishes the menu block footer
746 private function finishBlockFooter () {
747 // Pop the last entry
748 $this->getStackInstance()->popNamed('current_node');
756 private function finishMenu () {
757 // Pop the last entry
758 $this->getStackInstance()->popNamed('current_node');
762 * Renders this menu entry, as every block all variables got overwritten
763 * with data from next entry.
767 private function renderMenuEntry () {
768 // Prepare template engine
769 $templateInstance = $this->prepareTemplateInstance();
771 // Load menu entry template
772 $templateInstance->loadCodeTemplate('menu_entry');
774 // Copy all variables over to it
775 foreach ($this->menuEntryVariables as $variableName) {
777 $variableValue = $this->readVariable($variableName);
779 // Is the key 'anchor-href'?
780 if ($variableName == 'anchor-href') {
781 // Expand variable with URL then
782 $variableValue = '{?base_url?}/' . $variableValue;
785 // ... into the instance
786 $templateInstance->assignVariable($variableName, $variableValue);
789 // Compile template + variables
790 $templateInstance->compileTemplate();
791 $templateInstance->compileVariables();
794 $this->menuEntries[$this->readVariable('entry_id')] = $templateInstance->getRawTemplateData();
798 * Renders this menu block, as next block all data is overwritten with
803 private function renderMenuBlock () {
804 // Init block content
805 $blockContent = implode('', $this->menuEntries);
807 // Prepare template engine
808 $templateInstance = $this->prepareTemplateInstance();
810 // Load menu entry template
811 $templateInstance->loadCodeTemplate('menu_block');
813 // Copy all variables over to it
814 foreach ($this->menuBlockVariables as $variableName) {
816 $variableValue = $this->readVariable($variableName);
818 // ... into the instance
819 $templateInstance->assignVariable($variableName, $variableValue);
822 // Assign block content
823 $templateInstance->assignVariable('block_content', $blockContent);
825 // Compile template + variables
826 $templateInstance->compileTemplate();
827 $templateInstance->compileVariables();
830 array_push($this->menuBlocks, $templateInstance->getRawTemplateData());
832 // Reset rendered menu entries array
833 $this->menuEntries = array();
837 * "Getter" for menu content
839 * @return $menuContent Returned menu content
841 public function getMenuContent () {
842 // Implode menuBlocks
843 $menuContent = implode('', $this->menuBlocks);
846 $this->menuBlocks = array();
853 * Getter for menu cache file (FQFN)
855 * @return $fqfn Full-qualified file name of the menu cache
857 public function getMenuCacheFqfn () {
858 // Get the FQFN ready
859 $fqfn = sprintf('%s%s%s/%s.%s',
860 $this->getConfigInstance()->getConfigEntry('base_path'),
861 $this->getGenericBasePath(),
864 $this->getMenuInstance()->getMenuName() . ':' .
865 $this->__toString() . ':' .
866 $this->getMenuInstance()->__toString()
868 $this->getMenuInstance()->getMenuType()