3 namespace Org\Mxchange\CoreFramework\Template\Engine;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Filesystem\InvalidDirectoryException;
9 use Org\Mxchange\CoreFramework\Menu\RenderableMenu;
10 use Org\Mxchange\CoreFramework\Parser\Xml\XmlParser;
11 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
12 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
13 use Org\Mxchange\CoreFramework\Template\Engine\BaseTemplateEngine;
17 use \UnexpectedValueException;
20 * A Menu template engine class
22 * @author Roland Haeder <webmaster@shipsimu.org>
24 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
25 * @license GNU GPL 3.0 or any newer version
26 * @link http://www.shipsimu.org
28 * This program is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, either version 3 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program. If not, see <http://www.gnu.org/licenses/>.
41 class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
43 * Main nodes in the XML tree ('menu' is ignored)
45 private $mainNodes = array(
50 * Sub nodes in the XML tree
52 private $subNodes = array(
78 * Variables for a menu entry
80 private $menuEntryVariables = array(
91 * Variables for a menu block
93 private $menuBlockVariables = array(
98 // Content is taken from menuEntries
106 * Rendered menu entries
108 private $menuEntries = array();
111 * Rendered menu blocks
113 private $menuBlocks = array();
118 private $menuInstance = NULL;
123 private $curr = array();
126 * Content from dependency
128 private $dependencyContent = array();
131 * Protected constructor
135 protected function __construct () {
136 // Call parent constructor
137 parent::__construct(__CLASS__);
141 * Creates an instance of the class TemplateEngine and prepares it for usage
143 * @param $menuInstance A RenderableMenu instance
144 * @return $templateInstance An instance of TemplateEngine
145 * @throws UnexpectedValueException If the found $templateBasePath is empty or not a string
146 * @throws InvalidDirectoryException If $templateBasePath is no directory or not found
147 * @throws BasePathReadProtectedException If $templateBasePath is
150 public static final function createMenuTemplateEngine (RenderableMenu $menuInstance) {
151 // Get a new instance
152 $templateInstance = new MenuTemplateEngine();
154 // Get the application instance from registry
155 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
157 // Determine base path
158 $templateBasePath = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('application_base_path') . $applicationInstance->getAppShortName(). '/';
160 // Is the base path valid?
161 if (empty($templateBasePath)) {
162 // Base path is empty
163 throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
164 } elseif (!is_string($templateBasePath)) {
166 throw new UnexpectedValueException(sprintf('[%s:%d] %s is not a string with a base path.', $templateInstance->__toString(), __LINE__, $templateBasePath), self::EXCEPTION_INVALID_STRING);
167 } elseif (!is_dir($templateBasePath)) {
169 throw new InvalidDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
170 } elseif (!is_readable($templateBasePath)) {
172 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
176 $templateInstance->setTemplateBasePath($templateBasePath);
178 // Set template extensions
179 $templateInstance->setRawTemplateExtension(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('raw_template_extension'));
180 $templateInstance->setCodeTemplateExtension(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('menu_template_extension'));
182 // Absolute output path for compiled templates
183 $templateInstance->setCompileOutputPath(sprintf('%s%s/',
185 FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('compile_output_path')
188 // Set the menu instance
189 $templateInstance->setMenuInstance($menuInstance);
191 // Init a variable stacker
192 $stackInstance = ObjectFactory::createObjectByConfiguredName('menu_stacker_class');
195 $templateInstance->setStackInstance($stackInstance);
197 // Return the prepared instance
198 return $templateInstance;
202 * Load a specified menu template into the engine
204 * @param $template The menu template we shall load which is
205 * located in 'menu' by default
208 public function loadMenuTemplate ($template) {
210 $this->setTemplateType(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('menu_template_type'));
212 // Load the special template
213 $this->loadTemplate($template);
217 * Getter for current main node
219 * @return $currMainNode Current main node
221 public final function getCurrMainNode () {
222 return $this->curr['main_node'];
226 * Setter for current main node
228 * @param $element Element name to set as current main node
229 * @return $currMainNode Current main node
231 private final function setCurrMainNode ($element) {
232 $this->curr['main_node'] = (string) $element;
236 * Getter for main node array
238 * @return $mainNodes Array with valid main node names
240 public final function getMainNodes () {
241 return $this->mainNodes;
245 * Getter for sub node array
247 * @return $subNodes Array with valid sub node names
249 public final function getSubNodes () {
250 return $this->subNodes;
254 * Handles the start element of an XML resource
256 * @param $resource XML parser resource (currently ignored)
257 * @param $element The element we shall handle
258 * @param $attributes All attributes
260 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
262 public function startElement ($resource, $element, array $attributes) {
263 // Initial method name which will never be called...
264 $methodName = 'initMenu';
266 // Make the element name lower-case
267 $element = strtolower($element);
269 // Is the element a main node?
270 //* DEBUG: */ echo "START: >".$element."<<br />\n";
271 if (in_array($element, $this->getMainNodes())) {
272 // Okay, main node found!
273 $methodName = 'start' . self::convertToClassName($element);
276 $this->setCurrMainNode($element);
277 } elseif (in_array($element, $this->getSubNodes())) {
279 $methodName = 'start' . self::convertToClassName($element);
280 } elseif ($element != 'menu') {
281 // Invalid node name found
282 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
286 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
287 call_user_func_array(array($this, $methodName), $attributes);
291 * Ends the main or sub node by sending out the gathered data
293 * @param $resource An XML resource pointer (currently ignored)
294 * @param $nodeName Name of the node we want to finish
296 * @throws XmlNodeMismatchException If current main node mismatches the closing one
298 public function finishElement ($resource, $nodeName) {
299 // Make all lower-case
300 $nodeName = strtolower($nodeName);
302 // Does this match with current main node?
303 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
304 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
306 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
309 // Construct method name
310 $methodName = 'finish' . self::convertToClassName($nodeName);
312 // Call the corresponding method
313 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
314 call_user_func_array(array($this, $methodName), array());
320 * @param $resource XML parser resource (currently ignored)
321 * @param $characters Characters to handle
323 * @todo Find something useful with this!
325 public function characterHandler ($resource, $characters) {
326 // Trim all spaces away
327 $characters = trim($characters);
329 // Is this string empty?
330 if (empty($characters)) {
331 // Then skip it silently
335 // Assign the found characters to variable and use the last entry from
337 parent::assignVariable($this->getStackInstance()->getNamed('current_node'), $characters);
341 * Handles the template dependency for given node
343 * @param $node The node we should load a dependency template
344 * @param $templateDependency A template to load to satisfy dependencies
347 private function handleTemplateDependency ($node, $templateDependency) {
348 // Is the template dependency set?
349 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
350 // Get a temporay menu template instance
351 $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getMenuInstance()));
354 $templateInstance->loadMenuTemplate($templateDependency);
356 // Parse the XML content
357 $templateInstance->renderXmlContent();
359 // Save the parsed raw content in our dependency array
360 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
365 * Intializes the menu
367 * @param $templateDependency A template to load to satisfy dependencies
369 * @todo Add cache creation here
371 private function initMenu ($templateDependency = '') {
372 // Get web template engine
373 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('html_template_class', array(GenericRegistry::getRegistry()->getInstance('application'))));
375 // Handle the dependency template
376 $this->handleTemplateDependency('menu', $templateDependency);
378 // Push the node name on the stacker
379 $this->getStackInstance()->pushNamed('current_node', 'menu');
383 * Starts the menu entries
385 * @param $templateDependency A template to load to satisfy dependencies
388 private function startEntryList () {
389 // Push the node name on the stacker
390 $this->getStackInstance()->pushNamed('current_node', 'entry-list');
394 * Starts the menu block header
398 private function startBlockHeader () {
399 // Push the node name on the stacker
400 $this->getStackInstance()->pushNamed('current_node', 'block-header');
404 * Starts the menu block footer
408 private function startBlockFooter () {
409 // Push the node name on the stacker
410 $this->getStackInstance()->pushNamed('current_node', 'block-footer');
414 * Starts the menu property 'block-list'
418 private function startBlockList () {
419 // Push the node name on the stacker
420 $this->getStackInstance()->pushNamed('current_node', 'block-list');
424 * Starts the menu property 'block'
428 private function startBlock () {
429 // Push the node name on the stacker
430 $this->getStackInstance()->pushNamed('current_node', 'block');
434 * Starts the menu property 'title'
438 private function startTitle () {
439 // Push the node name on the stacker
440 $this->getStackInstance()->pushNamed('current_node', 'title');
444 * Starts the menu property 'title-id'
448 private function startTitleId () {
449 // Push the node name on the stacker
450 $this->getStackInstance()->pushNamed('current_node', 'title-id');
454 * Starts the menu property 'title-class'
458 private function startTitleClass () {
459 // Push the node name on the stacker
460 $this->getStackInstance()->pushNamed('current_node', 'title-class');
464 * Starts the menu property 'title-text'
468 private function startTitleText () {
469 // Push the node name on the stacker
470 $this->getStackInstance()->pushNamed('current_node', 'title-text');
474 * Starts the menu property 'entry'
478 private function startEntry () {
479 // Push the node name on the stacker
480 $this->getStackInstance()->pushNamed('current_node', 'entry');
484 * Starts the menu property 'entry-id'
488 private function startEntryId () {
489 // Push the node name on the stacker
490 $this->getStackInstance()->pushNamed('current_node', 'entry-id');
494 * Starts the menu property 'anchor'
498 private function startAnchor () {
499 // Push the node name on the stacker
500 $this->getStackInstance()->pushNamed('current_node', 'anchor');
504 * Starts the menu property 'anchor-id'
508 private function startAnchorId () {
509 // Push the node name on the stacker
510 $this->getStackInstance()->pushNamed('current_node', 'anchor-id');
514 * Starts the menu property 'anchor-text'
518 private function startAnchorText () {
519 // Push the node name on the stacker
520 $this->getStackInstance()->pushNamed('current_node', 'anchor-text');
524 * Starts the menu property 'anchor-title'
528 private function startAnchorTitle () {
529 // Push the node name on the stacker
530 $this->getStackInstance()->pushNamed('current_node', 'anchor-title');
534 * Starts the menu property 'anchor-href'
538 private function startAnchorHref () {
539 // Push the node name on the stacker
540 $this->getStackInstance()->pushNamed('current_node', 'anchor-href');
544 * Starts the menu property 'footer-id'
548 private function startFooterId () {
549 // Push the node name on the stacker
550 $this->getStackInstance()->pushNamed('current_node', 'footer-id');
554 * Starts the menu property 'footer-class'
558 private function startFooterClass () {
559 // Push the node name on the stacker
560 $this->getStackInstance()->pushNamed('current_node', 'footer-class');
564 * Starts the menu property 'footer-text'
568 private function startFooterText () {
569 // Push the node name on the stacker
570 $this->getStackInstance()->pushNamed('current_node', 'footer-text');
574 * Finishes the title node by added another template to the menu
578 private function finishTitle () {
579 // Pop the last entry
580 $this->getStackInstance()->popNamed('current_node');
584 * Finishes the title-id node by
588 private function finishTitleId () {
589 // Pop the last entry
590 $this->getStackInstance()->popNamed('current_node');
594 * Finishes the title-class node
598 private function finishTitleClass () {
599 // Pop the last entry
600 $this->getStackInstance()->popNamed('current_node');
604 * Finishes the title-class node
608 private function finishTitleText () {
609 // Pop the last entry
610 $this->getStackInstance()->popNamed('current_node');
614 * Finishes the footer-text node
618 private function finishFooterText () {
619 // Pop the last entry
620 $this->getStackInstance()->popNamed('current_node');
624 * Finishes the footer-class node
628 private function finishFooterClass () {
629 // Pop the last entry
630 $this->getStackInstance()->popNamed('current_node');
634 * Finishes the footer-id node
638 private function finishFooterId () {
639 // Pop the last entry
640 $this->getStackInstance()->popNamed('current_node');
644 * Finishes the anchor-href node
648 private function finishAnchorHref () {
649 // Pop the last entry
650 $this->getStackInstance()->popNamed('current_node');
654 * Finishes the anchor-title node
658 private function finishAnchorTitle () {
659 // Pop the last entry
660 $this->getStackInstance()->popNamed('current_node');
664 * Finishes the anchor-text node
668 private function finishAnchorText () {
669 // Pop the last entry
670 $this->getStackInstance()->popNamed('current_node');
674 * Finishes the anchor-id node
678 private function finishAnchorId () {
679 // Pop the last entry
680 $this->getStackInstance()->popNamed('current_node');
684 * Finishes the anchor node
688 private function finishAnchor () {
689 // Pop the last entry
690 $this->getStackInstance()->popNamed('current_node');
694 * Finishes the entry-id node
698 private function finishEntryId () {
699 // Pop the last entry
700 $this->getStackInstance()->popNamed('current_node');
704 * Finishes the entry node
708 private function finishEntry () {
709 // Pop the last entry
710 $this->getStackInstance()->popNamed('current_node');
712 // Render this menu entry
713 $this->renderMenuEntry();
717 * Finishes the block node
721 private function finishBlock () {
722 // Pop the last entry
723 $this->getStackInstance()->popNamed('current_node');
725 // Render this menu block
726 $this->renderMenuBlock();
730 * Finishes the block-list node
734 private function finishBlockList () {
735 // Pop the last entry
736 $this->getStackInstance()->popNamed('current_node');
740 * Finishes the menu entries
744 private function finishEntryList () {
745 // Pop the last entry
746 $this->getStackInstance()->popNamed('current_node');
750 * Finishes the menu block header
754 private function finishBlockHeader () {
755 // Pop the last entry
756 $this->getStackInstance()->popNamed('current_node');
760 * Finishes the menu block footer
764 private function finishBlockFooter () {
765 // Pop the last entry
766 $this->getStackInstance()->popNamed('current_node');
774 private function finishMenu () {
775 // Pop the last entry
776 $this->getStackInstance()->popNamed('current_node');
780 * Renders this menu entry, as every block all variables got overwritten
781 * with data from next entry.
785 private function renderMenuEntry () {
786 // Prepare template engine
787 $templateInstance = $this->prepareTemplateInstance();
789 // Load menu entry template
790 $templateInstance->loadCodeTemplate('menu_entry');
792 // Copy all variables over to it
793 foreach ($this->menuEntryVariables as $variableName) {
795 $variableValue = $this->readVariable($variableName);
797 // Is the key 'anchor-href'?
798 if ($variableName == 'anchor-href') {
799 // Expand variable with URL then
800 $variableValue = '{?base_url?}/' . $variableValue;
803 // ... into the instance
804 $templateInstance->assignVariable($variableName, $variableValue);
807 // Compile template + variables
808 $templateInstance->compileTemplate();
809 $templateInstance->compileVariables();
812 $this->menuEntries[$this->readVariable('entry_id')] = $templateInstance->getRawTemplateData();
816 * Renders this menu block, as next block all data is overwritten with
821 private function renderMenuBlock () {
822 // Init block content
823 $blockContent = implode('', $this->menuEntries);
825 // Prepare template engine
826 $templateInstance = $this->prepareTemplateInstance();
828 // Load menu entry template
829 $templateInstance->loadCodeTemplate('menu_block');
831 // Copy all variables over to it
832 foreach ($this->menuBlockVariables as $variableName) {
834 $variableValue = $this->readVariable($variableName);
836 // ... into the instance
837 $templateInstance->assignVariable($variableName, $variableValue);
840 // Assign block content
841 $templateInstance->assignVariable('block_content', $blockContent);
843 // Compile template + variables
844 $templateInstance->compileTemplate();
845 $templateInstance->compileVariables();
848 array_push($this->menuBlocks, $templateInstance->getRawTemplateData());
850 // Reset rendered menu entries array
851 $this->menuEntries = array();
855 * "Getter" for menu content
857 * @return $menuContent Returned menu content
859 public function getMenuContent () {
860 // Implode menuBlocks
861 $menuContent = implode('', $this->menuBlocks);
864 $this->menuBlocks = array();
871 * Getter for menu cache file instance
873 * @return $fileInstance Full-qualified file name of the menu cache
875 public function getMenuCacheFile () {
876 // Get the application instance from registry
877 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
879 // Get the file instance ready
880 $fileInstance = new SplFileInfo(sprintf('%s%smenus/_cache/%s.%s',
881 FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('application_base_path'),
882 $applicationInstance->getAppShortName(),
884 $this->getMenuInstance()->getMenuName() . ':' .
885 $this->__toString() . ':' .
886 $this->getMenuInstance()->__toString()
888 $this->getMenuInstance()->getMenuType()
892 return $fileInstance;