3 namespace CoreFramework\Template\Engine;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Parser\Xml\XmlParser;
8 use CoreFramework\Registry\Registry;
9 use CoreFramework\Template\CompileableTemplate;
12 use \UnexpectedValueException;
15 * A Menu template engine class
17 * @author Roland Haeder <webmaster@shipsimu.org>
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
38 * Main nodes in the XML tree ('menu' is ignored)
40 private $mainNodes = array(
45 * Sub nodes in the XML tree
47 private $subNodes = array(
73 * Variables for a menu entry
75 private $menuEntryVariables = array(
86 * Variables for a menu block
88 private $menuBlockVariables = array(
93 // Content is taken from menuEntries
101 * Rendered menu entries
103 private $menuEntries = array();
106 * Rendered menu blocks
108 private $menuBlocks = array();
113 private $menuInstance = NULL;
118 private $curr = array();
121 * Content from dependency
123 private $dependencyContent = array();
126 * Protected constructor
130 protected function __construct () {
131 // Call parent constructor
132 parent::__construct(__CLASS__);
136 * Creates an instance of the class TemplateEngine and prepares it for usage
138 * @param $menuInstance A RenderableMenu instance
139 * @return $templateInstance An instance of TemplateEngine
140 * @throws UnexpectedValueException If the found $templateBasePath is empty or not a string
141 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
142 * directory or not found
143 * @throws BasePathReadProtectedException If $templateBasePath is
146 public static final function createMenuTemplateEngine (RenderableMenu $menuInstance) {
147 // Get a new instance
148 $templateInstance = new MenuTemplateEngine();
150 // Get the application instance from registry
151 $applicationInstance = Registry::getRegistry()->getInstance('app');
153 // Determine base path
154 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
156 // Is the base path valid?
157 if (empty($templateBasePath)) {
158 // Base path is empty
159 throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
160 } elseif (!is_string($templateBasePath)) {
162 throw new UnexpectedValueException(sprintf('[%s:%d] %s is not a string with a base path.', $templateInstance->__toString(), __LINE__, $templateBasePath), self::EXCEPTION_INVALID_STRING);
163 } elseif (!is_dir($templateBasePath)) {
165 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
166 } elseif (!is_readable($templateBasePath)) {
168 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
172 $templateInstance->setTemplateBasePath($templateBasePath);
174 // Set template extensions
175 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
176 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('menu_template_extension'));
178 // Absolute output path for compiled templates
179 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
181 // Set the menu instance
182 $templateInstance->setMenuInstance($menuInstance);
184 // Init a variable stacker
185 $stackInstance = ObjectFactory::createObjectByConfiguredName('menu_stacker_class');
188 $templateInstance->setStackInstance($stackInstance);
190 // Return the prepared instance
191 return $templateInstance;
195 * Load a specified menu template into the engine
197 * @param $template The menu template we shall load which is
198 * located in 'menu' by default
201 public function loadMenuTemplate ($template) {
203 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
205 // Load the special template
206 $this->loadTemplate($template);
210 * Getter for current main node
212 * @return $currMainNode Current main node
214 public final function getCurrMainNode () {
215 return $this->curr['main_node'];
219 * Setter for current main node
221 * @param $element Element name to set as current main node
222 * @return $currMainNode Current main node
224 private final function setCurrMainNode ($element) {
225 $this->curr['main_node'] = (string) $element;
229 * Getter for main node array
231 * @return $mainNodes Array with valid main node names
233 public final function getMainNodes () {
234 return $this->mainNodes;
238 * Getter for sub node array
240 * @return $subNodes Array with valid sub node names
242 public final function getSubNodes () {
243 return $this->subNodes;
247 * Handles the start element of an XML resource
249 * @param $resource XML parser resource (currently ignored)
250 * @param $element The element we shall handle
251 * @param $attributes All attributes
253 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
255 public function startElement ($resource, $element, array $attributes) {
256 // Initial method name which will never be called...
257 $methodName = 'initMenu';
259 // Make the element name lower-case
260 $element = strtolower($element);
262 // Is the element a main node?
263 //* DEBUG: */ echo "START: >".$element."<<br />\n";
264 if (in_array($element, $this->getMainNodes())) {
265 // Okay, main node found!
266 $methodName = 'start' . self::convertToClassName($element);
269 $this->setCurrMainNode($element);
270 } elseif (in_array($element, $this->getSubNodes())) {
272 $methodName = 'start' . self::convertToClassName($element);
273 } elseif ($element != 'menu') {
274 // Invalid node name found
275 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
279 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
280 call_user_func_array(array($this, $methodName), $attributes);
284 * Ends the main or sub node by sending out the gathered data
286 * @param $resource An XML resource pointer (currently ignored)
287 * @param $nodeName Name of the node we want to finish
289 * @throws XmlNodeMismatchException If current main node mismatches the closing one
291 public function finishElement ($resource, $nodeName) {
292 // Make all lower-case
293 $nodeName = strtolower($nodeName);
295 // Does this match with current main node?
296 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
297 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
299 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
302 // Construct method name
303 $methodName = 'finish' . self::convertToClassName($nodeName);
305 // Call the corresponding method
306 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
307 call_user_func_array(array($this, $methodName), array());
313 * @param $resource XML parser resource (currently ignored)
314 * @param $characters Characters to handle
316 * @todo Find something useful with this!
318 public function characterHandler ($resource, $characters) {
319 // Trim all spaces away
320 $characters = trim($characters);
322 // Is this string empty?
323 if (empty($characters)) {
324 // Then skip it silently
328 // Assign the found characters to variable and use the last entry from
330 parent::assignVariable($this->getStackInstance()->getNamed('current_node'), $characters);
334 * Handles the template dependency for given node
336 * @param $node The node we should load a dependency template
337 * @param $templateDependency A template to load to satisfy dependencies
340 private function handleTemplateDependency ($node, $templateDependency) {
341 // Is the template dependency set?
342 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
343 // Get a temporay menu template instance
344 $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getMenuInstance()));
347 $templateInstance->loadMenuTemplate($templateDependency);
349 // Parse the XML content
350 $templateInstance->renderXmlContent();
352 // Save the parsed raw content in our dependency array
353 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
358 * Intializes the menu
360 * @param $templateDependency A template to load to satisfy dependencies
362 * @todo Add cache creation here
364 private function initMenu ($templateDependency = '') {
365 // Get web template engine
366 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('html_template_class', array($this->getApplicationInstance())));
368 // Handle the dependency template
369 $this->handleTemplateDependency('menu', $templateDependency);
371 // Push the node name on the stacker
372 $this->getStackInstance()->pushNamed('current_node', 'menu');
376 * Starts the menu entries
378 * @param $templateDependency A template to load to satisfy dependencies
381 private function startEntryList () {
382 // Push the node name on the stacker
383 $this->getStackInstance()->pushNamed('current_node', 'entry-list');
387 * Starts the menu block header
391 private function startBlockHeader () {
392 // Push the node name on the stacker
393 $this->getStackInstance()->pushNamed('current_node', 'block-header');
397 * Starts the menu block footer
401 private function startBlockFooter () {
402 // Push the node name on the stacker
403 $this->getStackInstance()->pushNamed('current_node', 'block-footer');
407 * Starts the menu property 'block-list'
411 private function startBlockList () {
412 // Push the node name on the stacker
413 $this->getStackInstance()->pushNamed('current_node', 'block-list');
417 * Starts the menu property 'block'
421 private function startBlock () {
422 // Push the node name on the stacker
423 $this->getStackInstance()->pushNamed('current_node', 'block');
427 * Starts the menu property 'title'
431 private function startTitle () {
432 // Push the node name on the stacker
433 $this->getStackInstance()->pushNamed('current_node', 'title');
437 * Starts the menu property 'title-id'
441 private function startTitleId () {
442 // Push the node name on the stacker
443 $this->getStackInstance()->pushNamed('current_node', 'title-id');
447 * Starts the menu property 'title-class'
451 private function startTitleClass () {
452 // Push the node name on the stacker
453 $this->getStackInstance()->pushNamed('current_node', 'title-class');
457 * Starts the menu property 'title-text'
461 private function startTitleText () {
462 // Push the node name on the stacker
463 $this->getStackInstance()->pushNamed('current_node', 'title-text');
467 * Starts the menu property 'entry'
471 private function startEntry () {
472 // Push the node name on the stacker
473 $this->getStackInstance()->pushNamed('current_node', 'entry');
477 * Starts the menu property 'entry-id'
481 private function startEntryId () {
482 // Push the node name on the stacker
483 $this->getStackInstance()->pushNamed('current_node', 'entry-id');
487 * Starts the menu property 'anchor'
491 private function startAnchor () {
492 // Push the node name on the stacker
493 $this->getStackInstance()->pushNamed('current_node', 'anchor');
497 * Starts the menu property 'anchor-id'
501 private function startAnchorId () {
502 // Push the node name on the stacker
503 $this->getStackInstance()->pushNamed('current_node', 'anchor-id');
507 * Starts the menu property 'anchor-text'
511 private function startAnchorText () {
512 // Push the node name on the stacker
513 $this->getStackInstance()->pushNamed('current_node', 'anchor-text');
517 * Starts the menu property 'anchor-title'
521 private function startAnchorTitle () {
522 // Push the node name on the stacker
523 $this->getStackInstance()->pushNamed('current_node', 'anchor-title');
527 * Starts the menu property 'anchor-href'
531 private function startAnchorHref () {
532 // Push the node name on the stacker
533 $this->getStackInstance()->pushNamed('current_node', 'anchor-href');
537 * Starts the menu property 'footer-id'
541 private function startFooterId () {
542 // Push the node name on the stacker
543 $this->getStackInstance()->pushNamed('current_node', 'footer-id');
547 * Starts the menu property 'footer-class'
551 private function startFooterClass () {
552 // Push the node name on the stacker
553 $this->getStackInstance()->pushNamed('current_node', 'footer-class');
557 * Starts the menu property 'footer-text'
561 private function startFooterText () {
562 // Push the node name on the stacker
563 $this->getStackInstance()->pushNamed('current_node', 'footer-text');
567 * Finishes the title node by added another template to the menu
571 private function finishTitle () {
572 // Pop the last entry
573 $this->getStackInstance()->popNamed('current_node');
577 * Finishes the title-id node by
581 private function finishTitleId () {
582 // Pop the last entry
583 $this->getStackInstance()->popNamed('current_node');
587 * Finishes the title-class node
591 private function finishTitleClass () {
592 // Pop the last entry
593 $this->getStackInstance()->popNamed('current_node');
597 * Finishes the title-class node
601 private function finishTitleText () {
602 // Pop the last entry
603 $this->getStackInstance()->popNamed('current_node');
607 * Finishes the footer-text node
611 private function finishFooterText () {
612 // Pop the last entry
613 $this->getStackInstance()->popNamed('current_node');
617 * Finishes the footer-class node
621 private function finishFooterClass () {
622 // Pop the last entry
623 $this->getStackInstance()->popNamed('current_node');
627 * Finishes the footer-id node
631 private function finishFooterId () {
632 // Pop the last entry
633 $this->getStackInstance()->popNamed('current_node');
637 * Finishes the anchor-href node
641 private function finishAnchorHref () {
642 // Pop the last entry
643 $this->getStackInstance()->popNamed('current_node');
647 * Finishes the anchor-title node
651 private function finishAnchorTitle () {
652 // Pop the last entry
653 $this->getStackInstance()->popNamed('current_node');
657 * Finishes the anchor-text node
661 private function finishAnchorText () {
662 // Pop the last entry
663 $this->getStackInstance()->popNamed('current_node');
667 * Finishes the anchor-id node
671 private function finishAnchorId () {
672 // Pop the last entry
673 $this->getStackInstance()->popNamed('current_node');
677 * Finishes the anchor node
681 private function finishAnchor () {
682 // Pop the last entry
683 $this->getStackInstance()->popNamed('current_node');
687 * Finishes the entry-id node
691 private function finishEntryId () {
692 // Pop the last entry
693 $this->getStackInstance()->popNamed('current_node');
697 * Finishes the entry node
701 private function finishEntry () {
702 // Pop the last entry
703 $this->getStackInstance()->popNamed('current_node');
705 // Render this menu entry
706 $this->renderMenuEntry();
710 * Finishes the block node
714 private function finishBlock () {
715 // Pop the last entry
716 $this->getStackInstance()->popNamed('current_node');
718 // Render this menu block
719 $this->renderMenuBlock();
723 * Finishes the block-list node
727 private function finishBlockList () {
728 // Pop the last entry
729 $this->getStackInstance()->popNamed('current_node');
733 * Finishes the menu entries
737 private function finishEntryList () {
738 // Pop the last entry
739 $this->getStackInstance()->popNamed('current_node');
743 * Finishes the menu block header
747 private function finishBlockHeader () {
748 // Pop the last entry
749 $this->getStackInstance()->popNamed('current_node');
753 * Finishes the menu block footer
757 private function finishBlockFooter () {
758 // Pop the last entry
759 $this->getStackInstance()->popNamed('current_node');
767 private function finishMenu () {
768 // Pop the last entry
769 $this->getStackInstance()->popNamed('current_node');
773 * Renders this menu entry, as every block all variables got overwritten
774 * with data from next entry.
778 private function renderMenuEntry () {
779 // Prepare template engine
780 $templateInstance = $this->prepareTemplateInstance();
782 // Load menu entry template
783 $templateInstance->loadCodeTemplate('menu_entry');
785 // Copy all variables over to it
786 foreach ($this->menuEntryVariables as $variableName) {
788 $variableValue = $this->readVariable($variableName);
790 // Is the key 'anchor-href'?
791 if ($variableName == 'anchor-href') {
792 // Expand variable with URL then
793 $variableValue = '{?base_url?}/' . $variableValue;
796 // ... into the instance
797 $templateInstance->assignVariable($variableName, $variableValue);
800 // Compile template + variables
801 $templateInstance->compileTemplate();
802 $templateInstance->compileVariables();
805 $this->menuEntries[$this->readVariable('entry_id')] = $templateInstance->getRawTemplateData();
809 * Renders this menu block, as next block all data is overwritten with
814 private function renderMenuBlock () {
815 // Init block content
816 $blockContent = implode('', $this->menuEntries);
818 // Prepare template engine
819 $templateInstance = $this->prepareTemplateInstance();
821 // Load menu entry template
822 $templateInstance->loadCodeTemplate('menu_block');
824 // Copy all variables over to it
825 foreach ($this->menuBlockVariables as $variableName) {
827 $variableValue = $this->readVariable($variableName);
829 // ... into the instance
830 $templateInstance->assignVariable($variableName, $variableValue);
833 // Assign block content
834 $templateInstance->assignVariable('block_content', $blockContent);
836 // Compile template + variables
837 $templateInstance->compileTemplate();
838 $templateInstance->compileVariables();
841 array_push($this->menuBlocks, $templateInstance->getRawTemplateData());
843 // Reset rendered menu entries array
844 $this->menuEntries = array();
848 * "Getter" for menu content
850 * @return $menuContent Returned menu content
852 public function getMenuContent () {
853 // Implode menuBlocks
854 $menuContent = implode('', $this->menuBlocks);
857 $this->menuBlocks = array();
864 * Getter for menu cache file (FQFN)
866 * @return $fqfn Full-qualified file name of the menu cache
868 public function getMenuCacheFqfn () {
869 // Get the FQFN ready
870 $fqfn = sprintf('%s%s%s/%s.%s',
871 $this->getConfigInstance()->getConfigEntry('base_path'),
872 $this->getGenericBasePath(),
875 $this->getMenuInstance()->getMenuName() . ':' .
876 $this->__toString() . ':' .
877 $this->getMenuInstance()->__toString()
879 $this->getMenuInstance()->getMenuType()