3 namespace CoreFramework\Template\Engine;
5 // Import framework stuff
6 use CoreFramework\Registry\Generic\Registry;
7 use CoreFramework\Template\CompileableTemplate;
10 * A Menu template engine class
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
33 * Main nodes in the XML tree ('menu' is ignored)
35 private $mainNodes = array(
40 * Sub nodes in the XML tree
42 private $subNodes = array(
68 * Variables for a menu entry
70 private $menuEntryVariables = array(
81 * Variables for a menu block
83 private $menuBlockVariables = array(
88 // Content is taken from menuEntries
96 * Rendered menu entries
98 private $menuEntries = array();
101 * Rendered menu blocks
103 private $menuBlocks = array();
108 private $menuInstance = NULL;
113 private $curr = array();
116 * Content from dependency
118 private $dependencyContent = array();
121 * Protected constructor
125 protected function __construct () {
126 // Call parent constructor
127 parent::__construct(__CLASS__);
131 * Creates an instance of the class TemplateEngine and prepares it for usage
133 * @param $menuInstance A RenderableMenu instance
134 * @return $templateInstance An instance of TemplateEngine
135 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
136 * @throws InvalidBasePathStringException If $templateBasePath is no string
137 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
138 * directory or not found
139 * @throws BasePathReadProtectedException If $templateBasePath is
142 public static final function createMenuTemplateEngine (RenderableMenu $menuInstance) {
143 // Get a new instance
144 $templateInstance = new MenuTemplateEngine();
146 // Get the application instance from registry
147 $applicationInstance = Registry::getRegistry()->getInstance('app');
149 // Determine base path
150 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
152 // Is the base path valid?
153 if (empty($templateBasePath)) {
154 // Base path is empty
155 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
156 } elseif (!is_string($templateBasePath)) {
158 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
159 } elseif (!is_dir($templateBasePath)) {
161 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
162 } elseif (!is_readable($templateBasePath)) {
164 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
168 $templateInstance->setTemplateBasePath($templateBasePath);
170 // Set template extensions
171 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
172 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('menu_template_extension'));
174 // Absolute output path for compiled templates
175 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
177 // Set the menu instance
178 $templateInstance->setMenuInstance($menuInstance);
180 // Init a variable stacker
181 $stackInstance = ObjectFactory::createObjectByConfiguredName('menu_stacker_class');
184 $templateInstance->setStackInstance($stackInstance);
186 // Return the prepared instance
187 return $templateInstance;
191 * Load a specified menu template into the engine
193 * @param $template The menu template we shall load which is
194 * located in 'menu' by default
197 public function loadMenuTemplate ($template) {
199 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
201 // Load the special template
202 $this->loadTemplate($template);
206 * Getter for current main node
208 * @return $currMainNode Current main node
210 public final function getCurrMainNode () {
211 return $this->curr['main_node'];
215 * Setter for current main node
217 * @param $element Element name to set as current main node
218 * @return $currMainNode Current main node
220 private final function setCurrMainNode ($element) {
221 $this->curr['main_node'] = (string) $element;
225 * Getter for main node array
227 * @return $mainNodes Array with valid main node names
229 public final function getMainNodes () {
230 return $this->mainNodes;
234 * Getter for sub node array
236 * @return $subNodes Array with valid sub node names
238 public final function getSubNodes () {
239 return $this->subNodes;
243 * Handles the start element of an XML resource
245 * @param $resource XML parser resource (currently ignored)
246 * @param $element The element we shall handle
247 * @param $attributes All attributes
249 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
251 public function startElement ($resource, $element, array $attributes) {
252 // Initial method name which will never be called...
253 $methodName = 'initMenu';
255 // Make the element name lower-case
256 $element = strtolower($element);
258 // Is the element a main node?
259 //* DEBUG: */ echo "START: >".$element."<<br />\n";
260 if (in_array($element, $this->getMainNodes())) {
261 // Okay, main node found!
262 $methodName = 'start' . self::convertToClassName($element);
265 $this->setCurrMainNode($element);
266 } elseif (in_array($element, $this->getSubNodes())) {
268 $methodName = 'start' . self::convertToClassName($element);
269 } elseif ($element != 'menu') {
270 // Invalid node name found
271 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
275 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
276 call_user_func_array(array($this, $methodName), $attributes);
280 * Ends the main or sub node by sending out the gathered data
282 * @param $resource An XML resource pointer (currently ignored)
283 * @param $nodeName Name of the node we want to finish
285 * @throws XmlNodeMismatchException If current main node mismatches the closing one
287 public function finishElement ($resource, $nodeName) {
288 // Make all lower-case
289 $nodeName = strtolower($nodeName);
291 // Does this match with current main node?
292 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
293 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
295 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
298 // Construct method name
299 $methodName = 'finish' . self::convertToClassName($nodeName);
301 // Call the corresponding method
302 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
303 call_user_func_array(array($this, $methodName), array());
309 * @param $resource XML parser resource (currently ignored)
310 * @param $characters Characters to handle
312 * @todo Find something useful with this!
314 public function characterHandler ($resource, $characters) {
315 // Trim all spaces away
316 $characters = trim($characters);
318 // Is this string empty?
319 if (empty($characters)) {
320 // Then skip it silently
324 // Assign the found characters to variable and use the last entry from
326 parent::assignVariable($this->getStackInstance()->getNamed('current_node'), $characters);
330 * Handles the template dependency for given node
332 * @param $node The node we should load a dependency template
333 * @param $templateDependency A template to load to satisfy dependencies
336 private function handleTemplateDependency ($node, $templateDependency) {
337 // Is the template dependency set?
338 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
339 // Get a temporay menu template instance
340 $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getMenuInstance()));
343 $templateInstance->loadMenuTemplate($templateDependency);
345 // Parse the XML content
346 $templateInstance->renderXmlContent();
348 // Save the parsed raw content in our dependency array
349 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
354 * Intializes the menu
356 * @param $templateDependency A template to load to satisfy dependencies
358 * @todo Add cache creation here
360 private function initMenu ($templateDependency = '') {
361 // Get web template engine
362 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('html_template_class', array($this->getApplicationInstance())));
364 // Handle the dependency template
365 $this->handleTemplateDependency('menu', $templateDependency);
367 // Push the node name on the stacker
368 $this->getStackInstance()->pushNamed('current_node', 'menu');
372 * Starts the menu entries
374 * @param $templateDependency A template to load to satisfy dependencies
377 private function startEntryList () {
378 // Push the node name on the stacker
379 $this->getStackInstance()->pushNamed('current_node', 'entry-list');
383 * Starts the menu block header
387 private function startBlockHeader () {
388 // Push the node name on the stacker
389 $this->getStackInstance()->pushNamed('current_node', 'block-header');
393 * Starts the menu block footer
397 private function startBlockFooter () {
398 // Push the node name on the stacker
399 $this->getStackInstance()->pushNamed('current_node', 'block-footer');
403 * Starts the menu property 'block-list'
407 private function startBlockList () {
408 // Push the node name on the stacker
409 $this->getStackInstance()->pushNamed('current_node', 'block-list');
413 * Starts the menu property 'block'
417 private function startBlock () {
418 // Push the node name on the stacker
419 $this->getStackInstance()->pushNamed('current_node', 'block');
423 * Starts the menu property 'title'
427 private function startTitle () {
428 // Push the node name on the stacker
429 $this->getStackInstance()->pushNamed('current_node', 'title');
433 * Starts the menu property 'title-id'
437 private function startTitleId () {
438 // Push the node name on the stacker
439 $this->getStackInstance()->pushNamed('current_node', 'title-id');
443 * Starts the menu property 'title-class'
447 private function startTitleClass () {
448 // Push the node name on the stacker
449 $this->getStackInstance()->pushNamed('current_node', 'title-class');
453 * Starts the menu property 'title-text'
457 private function startTitleText () {
458 // Push the node name on the stacker
459 $this->getStackInstance()->pushNamed('current_node', 'title-text');
463 * Starts the menu property 'entry'
467 private function startEntry () {
468 // Push the node name on the stacker
469 $this->getStackInstance()->pushNamed('current_node', 'entry');
473 * Starts the menu property 'entry-id'
477 private function startEntryId () {
478 // Push the node name on the stacker
479 $this->getStackInstance()->pushNamed('current_node', 'entry-id');
483 * Starts the menu property 'anchor'
487 private function startAnchor () {
488 // Push the node name on the stacker
489 $this->getStackInstance()->pushNamed('current_node', 'anchor');
493 * Starts the menu property 'anchor-id'
497 private function startAnchorId () {
498 // Push the node name on the stacker
499 $this->getStackInstance()->pushNamed('current_node', 'anchor-id');
503 * Starts the menu property 'anchor-text'
507 private function startAnchorText () {
508 // Push the node name on the stacker
509 $this->getStackInstance()->pushNamed('current_node', 'anchor-text');
513 * Starts the menu property 'anchor-title'
517 private function startAnchorTitle () {
518 // Push the node name on the stacker
519 $this->getStackInstance()->pushNamed('current_node', 'anchor-title');
523 * Starts the menu property 'anchor-href'
527 private function startAnchorHref () {
528 // Push the node name on the stacker
529 $this->getStackInstance()->pushNamed('current_node', 'anchor-href');
533 * Starts the menu property 'footer-id'
537 private function startFooterId () {
538 // Push the node name on the stacker
539 $this->getStackInstance()->pushNamed('current_node', 'footer-id');
543 * Starts the menu property 'footer-class'
547 private function startFooterClass () {
548 // Push the node name on the stacker
549 $this->getStackInstance()->pushNamed('current_node', 'footer-class');
553 * Starts the menu property 'footer-text'
557 private function startFooterText () {
558 // Push the node name on the stacker
559 $this->getStackInstance()->pushNamed('current_node', 'footer-text');
563 * Finishes the title node by added another template to the menu
567 private function finishTitle () {
568 // Pop the last entry
569 $this->getStackInstance()->popNamed('current_node');
573 * Finishes the title-id node by
577 private function finishTitleId () {
578 // Pop the last entry
579 $this->getStackInstance()->popNamed('current_node');
583 * Finishes the title-class node
587 private function finishTitleClass () {
588 // Pop the last entry
589 $this->getStackInstance()->popNamed('current_node');
593 * Finishes the title-class node
597 private function finishTitleText () {
598 // Pop the last entry
599 $this->getStackInstance()->popNamed('current_node');
603 * Finishes the footer-text node
607 private function finishFooterText () {
608 // Pop the last entry
609 $this->getStackInstance()->popNamed('current_node');
613 * Finishes the footer-class node
617 private function finishFooterClass () {
618 // Pop the last entry
619 $this->getStackInstance()->popNamed('current_node');
623 * Finishes the footer-id node
627 private function finishFooterId () {
628 // Pop the last entry
629 $this->getStackInstance()->popNamed('current_node');
633 * Finishes the anchor-href node
637 private function finishAnchorHref () {
638 // Pop the last entry
639 $this->getStackInstance()->popNamed('current_node');
643 * Finishes the anchor-title node
647 private function finishAnchorTitle () {
648 // Pop the last entry
649 $this->getStackInstance()->popNamed('current_node');
653 * Finishes the anchor-text node
657 private function finishAnchorText () {
658 // Pop the last entry
659 $this->getStackInstance()->popNamed('current_node');
663 * Finishes the anchor-id node
667 private function finishAnchorId () {
668 // Pop the last entry
669 $this->getStackInstance()->popNamed('current_node');
673 * Finishes the anchor node
677 private function finishAnchor () {
678 // Pop the last entry
679 $this->getStackInstance()->popNamed('current_node');
683 * Finishes the entry-id node
687 private function finishEntryId () {
688 // Pop the last entry
689 $this->getStackInstance()->popNamed('current_node');
693 * Finishes the entry node
697 private function finishEntry () {
698 // Pop the last entry
699 $this->getStackInstance()->popNamed('current_node');
701 // Render this menu entry
702 $this->renderMenuEntry();
706 * Finishes the block node
710 private function finishBlock () {
711 // Pop the last entry
712 $this->getStackInstance()->popNamed('current_node');
714 // Render this menu block
715 $this->renderMenuBlock();
719 * Finishes the block-list node
723 private function finishBlockList () {
724 // Pop the last entry
725 $this->getStackInstance()->popNamed('current_node');
729 * Finishes the menu entries
733 private function finishEntryList () {
734 // Pop the last entry
735 $this->getStackInstance()->popNamed('current_node');
739 * Finishes the menu block header
743 private function finishBlockHeader () {
744 // Pop the last entry
745 $this->getStackInstance()->popNamed('current_node');
749 * Finishes the menu block footer
753 private function finishBlockFooter () {
754 // Pop the last entry
755 $this->getStackInstance()->popNamed('current_node');
763 private function finishMenu () {
764 // Pop the last entry
765 $this->getStackInstance()->popNamed('current_node');
769 * Renders this menu entry, as every block all variables got overwritten
770 * with data from next entry.
774 private function renderMenuEntry () {
775 // Prepare template engine
776 $templateInstance = $this->prepareTemplateInstance();
778 // Load menu entry template
779 $templateInstance->loadCodeTemplate('menu_entry');
781 // Copy all variables over to it
782 foreach ($this->menuEntryVariables as $variableName) {
784 $variableValue = $this->readVariable($variableName);
786 // Is the key 'anchor-href'?
787 if ($variableName == 'anchor-href') {
788 // Expand variable with URL then
789 $variableValue = '{?base_url?}/' . $variableValue;
792 // ... into the instance
793 $templateInstance->assignVariable($variableName, $variableValue);
796 // Compile template + variables
797 $templateInstance->compileTemplate();
798 $templateInstance->compileVariables();
801 $this->menuEntries[$this->readVariable('entry_id')] = $templateInstance->getRawTemplateData();
805 * Renders this menu block, as next block all data is overwritten with
810 private function renderMenuBlock () {
811 // Init block content
812 $blockContent = implode('', $this->menuEntries);
814 // Prepare template engine
815 $templateInstance = $this->prepareTemplateInstance();
817 // Load menu entry template
818 $templateInstance->loadCodeTemplate('menu_block');
820 // Copy all variables over to it
821 foreach ($this->menuBlockVariables as $variableName) {
823 $variableValue = $this->readVariable($variableName);
825 // ... into the instance
826 $templateInstance->assignVariable($variableName, $variableValue);
829 // Assign block content
830 $templateInstance->assignVariable('block_content', $blockContent);
832 // Compile template + variables
833 $templateInstance->compileTemplate();
834 $templateInstance->compileVariables();
837 array_push($this->menuBlocks, $templateInstance->getRawTemplateData());
839 // Reset rendered menu entries array
840 $this->menuEntries = array();
844 * "Getter" for menu content
846 * @return $menuContent Returned menu content
848 public function getMenuContent () {
849 // Implode menuBlocks
850 $menuContent = implode('', $this->menuBlocks);
853 $this->menuBlocks = array();
860 * Getter for menu cache file (FQFN)
862 * @return $fqfn Full-qualified file name of the menu cache
864 public function getMenuCacheFqfn () {
865 // Get the FQFN ready
866 $fqfn = sprintf('%s%s%s/%s.%s',
867 $this->getConfigInstance()->getConfigEntry('base_path'),
868 $this->getGenericBasePath(),
871 $this->getMenuInstance()->getMenuName() . ':' .
872 $this->__toString() . ':' .
873 $this->getMenuInstance()->__toString()
875 $this->getMenuInstance()->getMenuType()