3 namespace CoreFramework\Template\Engine;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Filesystem\InvalidDirectoryException;
8 use CoreFramework\Parser\Xml\XmlParser;
9 use CoreFramework\Registry\Registry;
10 use CoreFramework\Template\CompileableTemplate;
13 use \UnexpectedValueException;
16 * A Menu template engine class
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
39 * Main nodes in the XML tree ('menu' is ignored)
41 private $mainNodes = array(
46 * Sub nodes in the XML tree
48 private $subNodes = array(
74 * Variables for a menu entry
76 private $menuEntryVariables = array(
87 * Variables for a menu block
89 private $menuBlockVariables = array(
94 // Content is taken from menuEntries
102 * Rendered menu entries
104 private $menuEntries = array();
107 * Rendered menu blocks
109 private $menuBlocks = array();
114 private $menuInstance = NULL;
119 private $curr = array();
122 * Content from dependency
124 private $dependencyContent = array();
127 * Protected constructor
131 protected function __construct () {
132 // Call parent constructor
133 parent::__construct(__CLASS__);
137 * Creates an instance of the class TemplateEngine and prepares it for usage
139 * @param $menuInstance A RenderableMenu instance
140 * @return $templateInstance An instance of TemplateEngine
141 * @throws UnexpectedValueException If the found $templateBasePath is empty or not a string
142 * @throws InvalidDirectoryException If $templateBasePath is no 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->getAppShortName(). '/';
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 InvalidDirectoryException(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(sprintf('%s%s/',
181 $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path')
184 // Set the menu instance
185 $templateInstance->setMenuInstance($menuInstance);
187 // Init a variable stacker
188 $stackInstance = ObjectFactory::createObjectByConfiguredName('menu_stacker_class');
191 $templateInstance->setStackInstance($stackInstance);
193 // Return the prepared instance
194 return $templateInstance;
198 * Load a specified menu template into the engine
200 * @param $template The menu template we shall load which is
201 * located in 'menu' by default
204 public function loadMenuTemplate ($template) {
206 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
208 // Load the special template
209 $this->loadTemplate($template);
213 * Getter for current main node
215 * @return $currMainNode Current main node
217 public final function getCurrMainNode () {
218 return $this->curr['main_node'];
222 * Setter for current main node
224 * @param $element Element name to set as current main node
225 * @return $currMainNode Current main node
227 private final function setCurrMainNode ($element) {
228 $this->curr['main_node'] = (string) $element;
232 * Getter for main node array
234 * @return $mainNodes Array with valid main node names
236 public final function getMainNodes () {
237 return $this->mainNodes;
241 * Getter for sub node array
243 * @return $subNodes Array with valid sub node names
245 public final function getSubNodes () {
246 return $this->subNodes;
250 * Handles the start element of an XML resource
252 * @param $resource XML parser resource (currently ignored)
253 * @param $element The element we shall handle
254 * @param $attributes All attributes
256 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
258 public function startElement ($resource, $element, array $attributes) {
259 // Initial method name which will never be called...
260 $methodName = 'initMenu';
262 // Make the element name lower-case
263 $element = strtolower($element);
265 // Is the element a main node?
266 //* DEBUG: */ echo "START: >".$element."<<br />\n";
267 if (in_array($element, $this->getMainNodes())) {
268 // Okay, main node found!
269 $methodName = 'start' . self::convertToClassName($element);
272 $this->setCurrMainNode($element);
273 } elseif (in_array($element, $this->getSubNodes())) {
275 $methodName = 'start' . self::convertToClassName($element);
276 } elseif ($element != 'menu') {
277 // Invalid node name found
278 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
282 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
283 call_user_func_array(array($this, $methodName), $attributes);
287 * Ends the main or sub node by sending out the gathered data
289 * @param $resource An XML resource pointer (currently ignored)
290 * @param $nodeName Name of the node we want to finish
292 * @throws XmlNodeMismatchException If current main node mismatches the closing one
294 public function finishElement ($resource, $nodeName) {
295 // Make all lower-case
296 $nodeName = strtolower($nodeName);
298 // Does this match with current main node?
299 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
300 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
302 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
305 // Construct method name
306 $methodName = 'finish' . self::convertToClassName($nodeName);
308 // Call the corresponding method
309 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
310 call_user_func_array(array($this, $methodName), array());
316 * @param $resource XML parser resource (currently ignored)
317 * @param $characters Characters to handle
319 * @todo Find something useful with this!
321 public function characterHandler ($resource, $characters) {
322 // Trim all spaces away
323 $characters = trim($characters);
325 // Is this string empty?
326 if (empty($characters)) {
327 // Then skip it silently
331 // Assign the found characters to variable and use the last entry from
333 parent::assignVariable($this->getStackInstance()->getNamed('current_node'), $characters);
337 * Handles the template dependency for given node
339 * @param $node The node we should load a dependency template
340 * @param $templateDependency A template to load to satisfy dependencies
343 private function handleTemplateDependency ($node, $templateDependency) {
344 // Is the template dependency set?
345 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
346 // Get a temporay menu template instance
347 $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getMenuInstance()));
350 $templateInstance->loadMenuTemplate($templateDependency);
352 // Parse the XML content
353 $templateInstance->renderXmlContent();
355 // Save the parsed raw content in our dependency array
356 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
361 * Intializes the menu
363 * @param $templateDependency A template to load to satisfy dependencies
365 * @todo Add cache creation here
367 private function initMenu ($templateDependency = '') {
368 // Get web template engine
369 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('html_template_class', array(Registry::getRegistry()->getInstance('app'))));
371 // Handle the dependency template
372 $this->handleTemplateDependency('menu', $templateDependency);
374 // Push the node name on the stacker
375 $this->getStackInstance()->pushNamed('current_node', 'menu');
379 * Starts the menu entries
381 * @param $templateDependency A template to load to satisfy dependencies
384 private function startEntryList () {
385 // Push the node name on the stacker
386 $this->getStackInstance()->pushNamed('current_node', 'entry-list');
390 * Starts the menu block header
394 private function startBlockHeader () {
395 // Push the node name on the stacker
396 $this->getStackInstance()->pushNamed('current_node', 'block-header');
400 * Starts the menu block footer
404 private function startBlockFooter () {
405 // Push the node name on the stacker
406 $this->getStackInstance()->pushNamed('current_node', 'block-footer');
410 * Starts the menu property 'block-list'
414 private function startBlockList () {
415 // Push the node name on the stacker
416 $this->getStackInstance()->pushNamed('current_node', 'block-list');
420 * Starts the menu property 'block'
424 private function startBlock () {
425 // Push the node name on the stacker
426 $this->getStackInstance()->pushNamed('current_node', 'block');
430 * Starts the menu property 'title'
434 private function startTitle () {
435 // Push the node name on the stacker
436 $this->getStackInstance()->pushNamed('current_node', 'title');
440 * Starts the menu property 'title-id'
444 private function startTitleId () {
445 // Push the node name on the stacker
446 $this->getStackInstance()->pushNamed('current_node', 'title-id');
450 * Starts the menu property 'title-class'
454 private function startTitleClass () {
455 // Push the node name on the stacker
456 $this->getStackInstance()->pushNamed('current_node', 'title-class');
460 * Starts the menu property 'title-text'
464 private function startTitleText () {
465 // Push the node name on the stacker
466 $this->getStackInstance()->pushNamed('current_node', 'title-text');
470 * Starts the menu property 'entry'
474 private function startEntry () {
475 // Push the node name on the stacker
476 $this->getStackInstance()->pushNamed('current_node', 'entry');
480 * Starts the menu property 'entry-id'
484 private function startEntryId () {
485 // Push the node name on the stacker
486 $this->getStackInstance()->pushNamed('current_node', 'entry-id');
490 * Starts the menu property 'anchor'
494 private function startAnchor () {
495 // Push the node name on the stacker
496 $this->getStackInstance()->pushNamed('current_node', 'anchor');
500 * Starts the menu property 'anchor-id'
504 private function startAnchorId () {
505 // Push the node name on the stacker
506 $this->getStackInstance()->pushNamed('current_node', 'anchor-id');
510 * Starts the menu property 'anchor-text'
514 private function startAnchorText () {
515 // Push the node name on the stacker
516 $this->getStackInstance()->pushNamed('current_node', 'anchor-text');
520 * Starts the menu property 'anchor-title'
524 private function startAnchorTitle () {
525 // Push the node name on the stacker
526 $this->getStackInstance()->pushNamed('current_node', 'anchor-title');
530 * Starts the menu property 'anchor-href'
534 private function startAnchorHref () {
535 // Push the node name on the stacker
536 $this->getStackInstance()->pushNamed('current_node', 'anchor-href');
540 * Starts the menu property 'footer-id'
544 private function startFooterId () {
545 // Push the node name on the stacker
546 $this->getStackInstance()->pushNamed('current_node', 'footer-id');
550 * Starts the menu property 'footer-class'
554 private function startFooterClass () {
555 // Push the node name on the stacker
556 $this->getStackInstance()->pushNamed('current_node', 'footer-class');
560 * Starts the menu property 'footer-text'
564 private function startFooterText () {
565 // Push the node name on the stacker
566 $this->getStackInstance()->pushNamed('current_node', 'footer-text');
570 * Finishes the title node by added another template to the menu
574 private function finishTitle () {
575 // Pop the last entry
576 $this->getStackInstance()->popNamed('current_node');
580 * Finishes the title-id node by
584 private function finishTitleId () {
585 // Pop the last entry
586 $this->getStackInstance()->popNamed('current_node');
590 * Finishes the title-class node
594 private function finishTitleClass () {
595 // Pop the last entry
596 $this->getStackInstance()->popNamed('current_node');
600 * Finishes the title-class node
604 private function finishTitleText () {
605 // Pop the last entry
606 $this->getStackInstance()->popNamed('current_node');
610 * Finishes the footer-text node
614 private function finishFooterText () {
615 // Pop the last entry
616 $this->getStackInstance()->popNamed('current_node');
620 * Finishes the footer-class node
624 private function finishFooterClass () {
625 // Pop the last entry
626 $this->getStackInstance()->popNamed('current_node');
630 * Finishes the footer-id node
634 private function finishFooterId () {
635 // Pop the last entry
636 $this->getStackInstance()->popNamed('current_node');
640 * Finishes the anchor-href node
644 private function finishAnchorHref () {
645 // Pop the last entry
646 $this->getStackInstance()->popNamed('current_node');
650 * Finishes the anchor-title node
654 private function finishAnchorTitle () {
655 // Pop the last entry
656 $this->getStackInstance()->popNamed('current_node');
660 * Finishes the anchor-text node
664 private function finishAnchorText () {
665 // Pop the last entry
666 $this->getStackInstance()->popNamed('current_node');
670 * Finishes the anchor-id node
674 private function finishAnchorId () {
675 // Pop the last entry
676 $this->getStackInstance()->popNamed('current_node');
680 * Finishes the anchor node
684 private function finishAnchor () {
685 // Pop the last entry
686 $this->getStackInstance()->popNamed('current_node');
690 * Finishes the entry-id node
694 private function finishEntryId () {
695 // Pop the last entry
696 $this->getStackInstance()->popNamed('current_node');
700 * Finishes the entry node
704 private function finishEntry () {
705 // Pop the last entry
706 $this->getStackInstance()->popNamed('current_node');
708 // Render this menu entry
709 $this->renderMenuEntry();
713 * Finishes the block node
717 private function finishBlock () {
718 // Pop the last entry
719 $this->getStackInstance()->popNamed('current_node');
721 // Render this menu block
722 $this->renderMenuBlock();
726 * Finishes the block-list node
730 private function finishBlockList () {
731 // Pop the last entry
732 $this->getStackInstance()->popNamed('current_node');
736 * Finishes the menu entries
740 private function finishEntryList () {
741 // Pop the last entry
742 $this->getStackInstance()->popNamed('current_node');
746 * Finishes the menu block header
750 private function finishBlockHeader () {
751 // Pop the last entry
752 $this->getStackInstance()->popNamed('current_node');
756 * Finishes the menu block footer
760 private function finishBlockFooter () {
761 // Pop the last entry
762 $this->getStackInstance()->popNamed('current_node');
770 private function finishMenu () {
771 // Pop the last entry
772 $this->getStackInstance()->popNamed('current_node');
776 * Renders this menu entry, as every block all variables got overwritten
777 * with data from next entry.
781 private function renderMenuEntry () {
782 // Prepare template engine
783 $templateInstance = $this->prepareTemplateInstance();
785 // Load menu entry template
786 $templateInstance->loadCodeTemplate('menu_entry');
788 // Copy all variables over to it
789 foreach ($this->menuEntryVariables as $variableName) {
791 $variableValue = $this->readVariable($variableName);
793 // Is the key 'anchor-href'?
794 if ($variableName == 'anchor-href') {
795 // Expand variable with URL then
796 $variableValue = '{?base_url?}/' . $variableValue;
799 // ... into the instance
800 $templateInstance->assignVariable($variableName, $variableValue);
803 // Compile template + variables
804 $templateInstance->compileTemplate();
805 $templateInstance->compileVariables();
808 $this->menuEntries[$this->readVariable('entry_id')] = $templateInstance->getRawTemplateData();
812 * Renders this menu block, as next block all data is overwritten with
817 private function renderMenuBlock () {
818 // Init block content
819 $blockContent = implode('', $this->menuEntries);
821 // Prepare template engine
822 $templateInstance = $this->prepareTemplateInstance();
824 // Load menu entry template
825 $templateInstance->loadCodeTemplate('menu_block');
827 // Copy all variables over to it
828 foreach ($this->menuBlockVariables as $variableName) {
830 $variableValue = $this->readVariable($variableName);
832 // ... into the instance
833 $templateInstance->assignVariable($variableName, $variableValue);
836 // Assign block content
837 $templateInstance->assignVariable('block_content', $blockContent);
839 // Compile template + variables
840 $templateInstance->compileTemplate();
841 $templateInstance->compileVariables();
844 array_push($this->menuBlocks, $templateInstance->getRawTemplateData());
846 // Reset rendered menu entries array
847 $this->menuEntries = array();
851 * "Getter" for menu content
853 * @return $menuContent Returned menu content
855 public function getMenuContent () {
856 // Implode menuBlocks
857 $menuContent = implode('', $this->menuBlocks);
860 $this->menuBlocks = array();
867 * Getter for menu cache file (FQFN)
869 * @return $fqfn Full-qualified file name of the menu cache
871 public function getMenuCacheFqfn () {
872 // Get the application instance from registry
873 $applicationInstance = Registry::getRegistry()->getInstance('app');
875 // Get the FQFN ready
876 $fqfn = sprintf('%s%smenus/_cache/%s.%s',
877 $this->getConfigInstance()->getConfigEntry('application_base_path'),
878 $applicationInstance->getAppShortName(),
880 $this->getMenuInstance()->getMenuName() . ':' .
881 $this->__toString() . ':' .
882 $this->getMenuInstance()->__toString()
884 $this->getMenuInstance()->getMenuType()