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\Registry;
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 <<<<<<< HEAD:framework/main/classes/template/menu/class_MenuTemplateEngine.php
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
25 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
26 >>>>>>> Some updates::inc/main/classes/template/menu/class_MenuTemplateEngine.php
27 * @license GNU GPL 3.0 or any newer version
28 * @link http://www.shipsimu.org
30 * This program is free software: you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation, either version 3 of the License, or
33 * (at your option) any later version.
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
40 * You should have received a copy of the GNU General Public License
41 * along with this program. If not, see <http://www.gnu.org/licenses/>.
43 class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
45 * Main nodes in the XML tree ('menu' is ignored)
47 private $mainNodes = array(
52 * Sub nodes in the XML tree
54 private $subNodes = array(
80 * Variables for a menu entry
82 private $menuEntryVariables = array(
93 * Variables for a menu block
95 private $menuBlockVariables = array(
100 // Content is taken from menuEntries
108 * Rendered menu entries
110 private $menuEntries = array();
113 * Rendered menu blocks
115 private $menuBlocks = array();
120 private $menuInstance = NULL;
125 private $curr = array();
128 * Content from dependency
130 private $dependencyContent = array();
133 * Protected constructor
137 protected function __construct () {
138 // Call parent constructor
139 parent::__construct(__CLASS__);
143 * Creates an instance of the class TemplateEngine and prepares it for usage
145 * @param $menuInstance A RenderableMenu instance
146 * @return $templateInstance An instance of TemplateEngine
147 * @throws UnexpectedValueException If the found $templateBasePath is empty or not a string
148 * @throws InvalidDirectoryException If $templateBasePath is no directory or not found
149 * @throws BasePathReadProtectedException If $templateBasePath is
152 public static final function createMenuTemplateEngine (RenderableMenu $menuInstance) {
153 // Get a new instance
154 $templateInstance = new MenuTemplateEngine();
156 // Get the application instance from registry
157 $applicationInstance = Registry::getRegistry()->getInstance('app');
159 // Determine base path
160 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getAppShortName(). '/';
162 // Is the base path valid?
163 if (empty($templateBasePath)) {
164 // Base path is empty
165 throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
166 } elseif (!is_string($templateBasePath)) {
168 throw new UnexpectedValueException(sprintf('[%s:%d] %s is not a string with a base path.', $templateInstance->__toString(), __LINE__, $templateBasePath), self::EXCEPTION_INVALID_STRING);
169 } elseif (!is_dir($templateBasePath)) {
171 throw new InvalidDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
172 } elseif (!is_readable($templateBasePath)) {
174 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
178 $templateInstance->setTemplateBasePath($templateBasePath);
180 // Set template extensions
181 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
182 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('menu_template_extension'));
184 // Absolute output path for compiled templates
185 $templateInstance->setCompileOutputPath(sprintf('%s%s/',
187 $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path')
190 // Set the menu instance
191 $templateInstance->setMenuInstance($menuInstance);
193 // Init a variable stacker
194 $stackInstance = ObjectFactory::createObjectByConfiguredName('menu_stacker_class');
197 $templateInstance->setStackInstance($stackInstance);
199 // Return the prepared instance
200 return $templateInstance;
204 * Load a specified menu template into the engine
206 * @param $template The menu template we shall load which is
207 * located in 'menu' by default
210 public function loadMenuTemplate ($template) {
212 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
214 // Load the special template
215 $this->loadTemplate($template);
219 * Getter for current main node
221 * @return $currMainNode Current main node
223 public final function getCurrMainNode () {
224 return $this->curr['main_node'];
228 * Setter for current main node
230 * @param $element Element name to set as current main node
231 * @return $currMainNode Current main node
233 private final function setCurrMainNode ($element) {
234 $this->curr['main_node'] = (string) $element;
238 * Getter for main node array
240 * @return $mainNodes Array with valid main node names
242 public final function getMainNodes () {
243 return $this->mainNodes;
247 * Getter for sub node array
249 * @return $subNodes Array with valid sub node names
251 public final function getSubNodes () {
252 return $this->subNodes;
256 * Handles the start element of an XML resource
258 * @param $resource XML parser resource (currently ignored)
259 * @param $element The element we shall handle
260 * @param $attributes All attributes
262 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
264 public function startElement ($resource, $element, array $attributes) {
265 // Initial method name which will never be called...
266 $methodName = 'initMenu';
268 // Make the element name lower-case
269 $element = strtolower($element);
271 // Is the element a main node?
272 //* DEBUG: */ echo "START: >".$element."<<br />\n";
273 if (in_array($element, $this->getMainNodes())) {
274 // Okay, main node found!
275 $methodName = 'start' . self::convertToClassName($element);
278 $this->setCurrMainNode($element);
279 } elseif (in_array($element, $this->getSubNodes())) {
281 $methodName = 'start' . self::convertToClassName($element);
282 } elseif ($element != 'menu') {
283 // Invalid node name found
284 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
288 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
289 call_user_func_array(array($this, $methodName), $attributes);
293 * Ends the main or sub node by sending out the gathered data
295 * @param $resource An XML resource pointer (currently ignored)
296 * @param $nodeName Name of the node we want to finish
298 * @throws XmlNodeMismatchException If current main node mismatches the closing one
300 public function finishElement ($resource, $nodeName) {
301 // Make all lower-case
302 $nodeName = strtolower($nodeName);
304 // Does this match with current main node?
305 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
306 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
308 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
311 // Construct method name
312 $methodName = 'finish' . self::convertToClassName($nodeName);
314 // Call the corresponding method
315 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
316 call_user_func_array(array($this, $methodName), array());
322 * @param $resource XML parser resource (currently ignored)
323 * @param $characters Characters to handle
325 * @todo Find something useful with this!
327 public function characterHandler ($resource, $characters) {
328 // Trim all spaces away
329 $characters = trim($characters);
331 // Is this string empty?
332 if (empty($characters)) {
333 // Then skip it silently
337 // Assign the found characters to variable and use the last entry from
339 parent::assignVariable($this->getStackInstance()->getNamed('current_node'), $characters);
343 * Handles the template dependency for given node
345 * @param $node The node we should load a dependency template
346 * @param $templateDependency A template to load to satisfy dependencies
349 private function handleTemplateDependency ($node, $templateDependency) {
350 // Is the template dependency set?
351 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
352 // Get a temporay menu template instance
353 $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getMenuInstance()));
356 $templateInstance->loadMenuTemplate($templateDependency);
358 // Parse the XML content
359 $templateInstance->renderXmlContent();
361 // Save the parsed raw content in our dependency array
362 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
367 * Intializes the menu
369 * @param $templateDependency A template to load to satisfy dependencies
371 * @todo Add cache creation here
373 private function initMenu ($templateDependency = '') {
374 // Get web template engine
375 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('html_template_class', array(Registry::getRegistry()->getInstance('app'))));
377 // Handle the dependency template
378 $this->handleTemplateDependency('menu', $templateDependency);
380 // Push the node name on the stacker
381 $this->getStackInstance()->pushNamed('current_node', 'menu');
385 * Starts the menu entries
387 * @param $templateDependency A template to load to satisfy dependencies
390 private function startEntryList () {
391 // Push the node name on the stacker
392 $this->getStackInstance()->pushNamed('current_node', 'entry-list');
396 * Starts the menu block header
400 private function startBlockHeader () {
401 // Push the node name on the stacker
402 $this->getStackInstance()->pushNamed('current_node', 'block-header');
406 * Starts the menu block footer
410 private function startBlockFooter () {
411 // Push the node name on the stacker
412 $this->getStackInstance()->pushNamed('current_node', 'block-footer');
416 * Starts the menu property 'block-list'
420 private function startBlockList () {
421 // Push the node name on the stacker
422 $this->getStackInstance()->pushNamed('current_node', 'block-list');
426 * Starts the menu property 'block'
430 private function startBlock () {
431 // Push the node name on the stacker
432 $this->getStackInstance()->pushNamed('current_node', 'block');
436 * Starts the menu property 'title'
440 private function startTitle () {
441 // Push the node name on the stacker
442 $this->getStackInstance()->pushNamed('current_node', 'title');
446 * Starts the menu property 'title-id'
450 private function startTitleId () {
451 // Push the node name on the stacker
452 $this->getStackInstance()->pushNamed('current_node', 'title-id');
456 * Starts the menu property 'title-class'
460 private function startTitleClass () {
461 // Push the node name on the stacker
462 $this->getStackInstance()->pushNamed('current_node', 'title-class');
466 * Starts the menu property 'title-text'
470 private function startTitleText () {
471 // Push the node name on the stacker
472 $this->getStackInstance()->pushNamed('current_node', 'title-text');
476 * Starts the menu property 'entry'
480 private function startEntry () {
481 // Push the node name on the stacker
482 $this->getStackInstance()->pushNamed('current_node', 'entry');
486 * Starts the menu property 'entry-id'
490 private function startEntryId () {
491 // Push the node name on the stacker
492 $this->getStackInstance()->pushNamed('current_node', 'entry-id');
496 * Starts the menu property 'anchor'
500 private function startAnchor () {
501 // Push the node name on the stacker
502 $this->getStackInstance()->pushNamed('current_node', 'anchor');
506 * Starts the menu property 'anchor-id'
510 private function startAnchorId () {
511 // Push the node name on the stacker
512 $this->getStackInstance()->pushNamed('current_node', 'anchor-id');
516 * Starts the menu property 'anchor-text'
520 private function startAnchorText () {
521 // Push the node name on the stacker
522 $this->getStackInstance()->pushNamed('current_node', 'anchor-text');
526 * Starts the menu property 'anchor-title'
530 private function startAnchorTitle () {
531 // Push the node name on the stacker
532 $this->getStackInstance()->pushNamed('current_node', 'anchor-title');
536 * Starts the menu property 'anchor-href'
540 private function startAnchorHref () {
541 // Push the node name on the stacker
542 $this->getStackInstance()->pushNamed('current_node', 'anchor-href');
546 * Starts the menu property 'footer-id'
550 private function startFooterId () {
551 // Push the node name on the stacker
552 $this->getStackInstance()->pushNamed('current_node', 'footer-id');
556 * Starts the menu property 'footer-class'
560 private function startFooterClass () {
561 // Push the node name on the stacker
562 $this->getStackInstance()->pushNamed('current_node', 'footer-class');
566 * Starts the menu property 'footer-text'
570 private function startFooterText () {
571 // Push the node name on the stacker
572 $this->getStackInstance()->pushNamed('current_node', 'footer-text');
576 * Finishes the title node by added another template to the menu
580 private function finishTitle () {
581 // Pop the last entry
582 $this->getStackInstance()->popNamed('current_node');
586 * Finishes the title-id node by
590 private function finishTitleId () {
591 // Pop the last entry
592 $this->getStackInstance()->popNamed('current_node');
596 * Finishes the title-class node
600 private function finishTitleClass () {
601 // Pop the last entry
602 $this->getStackInstance()->popNamed('current_node');
606 * Finishes the title-class node
610 private function finishTitleText () {
611 // Pop the last entry
612 $this->getStackInstance()->popNamed('current_node');
616 * Finishes the footer-text node
620 private function finishFooterText () {
621 // Pop the last entry
622 $this->getStackInstance()->popNamed('current_node');
626 * Finishes the footer-class node
630 private function finishFooterClass () {
631 // Pop the last entry
632 $this->getStackInstance()->popNamed('current_node');
636 * Finishes the footer-id node
640 private function finishFooterId () {
641 // Pop the last entry
642 $this->getStackInstance()->popNamed('current_node');
646 * Finishes the anchor-href node
650 private function finishAnchorHref () {
651 // Pop the last entry
652 $this->getStackInstance()->popNamed('current_node');
656 * Finishes the anchor-title node
660 private function finishAnchorTitle () {
661 // Pop the last entry
662 $this->getStackInstance()->popNamed('current_node');
666 * Finishes the anchor-text node
670 private function finishAnchorText () {
671 // Pop the last entry
672 $this->getStackInstance()->popNamed('current_node');
676 * Finishes the anchor-id node
680 private function finishAnchorId () {
681 // Pop the last entry
682 $this->getStackInstance()->popNamed('current_node');
686 * Finishes the anchor node
690 private function finishAnchor () {
691 // Pop the last entry
692 $this->getStackInstance()->popNamed('current_node');
696 * Finishes the entry-id node
700 private function finishEntryId () {
701 // Pop the last entry
702 $this->getStackInstance()->popNamed('current_node');
706 * Finishes the entry node
710 private function finishEntry () {
711 // Pop the last entry
712 $this->getStackInstance()->popNamed('current_node');
714 // Render this menu entry
715 $this->renderMenuEntry();
719 * Finishes the block node
723 private function finishBlock () {
724 // Pop the last entry
725 $this->getStackInstance()->popNamed('current_node');
727 // Render this menu block
728 $this->renderMenuBlock();
732 * Finishes the block-list node
736 private function finishBlockList () {
737 // Pop the last entry
738 $this->getStackInstance()->popNamed('current_node');
742 * Finishes the menu entries
746 private function finishEntryList () {
747 // Pop the last entry
748 $this->getStackInstance()->popNamed('current_node');
752 * Finishes the menu block header
756 private function finishBlockHeader () {
757 // Pop the last entry
758 $this->getStackInstance()->popNamed('current_node');
762 * Finishes the menu block footer
766 private function finishBlockFooter () {
767 // Pop the last entry
768 $this->getStackInstance()->popNamed('current_node');
776 private function finishMenu () {
777 // Pop the last entry
778 $this->getStackInstance()->popNamed('current_node');
782 * Renders this menu entry, as every block all variables got overwritten
783 * with data from next entry.
787 private function renderMenuEntry () {
788 // Prepare template engine
789 $templateInstance = $this->prepareTemplateInstance();
791 // Load menu entry template
792 $templateInstance->loadCodeTemplate('menu_entry');
794 // Copy all variables over to it
795 foreach ($this->menuEntryVariables as $variableName) {
797 $variableValue = $this->readVariable($variableName);
799 // Is the key 'anchor-href'?
800 if ($variableName == 'anchor-href') {
801 // Expand variable with URL then
802 $variableValue = '{?base_url?}/' . $variableValue;
805 // ... into the instance
806 $templateInstance->assignVariable($variableName, $variableValue);
809 // Compile template + variables
810 $templateInstance->compileTemplate();
811 $templateInstance->compileVariables();
814 $this->menuEntries[$this->readVariable('entry_id')] = $templateInstance->getRawTemplateData();
818 * Renders this menu block, as next block all data is overwritten with
823 private function renderMenuBlock () {
824 // Init block content
825 $blockContent = implode('', $this->menuEntries);
827 // Prepare template engine
828 $templateInstance = $this->prepareTemplateInstance();
830 // Load menu entry template
831 $templateInstance->loadCodeTemplate('menu_block');
833 // Copy all variables over to it
834 foreach ($this->menuBlockVariables as $variableName) {
836 $variableValue = $this->readVariable($variableName);
838 // ... into the instance
839 $templateInstance->assignVariable($variableName, $variableValue);
842 // Assign block content
843 $templateInstance->assignVariable('block_content', $blockContent);
845 // Compile template + variables
846 $templateInstance->compileTemplate();
847 $templateInstance->compileVariables();
850 array_push($this->menuBlocks, $templateInstance->getRawTemplateData());
852 // Reset rendered menu entries array
853 $this->menuEntries = array();
857 * "Getter" for menu content
859 * @return $menuContent Returned menu content
861 public function getMenuContent () {
862 // Implode menuBlocks
863 $menuContent = implode('', $this->menuBlocks);
866 $this->menuBlocks = array();
873 * Getter for menu cache file instance
875 * @return $fileInstance Full-qualified file name of the menu cache
877 public function getMenuCacheFile () {
878 // Get the application instance from registry
879 $applicationInstance = Registry::getRegistry()->getInstance('app');
881 // Get the file instance ready
882 $fileInstance = new SplFileInfo(sprintf('%s%smenus/_cache/%s.%s',
883 $this->getConfigInstance()->getConfigEntry('application_base_path'),
884 $applicationInstance->getAppShortName(),
886 $this->getMenuInstance()->getMenuName() . ':' .
887 $this->__toString() . ':' .
888 $this->getMenuInstance()->__toString()
890 $this->getMenuInstance()->getMenuType()
894 return $fileInstance;