3 namespace CoreFramework\Template\Engine;
5 // Import framework stuff
6 use CoreFramework\Template\CompileableTemplate;
9 * A Menu template engine class
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
32 * Main nodes in the XML tree ('menu' is ignored)
34 private $mainNodes = array(
39 * Sub nodes in the XML tree
41 private $subNodes = array(
67 * Variables for a menu entry
69 private $menuEntryVariables = array(
80 * Variables for a menu block
82 private $menuBlockVariables = array(
87 // Content is taken from menuEntries
95 * Rendered menu entries
97 private $menuEntries = array();
100 * Rendered menu blocks
102 private $menuBlocks = array();
107 private $menuInstance = NULL;
112 private $curr = array();
115 * Content from dependency
117 private $dependencyContent = array();
120 * Protected constructor
124 protected function __construct () {
125 // Call parent constructor
126 parent::__construct(__CLASS__);
130 * Creates an instance of the class TemplateEngine and prepares it for usage
132 * @param $menuInstance A RenderableMenu instance
133 * @return $templateInstance An instance of TemplateEngine
134 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
135 * @throws InvalidBasePathStringException If $templateBasePath is no string
136 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
137 * directory or not found
138 * @throws BasePathReadProtectedException If $templateBasePath is
141 public static final function createMenuTemplateEngine (RenderableMenu $menuInstance) {
142 // Get a new instance
143 $templateInstance = new MenuTemplateEngine();
145 // Get the application instance from registry
146 $applicationInstance = Registry::getRegistry()->getInstance('app');
148 // Determine base path
149 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
151 // Is the base path valid?
152 if (empty($templateBasePath)) {
153 // Base path is empty
154 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
155 } elseif (!is_string($templateBasePath)) {
157 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
158 } elseif (!is_dir($templateBasePath)) {
160 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
161 } elseif (!is_readable($templateBasePath)) {
163 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
167 $templateInstance->setTemplateBasePath($templateBasePath);
169 // Set template extensions
170 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
171 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('menu_template_extension'));
173 // Absolute output path for compiled templates
174 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
176 // Set the menu instance
177 $templateInstance->setMenuInstance($menuInstance);
179 // Init a variable stacker
180 $stackInstance = ObjectFactory::createObjectByConfiguredName('menu_stacker_class');
183 $templateInstance->setStackInstance($stackInstance);
185 // Return the prepared instance
186 return $templateInstance;
190 * Load a specified menu template into the engine
192 * @param $template The menu template we shall load which is
193 * located in 'menu' by default
196 public function loadMenuTemplate ($template) {
198 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
200 // Load the special template
201 $this->loadTemplate($template);
205 * Getter for current main node
207 * @return $currMainNode Current main node
209 public final function getCurrMainNode () {
210 return $this->curr['main_node'];
214 * Setter for current main node
216 * @param $element Element name to set as current main node
217 * @return $currMainNode Current main node
219 private final function setCurrMainNode ($element) {
220 $this->curr['main_node'] = (string) $element;
224 * Getter for main node array
226 * @return $mainNodes Array with valid main node names
228 public final function getMainNodes () {
229 return $this->mainNodes;
233 * Getter for sub node array
235 * @return $subNodes Array with valid sub node names
237 public final function getSubNodes () {
238 return $this->subNodes;
242 * Handles the start element of an XML resource
244 * @param $resource XML parser resource (currently ignored)
245 * @param $element The element we shall handle
246 * @param $attributes All attributes
248 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
250 public function startElement ($resource, $element, array $attributes) {
251 // Initial method name which will never be called...
252 $methodName = 'initMenu';
254 // Make the element name lower-case
255 $element = strtolower($element);
257 // Is the element a main node?
258 //* DEBUG: */ echo "START: >".$element."<<br />\n";
259 if (in_array($element, $this->getMainNodes())) {
260 // Okay, main node found!
261 $methodName = 'start' . self::convertToClassName($element);
264 $this->setCurrMainNode($element);
265 } elseif (in_array($element, $this->getSubNodes())) {
267 $methodName = 'start' . self::convertToClassName($element);
268 } elseif ($element != 'menu') {
269 // Invalid node name found
270 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
274 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
275 call_user_func_array(array($this, $methodName), $attributes);
279 * Ends the main or sub node by sending out the gathered data
281 * @param $resource An XML resource pointer (currently ignored)
282 * @param $nodeName Name of the node we want to finish
284 * @throws XmlNodeMismatchException If current main node mismatches the closing one
286 public function finishElement ($resource, $nodeName) {
287 // Make all lower-case
288 $nodeName = strtolower($nodeName);
290 // Does this match with current main node?
291 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
292 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
294 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
297 // Construct method name
298 $methodName = 'finish' . self::convertToClassName($nodeName);
300 // Call the corresponding method
301 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
302 call_user_func_array(array($this, $methodName), array());
308 * @param $resource XML parser resource (currently ignored)
309 * @param $characters Characters to handle
311 * @todo Find something useful with this!
313 public function characterHandler ($resource, $characters) {
314 // Trim all spaces away
315 $characters = trim($characters);
317 // Is this string empty?
318 if (empty($characters)) {
319 // Then skip it silently
323 // Assign the found characters to variable and use the last entry from
325 parent::assignVariable($this->getStackInstance()->getNamed('current_node'), $characters);
329 * Handles the template dependency for given node
331 * @param $node The node we should load a dependency template
332 * @param $templateDependency A template to load to satisfy dependencies
335 private function handleTemplateDependency ($node, $templateDependency) {
336 // Is the template dependency set?
337 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
338 // Get a temporay menu template instance
339 $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getMenuInstance()));
342 $templateInstance->loadMenuTemplate($templateDependency);
344 // Parse the XML content
345 $templateInstance->renderXmlContent();
347 // Save the parsed raw content in our dependency array
348 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
353 * Intializes the menu
355 * @param $templateDependency A template to load to satisfy dependencies
357 * @todo Add cache creation here
359 private function initMenu ($templateDependency = '') {
360 // Get web template engine
361 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('html_template_class', array($this->getApplicationInstance())));
363 // Handle the dependency template
364 $this->handleTemplateDependency('menu', $templateDependency);
366 // Push the node name on the stacker
367 $this->getStackInstance()->pushNamed('current_node', 'menu');
371 * Starts the menu entries
373 * @param $templateDependency A template to load to satisfy dependencies
376 private function startEntryList () {
377 // Push the node name on the stacker
378 $this->getStackInstance()->pushNamed('current_node', 'entry-list');
382 * Starts the menu block header
386 private function startBlockHeader () {
387 // Push the node name on the stacker
388 $this->getStackInstance()->pushNamed('current_node', 'block-header');
392 * Starts the menu block footer
396 private function startBlockFooter () {
397 // Push the node name on the stacker
398 $this->getStackInstance()->pushNamed('current_node', 'block-footer');
402 * Starts the menu property 'block-list'
406 private function startBlockList () {
407 // Push the node name on the stacker
408 $this->getStackInstance()->pushNamed('current_node', 'block-list');
412 * Starts the menu property 'block'
416 private function startBlock () {
417 // Push the node name on the stacker
418 $this->getStackInstance()->pushNamed('current_node', 'block');
422 * Starts the menu property 'title'
426 private function startTitle () {
427 // Push the node name on the stacker
428 $this->getStackInstance()->pushNamed('current_node', 'title');
432 * Starts the menu property 'title-id'
436 private function startTitleId () {
437 // Push the node name on the stacker
438 $this->getStackInstance()->pushNamed('current_node', 'title-id');
442 * Starts the menu property 'title-class'
446 private function startTitleClass () {
447 // Push the node name on the stacker
448 $this->getStackInstance()->pushNamed('current_node', 'title-class');
452 * Starts the menu property 'title-text'
456 private function startTitleText () {
457 // Push the node name on the stacker
458 $this->getStackInstance()->pushNamed('current_node', 'title-text');
462 * Starts the menu property 'entry'
466 private function startEntry () {
467 // Push the node name on the stacker
468 $this->getStackInstance()->pushNamed('current_node', 'entry');
472 * Starts the menu property 'entry-id'
476 private function startEntryId () {
477 // Push the node name on the stacker
478 $this->getStackInstance()->pushNamed('current_node', 'entry-id');
482 * Starts the menu property 'anchor'
486 private function startAnchor () {
487 // Push the node name on the stacker
488 $this->getStackInstance()->pushNamed('current_node', 'anchor');
492 * Starts the menu property 'anchor-id'
496 private function startAnchorId () {
497 // Push the node name on the stacker
498 $this->getStackInstance()->pushNamed('current_node', 'anchor-id');
502 * Starts the menu property 'anchor-text'
506 private function startAnchorText () {
507 // Push the node name on the stacker
508 $this->getStackInstance()->pushNamed('current_node', 'anchor-text');
512 * Starts the menu property 'anchor-title'
516 private function startAnchorTitle () {
517 // Push the node name on the stacker
518 $this->getStackInstance()->pushNamed('current_node', 'anchor-title');
522 * Starts the menu property 'anchor-href'
526 private function startAnchorHref () {
527 // Push the node name on the stacker
528 $this->getStackInstance()->pushNamed('current_node', 'anchor-href');
532 * Starts the menu property 'footer-id'
536 private function startFooterId () {
537 // Push the node name on the stacker
538 $this->getStackInstance()->pushNamed('current_node', 'footer-id');
542 * Starts the menu property 'footer-class'
546 private function startFooterClass () {
547 // Push the node name on the stacker
548 $this->getStackInstance()->pushNamed('current_node', 'footer-class');
552 * Starts the menu property 'footer-text'
556 private function startFooterText () {
557 // Push the node name on the stacker
558 $this->getStackInstance()->pushNamed('current_node', 'footer-text');
562 * Finishes the title node by added another template to the menu
566 private function finishTitle () {
567 // Pop the last entry
568 $this->getStackInstance()->popNamed('current_node');
572 * Finishes the title-id node by
576 private function finishTitleId () {
577 // Pop the last entry
578 $this->getStackInstance()->popNamed('current_node');
582 * Finishes the title-class node
586 private function finishTitleClass () {
587 // Pop the last entry
588 $this->getStackInstance()->popNamed('current_node');
592 * Finishes the title-class node
596 private function finishTitleText () {
597 // Pop the last entry
598 $this->getStackInstance()->popNamed('current_node');
602 * Finishes the footer-text node
606 private function finishFooterText () {
607 // Pop the last entry
608 $this->getStackInstance()->popNamed('current_node');
612 * Finishes the footer-class node
616 private function finishFooterClass () {
617 // Pop the last entry
618 $this->getStackInstance()->popNamed('current_node');
622 * Finishes the footer-id node
626 private function finishFooterId () {
627 // Pop the last entry
628 $this->getStackInstance()->popNamed('current_node');
632 * Finishes the anchor-href node
636 private function finishAnchorHref () {
637 // Pop the last entry
638 $this->getStackInstance()->popNamed('current_node');
642 * Finishes the anchor-title node
646 private function finishAnchorTitle () {
647 // Pop the last entry
648 $this->getStackInstance()->popNamed('current_node');
652 * Finishes the anchor-text node
656 private function finishAnchorText () {
657 // Pop the last entry
658 $this->getStackInstance()->popNamed('current_node');
662 * Finishes the anchor-id node
666 private function finishAnchorId () {
667 // Pop the last entry
668 $this->getStackInstance()->popNamed('current_node');
672 * Finishes the anchor node
676 private function finishAnchor () {
677 // Pop the last entry
678 $this->getStackInstance()->popNamed('current_node');
682 * Finishes the entry-id node
686 private function finishEntryId () {
687 // Pop the last entry
688 $this->getStackInstance()->popNamed('current_node');
692 * Finishes the entry node
696 private function finishEntry () {
697 // Pop the last entry
698 $this->getStackInstance()->popNamed('current_node');
700 // Render this menu entry
701 $this->renderMenuEntry();
705 * Finishes the block node
709 private function finishBlock () {
710 // Pop the last entry
711 $this->getStackInstance()->popNamed('current_node');
713 // Render this menu block
714 $this->renderMenuBlock();
718 * Finishes the block-list node
722 private function finishBlockList () {
723 // Pop the last entry
724 $this->getStackInstance()->popNamed('current_node');
728 * Finishes the menu entries
732 private function finishEntryList () {
733 // Pop the last entry
734 $this->getStackInstance()->popNamed('current_node');
738 * Finishes the menu block header
742 private function finishBlockHeader () {
743 // Pop the last entry
744 $this->getStackInstance()->popNamed('current_node');
748 * Finishes the menu block footer
752 private function finishBlockFooter () {
753 // Pop the last entry
754 $this->getStackInstance()->popNamed('current_node');
762 private function finishMenu () {
763 // Pop the last entry
764 $this->getStackInstance()->popNamed('current_node');
768 * Renders this menu entry, as every block all variables got overwritten
769 * with data from next entry.
773 private function renderMenuEntry () {
774 // Prepare template engine
775 $templateInstance = $this->prepareTemplateInstance();
777 // Load menu entry template
778 $templateInstance->loadCodeTemplate('menu_entry');
780 // Copy all variables over to it
781 foreach ($this->menuEntryVariables as $variableName) {
783 $variableValue = $this->readVariable($variableName);
785 // Is the key 'anchor-href'?
786 if ($variableName == 'anchor-href') {
787 // Expand variable with URL then
788 $variableValue = '{?base_url?}/' . $variableValue;
791 // ... into the instance
792 $templateInstance->assignVariable($variableName, $variableValue);
795 // Compile template + variables
796 $templateInstance->compileTemplate();
797 $templateInstance->compileVariables();
800 $this->menuEntries[$this->readVariable('entry_id')] = $templateInstance->getRawTemplateData();
804 * Renders this menu block, as next block all data is overwritten with
809 private function renderMenuBlock () {
810 // Init block content
811 $blockContent = implode('', $this->menuEntries);
813 // Prepare template engine
814 $templateInstance = $this->prepareTemplateInstance();
816 // Load menu entry template
817 $templateInstance->loadCodeTemplate('menu_block');
819 // Copy all variables over to it
820 foreach ($this->menuBlockVariables as $variableName) {
822 $variableValue = $this->readVariable($variableName);
824 // ... into the instance
825 $templateInstance->assignVariable($variableName, $variableValue);
828 // Assign block content
829 $templateInstance->assignVariable('block_content', $blockContent);
831 // Compile template + variables
832 $templateInstance->compileTemplate();
833 $templateInstance->compileVariables();
836 array_push($this->menuBlocks, $templateInstance->getRawTemplateData());
838 // Reset rendered menu entries array
839 $this->menuEntries = array();
843 * "Getter" for menu content
845 * @return $menuContent Returned menu content
847 public function getMenuContent () {
848 // Implode menuBlocks
849 $menuContent = implode('', $this->menuBlocks);
852 $this->menuBlocks = array();
859 * Getter for menu cache file (FQFN)
861 * @return $fqfn Full-qualified file name of the menu cache
863 public function getMenuCacheFqfn () {
864 // Get the FQFN ready
865 $fqfn = sprintf('%s%s%s/%s.%s',
866 $this->getConfigInstance()->getConfigEntry('base_path'),
867 $this->getGenericBasePath(),
870 $this->getMenuInstance()->getMenuName() . ':' .
871 $this->__toString() . ':' .
872 $this->getMenuInstance()->__toString()
874 $this->getMenuInstance()->getMenuType()