3 namespace CoreFramework\Template\Engine;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Registry\Registry;
8 use CoreFramework\Template\CompileableTemplate;
11 * A Menu template engine class
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
34 * Main nodes in the XML tree ('menu' is ignored)
36 private $mainNodes = array(
41 * Sub nodes in the XML tree
43 private $subNodes = array(
69 * Variables for a menu entry
71 private $menuEntryVariables = array(
82 * Variables for a menu block
84 private $menuBlockVariables = array(
89 // Content is taken from menuEntries
97 * Rendered menu entries
99 private $menuEntries = array();
102 * Rendered menu blocks
104 private $menuBlocks = array();
109 private $menuInstance = NULL;
114 private $curr = array();
117 * Content from dependency
119 private $dependencyContent = array();
122 * Protected constructor
126 protected function __construct () {
127 // Call parent constructor
128 parent::__construct(__CLASS__);
132 * Creates an instance of the class TemplateEngine and prepares it for usage
134 * @param $menuInstance A RenderableMenu instance
135 * @return $templateInstance An instance of TemplateEngine
136 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
137 * @throws InvalidBasePathStringException If $templateBasePath is no string
138 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
139 * directory or not found
140 * @throws BasePathReadProtectedException If $templateBasePath is
143 public static final function createMenuTemplateEngine (RenderableMenu $menuInstance) {
144 // Get a new instance
145 $templateInstance = new MenuTemplateEngine();
147 // Get the application instance from registry
148 $applicationInstance = Registry::getRegistry()->getInstance('app');
150 // Determine base path
151 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
153 // Is the base path valid?
154 if (empty($templateBasePath)) {
155 // Base path is empty
156 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
157 } elseif (!is_string($templateBasePath)) {
159 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
160 } elseif (!is_dir($templateBasePath)) {
162 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
163 } elseif (!is_readable($templateBasePath)) {
165 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
169 $templateInstance->setTemplateBasePath($templateBasePath);
171 // Set template extensions
172 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
173 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('menu_template_extension'));
175 // Absolute output path for compiled templates
176 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
178 // Set the menu instance
179 $templateInstance->setMenuInstance($menuInstance);
181 // Init a variable stacker
182 $stackInstance = ObjectFactory::createObjectByConfiguredName('menu_stacker_class');
185 $templateInstance->setStackInstance($stackInstance);
187 // Return the prepared instance
188 return $templateInstance;
192 * Load a specified menu template into the engine
194 * @param $template The menu template we shall load which is
195 * located in 'menu' by default
198 public function loadMenuTemplate ($template) {
200 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
202 // Load the special template
203 $this->loadTemplate($template);
207 * Getter for current main node
209 * @return $currMainNode Current main node
211 public final function getCurrMainNode () {
212 return $this->curr['main_node'];
216 * Setter for current main node
218 * @param $element Element name to set as current main node
219 * @return $currMainNode Current main node
221 private final function setCurrMainNode ($element) {
222 $this->curr['main_node'] = (string) $element;
226 * Getter for main node array
228 * @return $mainNodes Array with valid main node names
230 public final function getMainNodes () {
231 return $this->mainNodes;
235 * Getter for sub node array
237 * @return $subNodes Array with valid sub node names
239 public final function getSubNodes () {
240 return $this->subNodes;
244 * Handles the start element of an XML resource
246 * @param $resource XML parser resource (currently ignored)
247 * @param $element The element we shall handle
248 * @param $attributes All attributes
250 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
252 public function startElement ($resource, $element, array $attributes) {
253 // Initial method name which will never be called...
254 $methodName = 'initMenu';
256 // Make the element name lower-case
257 $element = strtolower($element);
259 // Is the element a main node?
260 //* DEBUG: */ echo "START: >".$element."<<br />\n";
261 if (in_array($element, $this->getMainNodes())) {
262 // Okay, main node found!
263 $methodName = 'start' . self::convertToClassName($element);
266 $this->setCurrMainNode($element);
267 } elseif (in_array($element, $this->getSubNodes())) {
269 $methodName = 'start' . self::convertToClassName($element);
270 } elseif ($element != 'menu') {
271 // Invalid node name found
272 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
276 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
277 call_user_func_array(array($this, $methodName), $attributes);
281 * Ends the main or sub node by sending out the gathered data
283 * @param $resource An XML resource pointer (currently ignored)
284 * @param $nodeName Name of the node we want to finish
286 * @throws XmlNodeMismatchException If current main node mismatches the closing one
288 public function finishElement ($resource, $nodeName) {
289 // Make all lower-case
290 $nodeName = strtolower($nodeName);
292 // Does this match with current main node?
293 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
294 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
296 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
299 // Construct method name
300 $methodName = 'finish' . self::convertToClassName($nodeName);
302 // Call the corresponding method
303 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
304 call_user_func_array(array($this, $methodName), array());
310 * @param $resource XML parser resource (currently ignored)
311 * @param $characters Characters to handle
313 * @todo Find something useful with this!
315 public function characterHandler ($resource, $characters) {
316 // Trim all spaces away
317 $characters = trim($characters);
319 // Is this string empty?
320 if (empty($characters)) {
321 // Then skip it silently
325 // Assign the found characters to variable and use the last entry from
327 parent::assignVariable($this->getStackInstance()->getNamed('current_node'), $characters);
331 * Handles the template dependency for given node
333 * @param $node The node we should load a dependency template
334 * @param $templateDependency A template to load to satisfy dependencies
337 private function handleTemplateDependency ($node, $templateDependency) {
338 // Is the template dependency set?
339 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
340 // Get a temporay menu template instance
341 $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getMenuInstance()));
344 $templateInstance->loadMenuTemplate($templateDependency);
346 // Parse the XML content
347 $templateInstance->renderXmlContent();
349 // Save the parsed raw content in our dependency array
350 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
355 * Intializes the menu
357 * @param $templateDependency A template to load to satisfy dependencies
359 * @todo Add cache creation here
361 private function initMenu ($templateDependency = '') {
362 // Get web template engine
363 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('html_template_class', array($this->getApplicationInstance())));
365 // Handle the dependency template
366 $this->handleTemplateDependency('menu', $templateDependency);
368 // Push the node name on the stacker
369 $this->getStackInstance()->pushNamed('current_node', 'menu');
373 * Starts the menu entries
375 * @param $templateDependency A template to load to satisfy dependencies
378 private function startEntryList () {
379 // Push the node name on the stacker
380 $this->getStackInstance()->pushNamed('current_node', 'entry-list');
384 * Starts the menu block header
388 private function startBlockHeader () {
389 // Push the node name on the stacker
390 $this->getStackInstance()->pushNamed('current_node', 'block-header');
394 * Starts the menu block footer
398 private function startBlockFooter () {
399 // Push the node name on the stacker
400 $this->getStackInstance()->pushNamed('current_node', 'block-footer');
404 * Starts the menu property 'block-list'
408 private function startBlockList () {
409 // Push the node name on the stacker
410 $this->getStackInstance()->pushNamed('current_node', 'block-list');
414 * Starts the menu property 'block'
418 private function startBlock () {
419 // Push the node name on the stacker
420 $this->getStackInstance()->pushNamed('current_node', 'block');
424 * Starts the menu property 'title'
428 private function startTitle () {
429 // Push the node name on the stacker
430 $this->getStackInstance()->pushNamed('current_node', 'title');
434 * Starts the menu property 'title-id'
438 private function startTitleId () {
439 // Push the node name on the stacker
440 $this->getStackInstance()->pushNamed('current_node', 'title-id');
444 * Starts the menu property 'title-class'
448 private function startTitleClass () {
449 // Push the node name on the stacker
450 $this->getStackInstance()->pushNamed('current_node', 'title-class');
454 * Starts the menu property 'title-text'
458 private function startTitleText () {
459 // Push the node name on the stacker
460 $this->getStackInstance()->pushNamed('current_node', 'title-text');
464 * Starts the menu property 'entry'
468 private function startEntry () {
469 // Push the node name on the stacker
470 $this->getStackInstance()->pushNamed('current_node', 'entry');
474 * Starts the menu property 'entry-id'
478 private function startEntryId () {
479 // Push the node name on the stacker
480 $this->getStackInstance()->pushNamed('current_node', 'entry-id');
484 * Starts the menu property 'anchor'
488 private function startAnchor () {
489 // Push the node name on the stacker
490 $this->getStackInstance()->pushNamed('current_node', 'anchor');
494 * Starts the menu property 'anchor-id'
498 private function startAnchorId () {
499 // Push the node name on the stacker
500 $this->getStackInstance()->pushNamed('current_node', 'anchor-id');
504 * Starts the menu property 'anchor-text'
508 private function startAnchorText () {
509 // Push the node name on the stacker
510 $this->getStackInstance()->pushNamed('current_node', 'anchor-text');
514 * Starts the menu property 'anchor-title'
518 private function startAnchorTitle () {
519 // Push the node name on the stacker
520 $this->getStackInstance()->pushNamed('current_node', 'anchor-title');
524 * Starts the menu property 'anchor-href'
528 private function startAnchorHref () {
529 // Push the node name on the stacker
530 $this->getStackInstance()->pushNamed('current_node', 'anchor-href');
534 * Starts the menu property 'footer-id'
538 private function startFooterId () {
539 // Push the node name on the stacker
540 $this->getStackInstance()->pushNamed('current_node', 'footer-id');
544 * Starts the menu property 'footer-class'
548 private function startFooterClass () {
549 // Push the node name on the stacker
550 $this->getStackInstance()->pushNamed('current_node', 'footer-class');
554 * Starts the menu property 'footer-text'
558 private function startFooterText () {
559 // Push the node name on the stacker
560 $this->getStackInstance()->pushNamed('current_node', 'footer-text');
564 * Finishes the title node by added another template to the menu
568 private function finishTitle () {
569 // Pop the last entry
570 $this->getStackInstance()->popNamed('current_node');
574 * Finishes the title-id node by
578 private function finishTitleId () {
579 // Pop the last entry
580 $this->getStackInstance()->popNamed('current_node');
584 * Finishes the title-class node
588 private function finishTitleClass () {
589 // Pop the last entry
590 $this->getStackInstance()->popNamed('current_node');
594 * Finishes the title-class node
598 private function finishTitleText () {
599 // Pop the last entry
600 $this->getStackInstance()->popNamed('current_node');
604 * Finishes the footer-text node
608 private function finishFooterText () {
609 // Pop the last entry
610 $this->getStackInstance()->popNamed('current_node');
614 * Finishes the footer-class node
618 private function finishFooterClass () {
619 // Pop the last entry
620 $this->getStackInstance()->popNamed('current_node');
624 * Finishes the footer-id node
628 private function finishFooterId () {
629 // Pop the last entry
630 $this->getStackInstance()->popNamed('current_node');
634 * Finishes the anchor-href node
638 private function finishAnchorHref () {
639 // Pop the last entry
640 $this->getStackInstance()->popNamed('current_node');
644 * Finishes the anchor-title node
648 private function finishAnchorTitle () {
649 // Pop the last entry
650 $this->getStackInstance()->popNamed('current_node');
654 * Finishes the anchor-text node
658 private function finishAnchorText () {
659 // Pop the last entry
660 $this->getStackInstance()->popNamed('current_node');
664 * Finishes the anchor-id node
668 private function finishAnchorId () {
669 // Pop the last entry
670 $this->getStackInstance()->popNamed('current_node');
674 * Finishes the anchor node
678 private function finishAnchor () {
679 // Pop the last entry
680 $this->getStackInstance()->popNamed('current_node');
684 * Finishes the entry-id node
688 private function finishEntryId () {
689 // Pop the last entry
690 $this->getStackInstance()->popNamed('current_node');
694 * Finishes the entry node
698 private function finishEntry () {
699 // Pop the last entry
700 $this->getStackInstance()->popNamed('current_node');
702 // Render this menu entry
703 $this->renderMenuEntry();
707 * Finishes the block node
711 private function finishBlock () {
712 // Pop the last entry
713 $this->getStackInstance()->popNamed('current_node');
715 // Render this menu block
716 $this->renderMenuBlock();
720 * Finishes the block-list node
724 private function finishBlockList () {
725 // Pop the last entry
726 $this->getStackInstance()->popNamed('current_node');
730 * Finishes the menu entries
734 private function finishEntryList () {
735 // Pop the last entry
736 $this->getStackInstance()->popNamed('current_node');
740 * Finishes the menu block header
744 private function finishBlockHeader () {
745 // Pop the last entry
746 $this->getStackInstance()->popNamed('current_node');
750 * Finishes the menu block footer
754 private function finishBlockFooter () {
755 // Pop the last entry
756 $this->getStackInstance()->popNamed('current_node');
764 private function finishMenu () {
765 // Pop the last entry
766 $this->getStackInstance()->popNamed('current_node');
770 * Renders this menu entry, as every block all variables got overwritten
771 * with data from next entry.
775 private function renderMenuEntry () {
776 // Prepare template engine
777 $templateInstance = $this->prepareTemplateInstance();
779 // Load menu entry template
780 $templateInstance->loadCodeTemplate('menu_entry');
782 // Copy all variables over to it
783 foreach ($this->menuEntryVariables as $variableName) {
785 $variableValue = $this->readVariable($variableName);
787 // Is the key 'anchor-href'?
788 if ($variableName == 'anchor-href') {
789 // Expand variable with URL then
790 $variableValue = '{?base_url?}/' . $variableValue;
793 // ... into the instance
794 $templateInstance->assignVariable($variableName, $variableValue);
797 // Compile template + variables
798 $templateInstance->compileTemplate();
799 $templateInstance->compileVariables();
802 $this->menuEntries[$this->readVariable('entry_id')] = $templateInstance->getRawTemplateData();
806 * Renders this menu block, as next block all data is overwritten with
811 private function renderMenuBlock () {
812 // Init block content
813 $blockContent = implode('', $this->menuEntries);
815 // Prepare template engine
816 $templateInstance = $this->prepareTemplateInstance();
818 // Load menu entry template
819 $templateInstance->loadCodeTemplate('menu_block');
821 // Copy all variables over to it
822 foreach ($this->menuBlockVariables as $variableName) {
824 $variableValue = $this->readVariable($variableName);
826 // ... into the instance
827 $templateInstance->assignVariable($variableName, $variableValue);
830 // Assign block content
831 $templateInstance->assignVariable('block_content', $blockContent);
833 // Compile template + variables
834 $templateInstance->compileTemplate();
835 $templateInstance->compileVariables();
838 array_push($this->menuBlocks, $templateInstance->getRawTemplateData());
840 // Reset rendered menu entries array
841 $this->menuEntries = array();
845 * "Getter" for menu content
847 * @return $menuContent Returned menu content
849 public function getMenuContent () {
850 // Implode menuBlocks
851 $menuContent = implode('', $this->menuBlocks);
854 $this->menuBlocks = array();
861 * Getter for menu cache file (FQFN)
863 * @return $fqfn Full-qualified file name of the menu cache
865 public function getMenuCacheFqfn () {
866 // Get the FQFN ready
867 $fqfn = sprintf('%s%s%s/%s.%s',
868 $this->getConfigInstance()->getConfigEntry('base_path'),
869 $this->getGenericBasePath(),
872 $this->getMenuInstance()->getMenuName() . ':' .
873 $this->__toString() . ':' .
874 $this->getMenuInstance()->__toString()
876 $this->getMenuInstance()->getMenuType()