3 namespace Org\Mxchange\CoreFramework\Template\Engine;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Filesystem\InvalidDirectoryException;
8 use Org\Mxchange\CoreFramework\Parser\Xml\XmlParser;
9 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
10 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
11 use Org\Mxchange\CoreFramework\Template\Engine\BaseTemplateEngine;
15 use \UnexpectedValueException;
18 * A Menu template engine class
20 * @author Roland Haeder <webmaster@shipsimu.org>
22 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.shipsimu.org
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
41 * Main nodes in the XML tree ('menu' is ignored)
43 private $mainNodes = array(
48 * Sub nodes in the XML tree
50 private $subNodes = array(
76 * Variables for a menu entry
78 private $menuEntryVariables = array(
89 * Variables for a menu block
91 private $menuBlockVariables = array(
96 // Content is taken from menuEntries
104 * Rendered menu entries
106 private $menuEntries = array();
109 * Rendered menu blocks
111 private $menuBlocks = array();
116 private $menuInstance = NULL;
121 private $curr = array();
124 * Content from dependency
126 private $dependencyContent = array();
129 * Protected constructor
133 protected function __construct () {
134 // Call parent constructor
135 parent::__construct(__CLASS__);
139 * Creates an instance of the class TemplateEngine and prepares it for usage
141 * @param $menuInstance A RenderableMenu instance
142 * @return $templateInstance An instance of TemplateEngine
143 * @throws UnexpectedValueException If the found $templateBasePath is empty or not a string
144 * @throws InvalidDirectoryException If $templateBasePath is no directory or not found
145 * @throws BasePathReadProtectedException If $templateBasePath is
148 public static final function createMenuTemplateEngine (RenderableMenu $menuInstance) {
149 // Get a new instance
150 $templateInstance = new MenuTemplateEngine();
152 // Get the application instance from registry
153 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
155 // Determine base path
156 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getAppShortName(). '/';
158 // Is the base path valid?
159 if (empty($templateBasePath)) {
160 // Base path is empty
161 throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
162 } elseif (!is_string($templateBasePath)) {
164 throw new UnexpectedValueException(sprintf('[%s:%d] %s is not a string with a base path.', $templateInstance->__toString(), __LINE__, $templateBasePath), self::EXCEPTION_INVALID_STRING);
165 } elseif (!is_dir($templateBasePath)) {
167 throw new InvalidDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
168 } elseif (!is_readable($templateBasePath)) {
170 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
174 $templateInstance->setTemplateBasePath($templateBasePath);
176 // Set template extensions
177 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
178 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('menu_template_extension'));
180 // Absolute output path for compiled templates
181 $templateInstance->setCompileOutputPath(sprintf('%s%s/',
183 $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path')
186 // Set the menu instance
187 $templateInstance->setMenuInstance($menuInstance);
189 // Init a variable stacker
190 $stackInstance = ObjectFactory::createObjectByConfiguredName('menu_stacker_class');
193 $templateInstance->setStackInstance($stackInstance);
195 // Return the prepared instance
196 return $templateInstance;
200 * Load a specified menu template into the engine
202 * @param $template The menu template we shall load which is
203 * located in 'menu' by default
206 public function loadMenuTemplate ($template) {
208 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
210 // Load the special template
211 $this->loadTemplate($template);
215 * Getter for current main node
217 * @return $currMainNode Current main node
219 public final function getCurrMainNode () {
220 return $this->curr['main_node'];
224 * Setter for current main node
226 * @param $element Element name to set as current main node
227 * @return $currMainNode Current main node
229 private final function setCurrMainNode ($element) {
230 $this->curr['main_node'] = (string) $element;
234 * Getter for main node array
236 * @return $mainNodes Array with valid main node names
238 public final function getMainNodes () {
239 return $this->mainNodes;
243 * Getter for sub node array
245 * @return $subNodes Array with valid sub node names
247 public final function getSubNodes () {
248 return $this->subNodes;
252 * Handles the start element of an XML resource
254 * @param $resource XML parser resource (currently ignored)
255 * @param $element The element we shall handle
256 * @param $attributes All attributes
258 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
260 public function startElement ($resource, $element, array $attributes) {
261 // Initial method name which will never be called...
262 $methodName = 'initMenu';
264 // Make the element name lower-case
265 $element = strtolower($element);
267 // Is the element a main node?
268 //* DEBUG: */ echo "START: >".$element."<<br />\n";
269 if (in_array($element, $this->getMainNodes())) {
270 // Okay, main node found!
271 $methodName = 'start' . self::convertToClassName($element);
274 $this->setCurrMainNode($element);
275 } elseif (in_array($element, $this->getSubNodes())) {
277 $methodName = 'start' . self::convertToClassName($element);
278 } elseif ($element != 'menu') {
279 // Invalid node name found
280 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
284 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
285 call_user_func_array(array($this, $methodName), $attributes);
289 * Ends the main or sub node by sending out the gathered data
291 * @param $resource An XML resource pointer (currently ignored)
292 * @param $nodeName Name of the node we want to finish
294 * @throws XmlNodeMismatchException If current main node mismatches the closing one
296 public function finishElement ($resource, $nodeName) {
297 // Make all lower-case
298 $nodeName = strtolower($nodeName);
300 // Does this match with current main node?
301 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
302 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
304 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
307 // Construct method name
308 $methodName = 'finish' . self::convertToClassName($nodeName);
310 // Call the corresponding method
311 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
312 call_user_func_array(array($this, $methodName), array());
318 * @param $resource XML parser resource (currently ignored)
319 * @param $characters Characters to handle
321 * @todo Find something useful with this!
323 public function characterHandler ($resource, $characters) {
324 // Trim all spaces away
325 $characters = trim($characters);
327 // Is this string empty?
328 if (empty($characters)) {
329 // Then skip it silently
333 // Assign the found characters to variable and use the last entry from
335 parent::assignVariable($this->getStackInstance()->getNamed('current_node'), $characters);
339 * Handles the template dependency for given node
341 * @param $node The node we should load a dependency template
342 * @param $templateDependency A template to load to satisfy dependencies
345 private function handleTemplateDependency ($node, $templateDependency) {
346 // Is the template dependency set?
347 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
348 // Get a temporay menu template instance
349 $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getMenuInstance()));
352 $templateInstance->loadMenuTemplate($templateDependency);
354 // Parse the XML content
355 $templateInstance->renderXmlContent();
357 // Save the parsed raw content in our dependency array
358 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
363 * Intializes the menu
365 * @param $templateDependency A template to load to satisfy dependencies
367 * @todo Add cache creation here
369 private function initMenu ($templateDependency = '') {
370 // Get web template engine
371 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('html_template_class', array(GenericRegistry::getRegistry()->getInstance('application'))));
373 // Handle the dependency template
374 $this->handleTemplateDependency('menu', $templateDependency);
376 // Push the node name on the stacker
377 $this->getStackInstance()->pushNamed('current_node', 'menu');
381 * Starts the menu entries
383 * @param $templateDependency A template to load to satisfy dependencies
386 private function startEntryList () {
387 // Push the node name on the stacker
388 $this->getStackInstance()->pushNamed('current_node', 'entry-list');
392 * Starts the menu block header
396 private function startBlockHeader () {
397 // Push the node name on the stacker
398 $this->getStackInstance()->pushNamed('current_node', 'block-header');
402 * Starts the menu block footer
406 private function startBlockFooter () {
407 // Push the node name on the stacker
408 $this->getStackInstance()->pushNamed('current_node', 'block-footer');
412 * Starts the menu property 'block-list'
416 private function startBlockList () {
417 // Push the node name on the stacker
418 $this->getStackInstance()->pushNamed('current_node', 'block-list');
422 * Starts the menu property 'block'
426 private function startBlock () {
427 // Push the node name on the stacker
428 $this->getStackInstance()->pushNamed('current_node', 'block');
432 * Starts the menu property 'title'
436 private function startTitle () {
437 // Push the node name on the stacker
438 $this->getStackInstance()->pushNamed('current_node', 'title');
442 * Starts the menu property 'title-id'
446 private function startTitleId () {
447 // Push the node name on the stacker
448 $this->getStackInstance()->pushNamed('current_node', 'title-id');
452 * Starts the menu property 'title-class'
456 private function startTitleClass () {
457 // Push the node name on the stacker
458 $this->getStackInstance()->pushNamed('current_node', 'title-class');
462 * Starts the menu property 'title-text'
466 private function startTitleText () {
467 // Push the node name on the stacker
468 $this->getStackInstance()->pushNamed('current_node', 'title-text');
472 * Starts the menu property 'entry'
476 private function startEntry () {
477 // Push the node name on the stacker
478 $this->getStackInstance()->pushNamed('current_node', 'entry');
482 * Starts the menu property 'entry-id'
486 private function startEntryId () {
487 // Push the node name on the stacker
488 $this->getStackInstance()->pushNamed('current_node', 'entry-id');
492 * Starts the menu property 'anchor'
496 private function startAnchor () {
497 // Push the node name on the stacker
498 $this->getStackInstance()->pushNamed('current_node', 'anchor');
502 * Starts the menu property 'anchor-id'
506 private function startAnchorId () {
507 // Push the node name on the stacker
508 $this->getStackInstance()->pushNamed('current_node', 'anchor-id');
512 * Starts the menu property 'anchor-text'
516 private function startAnchorText () {
517 // Push the node name on the stacker
518 $this->getStackInstance()->pushNamed('current_node', 'anchor-text');
522 * Starts the menu property 'anchor-title'
526 private function startAnchorTitle () {
527 // Push the node name on the stacker
528 $this->getStackInstance()->pushNamed('current_node', 'anchor-title');
532 * Starts the menu property 'anchor-href'
536 private function startAnchorHref () {
537 // Push the node name on the stacker
538 $this->getStackInstance()->pushNamed('current_node', 'anchor-href');
542 * Starts the menu property 'footer-id'
546 private function startFooterId () {
547 // Push the node name on the stacker
548 $this->getStackInstance()->pushNamed('current_node', 'footer-id');
552 * Starts the menu property 'footer-class'
556 private function startFooterClass () {
557 // Push the node name on the stacker
558 $this->getStackInstance()->pushNamed('current_node', 'footer-class');
562 * Starts the menu property 'footer-text'
566 private function startFooterText () {
567 // Push the node name on the stacker
568 $this->getStackInstance()->pushNamed('current_node', 'footer-text');
572 * Finishes the title node by added another template to the menu
576 private function finishTitle () {
577 // Pop the last entry
578 $this->getStackInstance()->popNamed('current_node');
582 * Finishes the title-id node by
586 private function finishTitleId () {
587 // Pop the last entry
588 $this->getStackInstance()->popNamed('current_node');
592 * Finishes the title-class node
596 private function finishTitleClass () {
597 // Pop the last entry
598 $this->getStackInstance()->popNamed('current_node');
602 * Finishes the title-class node
606 private function finishTitleText () {
607 // Pop the last entry
608 $this->getStackInstance()->popNamed('current_node');
612 * Finishes the footer-text node
616 private function finishFooterText () {
617 // Pop the last entry
618 $this->getStackInstance()->popNamed('current_node');
622 * Finishes the footer-class node
626 private function finishFooterClass () {
627 // Pop the last entry
628 $this->getStackInstance()->popNamed('current_node');
632 * Finishes the footer-id node
636 private function finishFooterId () {
637 // Pop the last entry
638 $this->getStackInstance()->popNamed('current_node');
642 * Finishes the anchor-href node
646 private function finishAnchorHref () {
647 // Pop the last entry
648 $this->getStackInstance()->popNamed('current_node');
652 * Finishes the anchor-title node
656 private function finishAnchorTitle () {
657 // Pop the last entry
658 $this->getStackInstance()->popNamed('current_node');
662 * Finishes the anchor-text node
666 private function finishAnchorText () {
667 // Pop the last entry
668 $this->getStackInstance()->popNamed('current_node');
672 * Finishes the anchor-id node
676 private function finishAnchorId () {
677 // Pop the last entry
678 $this->getStackInstance()->popNamed('current_node');
682 * Finishes the anchor node
686 private function finishAnchor () {
687 // Pop the last entry
688 $this->getStackInstance()->popNamed('current_node');
692 * Finishes the entry-id node
696 private function finishEntryId () {
697 // Pop the last entry
698 $this->getStackInstance()->popNamed('current_node');
702 * Finishes the entry node
706 private function finishEntry () {
707 // Pop the last entry
708 $this->getStackInstance()->popNamed('current_node');
710 // Render this menu entry
711 $this->renderMenuEntry();
715 * Finishes the block node
719 private function finishBlock () {
720 // Pop the last entry
721 $this->getStackInstance()->popNamed('current_node');
723 // Render this menu block
724 $this->renderMenuBlock();
728 * Finishes the block-list node
732 private function finishBlockList () {
733 // Pop the last entry
734 $this->getStackInstance()->popNamed('current_node');
738 * Finishes the menu entries
742 private function finishEntryList () {
743 // Pop the last entry
744 $this->getStackInstance()->popNamed('current_node');
748 * Finishes the menu block header
752 private function finishBlockHeader () {
753 // Pop the last entry
754 $this->getStackInstance()->popNamed('current_node');
758 * Finishes the menu block footer
762 private function finishBlockFooter () {
763 // Pop the last entry
764 $this->getStackInstance()->popNamed('current_node');
772 private function finishMenu () {
773 // Pop the last entry
774 $this->getStackInstance()->popNamed('current_node');
778 * Renders this menu entry, as every block all variables got overwritten
779 * with data from next entry.
783 private function renderMenuEntry () {
784 // Prepare template engine
785 $templateInstance = $this->prepareTemplateInstance();
787 // Load menu entry template
788 $templateInstance->loadCodeTemplate('menu_entry');
790 // Copy all variables over to it
791 foreach ($this->menuEntryVariables as $variableName) {
793 $variableValue = $this->readVariable($variableName);
795 // Is the key 'anchor-href'?
796 if ($variableName == 'anchor-href') {
797 // Expand variable with URL then
798 $variableValue = '{?base_url?}/' . $variableValue;
801 // ... into the instance
802 $templateInstance->assignVariable($variableName, $variableValue);
805 // Compile template + variables
806 $templateInstance->compileTemplate();
807 $templateInstance->compileVariables();
810 $this->menuEntries[$this->readVariable('entry_id')] = $templateInstance->getRawTemplateData();
814 * Renders this menu block, as next block all data is overwritten with
819 private function renderMenuBlock () {
820 // Init block content
821 $blockContent = implode('', $this->menuEntries);
823 // Prepare template engine
824 $templateInstance = $this->prepareTemplateInstance();
826 // Load menu entry template
827 $templateInstance->loadCodeTemplate('menu_block');
829 // Copy all variables over to it
830 foreach ($this->menuBlockVariables as $variableName) {
832 $variableValue = $this->readVariable($variableName);
834 // ... into the instance
835 $templateInstance->assignVariable($variableName, $variableValue);
838 // Assign block content
839 $templateInstance->assignVariable('block_content', $blockContent);
841 // Compile template + variables
842 $templateInstance->compileTemplate();
843 $templateInstance->compileVariables();
846 array_push($this->menuBlocks, $templateInstance->getRawTemplateData());
848 // Reset rendered menu entries array
849 $this->menuEntries = array();
853 * "Getter" for menu content
855 * @return $menuContent Returned menu content
857 public function getMenuContent () {
858 // Implode menuBlocks
859 $menuContent = implode('', $this->menuBlocks);
862 $this->menuBlocks = array();
869 * Getter for menu cache file instance
871 * @return $fileInstance Full-qualified file name of the menu cache
873 public function getMenuCacheFile () {
874 // Get the application instance from registry
875 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
877 // Get the file instance ready
878 $fileInstance = new SplFileInfo(sprintf('%s%smenus/_cache/%s.%s',
879 $this->getConfigInstance()->getConfigEntry('application_base_path'),
880 $applicationInstance->getAppShortName(),
882 $this->getMenuInstance()->getMenuName() . ':' .
883 $this->__toString() . ':' .
884 $this->getMenuInstance()->__toString()
886 $this->getMenuInstance()->getMenuType()
890 return $fileInstance;