3 * A Menu template engine class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
26 * Main nodes in the XML tree ('menu' is ignored)
28 private $mainNodes = array(
33 * Sub nodes in the XML tree
35 private $subNodes = array(
63 private $menuInstance = null;
68 private $curr = array();
71 * Content from dependency
73 private $dependencyContent = array();
76 * Protected constructor
80 protected function __construct () {
81 // Call parent constructor
82 parent::__construct(__CLASS__);
86 * Creates an instance of the class TemplateEngine and prepares it for usage
88 * @param $appInstance A manageable application
89 * @param $menuInstance A RenderableMenu instance
90 * @return $tplInstance An instance of TemplateEngine
91 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
92 * @throws InvalidBasePathStringException If $templateBasePath is no string
93 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
94 * directory or not found
95 * @throws BasePathReadProtectedException If $templateBasePath is
98 public final static function createMenuTemplateEngine (ManageableApplication $appInstance, RenderableMenu $menuInstance) {
100 $tplInstance = new MenuTemplateEngine();
102 // Get language and file I/O instances from application
103 $langInstance = $appInstance->getLanguageInstance();
104 $ioInstance = $appInstance->getFileIoInstance();
106 // Determine base path
107 $templateBasePath = $tplInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
109 // Is the base path valid?
110 if (empty($templateBasePath)) {
111 // Base path is empty
112 throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
113 } elseif (!is_string($templateBasePath)) {
115 throw new InvalidBasePathStringException(array($tplInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
116 } elseif (!is_dir($templateBasePath)) {
118 throw new BasePathIsNoDirectoryException(array($tplInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
119 } elseif (!is_readable($templateBasePath)) {
121 throw new BasePathReadProtectedException(array($tplInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
124 // Get configuration instance
125 $configInstance = FrameworkConfiguration::getInstance();
128 $tplInstance->setTemplateBasePath($templateBasePath);
130 // Set the language and IO instances
131 $tplInstance->setLanguageInstance($langInstance);
132 $tplInstance->setFileIoInstance($ioInstance);
134 // Set template extensions
135 $tplInstance->setRawTemplateExtension($configInstance->getConfigEntry('raw_template_extension'));
136 $tplInstance->setCodeTemplateExtension($configInstance->getConfigEntry('menu_template_extension'));
138 // Absolute output path for compiled templates
139 $tplInstance->setCompileOutputPath($configInstance->getConfigEntry('base_path') . $configInstance->getConfigEntry('compile_output_path'));
141 // Set the menu instance
142 $tplInstance->setMenuInstance($menuInstance);
144 // Init a variable stacker
145 $stackerInstance = ObjectFactory::createObjectByConfiguredName('menu_stacker_class');
148 $tplInstance->setStackerInstance($stackerInstance);
150 // Return the prepared instance
155 * Load a specified menu template into the engine
157 * @param $template The menu template we shall load which is
158 * located in 'menu' by default
161 public function loadMenuTemplate ($template) {
163 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
165 // Load the special template
166 $this->loadTemplate($template);
170 * Getter for current main node
172 * @return $currMainNode Current main node
174 public final function getCurrMainNode () {
175 return $this->curr['main_node'];
179 * Setter for current main node
181 * @param $element Element name to set as current main node
182 * @return $currMainNode Current main node
184 private final function setCurrMainNode ($element) {
185 $this->curr['main_node'] = (string) $element;
189 * Getter for main node array
191 * @return $mainNodes Array with valid main node names
193 public final function getMainNodes () {
194 return $this->mainNodes;
198 * Getter for sub node array
200 * @return $subNodes Array with valid sub node names
202 public final function getSubNodes () {
203 return $this->subNodes;
207 * Handles the start element of an XML resource
209 * @param $resource XML parser resource (currently ignored)
210 * @param $element The element we shall handle
211 * @param $attributes All attributes
213 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
215 public function startElement ($resource, $element, array $attributes) {
216 // Initial method name which will never be called...
217 $methodName = 'initMenu';
219 // Make the element name lower-case
220 $element = strtolower($element);
222 // Is the element a main node?
223 //* DEBUG: */ echo "START: >".$element."<<br />\n";
224 if (in_array($element, $this->getMainNodes())) {
225 // Okay, main node found!
226 $methodName = 'start' . $this->convertToClassName($element);
229 $this->setCurrMainNode($element);
230 } elseif (in_array($element, $this->getSubNodes())) {
232 $methodName = 'start' . $this->convertToClassName($element);
233 } elseif ($element != 'menu') {
234 // Invalid node name found
235 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
239 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
240 call_user_func_array(array($this, $methodName), $attributes);
244 * Ends the main or sub node by sending out the gathered data
246 * @param $resource An XML resource pointer (currently ignored)
247 * @param $nodeName Name of the node we want to finish
249 * @throws XmlNodeMismatchException If current main node mismatches the closing one
251 public function endElement ($resource, $nodeName) {
252 // Make all lower-case
253 $nodeName = strtolower($nodeName);
255 // Does this match with current main node?
256 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
257 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
259 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
262 // Construct method name
263 $methodName = 'finish' . $this->convertToClassName($nodeName);
265 // Call the corresponding method
266 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
267 call_user_func_array(array($this, $methodName), array());
273 * @param $resource XML parser resource (currently ignored)
274 * @param $characters Characters to handle
276 * @todo Find something useful with this!
278 public function characterHandler ($resource, $characters) {
279 // Trim all spaces away
280 $characters = trim($characters);
282 // Is this string empty?
283 if (empty($characters)) {
284 // Then skip it silently
288 // Assign the found characters to variable and use the last entry from
290 parent::assignVariable($this->getStackerInstance()->getNamed('current_node'), $characters);
294 * Handles the template dependency for given node
296 * @param $node The node we should load a dependency template
297 * @param $templateDependency A template to load to satisfy dependencies
300 private function handleTemplateDependency ($node, $templateDependency) {
301 // Is the template dependency set?
302 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
303 // Get a temporay menu template instance
304 $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getApplicationInstance(), $this->getMenuInstance()));
307 $templateInstance->loadMenuTemplate($templateDependency);
309 // Get an XmlParser instance
310 $templateInstance->renderXmlContent();
312 // Parse the template's content contents
313 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
318 * Intializes the menu
320 * @param $templateDependency A template to load to satisfy dependencies
322 * @todo Add cache creation here
324 private function initMenu ($templateDependency = '') {
325 // Get web template engine
326 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('web_template_class', array($this->getApplicationInstance())));
328 // Handle the dependency template
329 $this->handleTemplateDependency('menu', $templateDependency);
331 // Load the header template for this page
332 $this->getTemplateInstance()->loadCodeTemplate('menu_global_start');
334 // Set the variable group to page
335 $this->setVariableGroup('menu');
337 // Set its content in this template instance
338 $this->assignVariable('menu_start', $this->getTemplateInstance()->getRawTemplateData());
346 private function finishMenu () {
347 // Load the header template for this page
348 $this->getTemplateInstance()->loadCodeTemplate('menu_global_end');
350 // Set the variable group to page
351 $this->setVariableGroup('menu');
353 // Set its content in this template instance
354 $this->assignVariable('menu_end', $this->getTemplateInstance()->getRawTemplateData());
358 * Starts the menu entries by loading a (maybe) provided template dependency
360 * @param $templateDependency A template to load to satisfy dependencies
363 private function startEntryList ($templateDependency = '') {
364 // Handle the dependency template
365 $this->handleTemplateDependency('entries', $templateDependency);
367 // Load the header template for this page
368 $this->getTemplateInstance()->loadCodeTemplate('menu_entries_start');
370 // Set the variable group to page
371 $this->setVariableGroup('menu');
373 // Set its content in this template instance
374 $this->assignVariable('entries_start', $this->getTemplateInstance()->getRawTemplateData());
378 * Finishes the menu entries
382 private function finishEntryList () {
383 // Load the header template for this page
384 $this->getTemplateInstance()->loadCodeTemplate('menu_entries_end');
386 // Set the variable group to page
387 $this->setVariableGroup('menu');
389 // Set its content in this template instance
390 $this->assignVariable('entries_end', $this->getTemplateInstance()->getRawTemplateData());
394 * Starts the menu block header
398 private function startBlockHeader () {
399 // Do we have a template instance?
400 if (is_null($this->getTemplateInstance())) {
401 // Init template instance for underlaying web templates
402 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
404 // Set it in this template engine
405 $this->setTemplateInstance($templateInstance);
408 // Load the header template for this page
409 $this->getTemplateInstance()->loadCodeTemplate('menu_header_start');
411 // Set the variable group to page
412 $this->setVariableGroup('menu');
414 // Set its content in this template instance
415 $this->assignVariable('header', $this->getTemplateInstance()->getRawTemplateData());
419 * Finishes the menu block header
423 private function finishBlockHeader () {
424 // Load the header template for this page
425 $this->getTemplateInstance()->loadCodeTemplate('menu_header_end');
427 // Set the variable group to page
428 $this->setVariableGroup('menu');
430 // Set its content in this template instance
431 $this->assignVariable('header_end', $this->getTemplateInstance()->getRawTemplateData());
435 * Starts the menu block footer
439 private function startBlockFooter () {
440 // Do we have a template instance?
441 if (is_null($this->getTemplateInstance())) {
442 // Init template instance for underlaying web templates
443 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
445 // Set it in this template engine
446 $this->setTemplateInstance($templateInstance);
449 // Load the footer template for this page
450 $this->getTemplateInstance()->loadCodeTemplate('menu_footer_start');
452 // Set the variable group to page
453 $this->setVariableGroup('menu');
455 // Set its content in this template instance
456 $this->assignVariable('footer', $this->getTemplateInstance()->getRawTemplateData());
460 * Finishes the menu block footer
464 private function finishBlockFooter () {
465 // Load the footer template for this page
466 $this->getTemplateInstance()->loadCodeTemplate('menu_footer_end');
468 // Set the variable group to page
469 $this->setVariableGroup('menu');
471 // Set its content in this template instance
472 $this->assignVariable('footer_end', $this->getTemplateInstance()->getRawTemplateData());
476 * Starts the menu property 'block-list'
480 private function startBlockList () {
481 // Push the node name on the stacker
482 $this->getStackerInstance()->pushNamed('current_node', 'block-list');
486 * Starts the menu property 'block'
490 private function startBlock () {
491 // Push the node name on the stacker
492 $this->getStackerInstance()->pushNamed('current_node', 'block');
496 * Starts the menu property 'title'
500 private function startTitle () {
501 // Push the node name on the stacker
502 $this->getStackerInstance()->pushNamed('current_node', 'title');
506 * Starts the menu property 'title-id'
510 private function startTitleId () {
511 // Push the node name on the stacker
512 $this->getStackerInstance()->pushNamed('current_node', 'title-id');
516 * Starts the menu property 'title-class'
520 private function startTitleClass () {
521 // Push the node name on the stacker
522 $this->getStackerInstance()->pushNamed('current_node', 'title-class');
526 * Starts the menu property 'title-text'
530 private function startTitleText () {
531 // Push the node name on the stacker
532 $this->getStackerInstance()->pushNamed('current_node', 'title-text');
536 * Starts the menu property 'entry'
540 private function startEntry () {
541 // Push the node name on the stacker
542 $this->getStackerInstance()->pushNamed('current_node', 'entry');
546 * Starts the menu property 'entry-id'
550 private function startEntryId () {
551 // Push the node name on the stacker
552 $this->getStackerInstance()->pushNamed('current_node', 'entry-id');
556 * Starts the menu property 'anchor'
560 private function startAnchor () {
561 // Push the node name on the stacker
562 $this->getStackerInstance()->pushNamed('current_node', 'anchor');
566 * Starts the menu property 'anchor-id'
570 private function startAnchorId () {
571 // Push the node name on the stacker
572 $this->getStackerInstance()->pushNamed('current_node', 'anchor-id');
576 * Starts the menu property 'anchor-text'
580 private function startAnchorText () {
581 // Push the node name on the stacker
582 $this->getStackerInstance()->pushNamed('current_node', 'anchor-text');
586 * Starts the menu property 'anchor-title'
590 private function startAnchorTitle () {
591 // Push the node name on the stacker
592 $this->getStackerInstance()->pushNamed('current_node', 'anchor-title');
596 * Starts the menu property 'anchor-href'
600 private function startAnchorHref () {
601 // Push the node name on the stacker
602 $this->getStackerInstance()->pushNamed('current_node', 'anchor-href');
606 * Starts the menu property 'footer-id'
610 private function startFooterId () {
611 // Push the node name on the stacker
612 $this->getStackerInstance()->pushNamed('current_node', 'footer-id');
616 * Starts the menu property 'footer-class'
620 private function startFooterClass () {
621 // Push the node name on the stacker
622 $this->getStackerInstance()->pushNamed('current_node', 'footer-class');
626 * Starts the menu property 'footer-text'
630 private function startFooterText () {
631 // Push the node name on the stacker
632 $this->getStackerInstance()->pushNamed('current_node', 'footer-text');
636 * Finishes the title node by added another template to the menu
640 private function finishTitle () {
641 // Pop the last entry
642 $this->getStackerInstance()->popNamed('current_node');
646 * Finishes the title-id node by
650 private function finishTitleId () {
651 // Pop the last entry
652 $this->getStackerInstance()->popNamed('current_node');
656 * Finishes the title-class node
660 private function finishTitleClass () {
661 // Pop the last entry
662 $this->getStackerInstance()->popNamed('current_node');
666 * Finishes the title-class node
670 private function finishTitleText () {
671 // Pop the last entry
672 $this->getStackerInstance()->popNamed('current_node');
676 * Finishes the footer-text node
680 private function finishFooterText () {
681 // Pop the last entry
682 $this->getStackerInstance()->popNamed('current_node');
686 * Finishes the footer-class node
690 private function finishFooterClass () {
691 // Pop the last entry
692 $this->getStackerInstance()->popNamed('current_node');
696 * Finishes the footer-id node
700 private function finishFooterId () {
701 // Pop the last entry
702 $this->getStackerInstance()->popNamed('current_node');
706 * Finishes the anchor-href node
710 private function finishAnchorHref () {
711 // Pop the last entry
712 $this->getStackerInstance()->popNamed('current_node');
716 * Finishes the anchor-title node
720 private function finishAnchorTitle () {
721 // Pop the last entry
722 $this->getStackerInstance()->popNamed('current_node');
726 * Finishes the anchor-text node
730 private function finishAnchorText () {
731 // Pop the last entry
732 $this->getStackerInstance()->popNamed('current_node');
736 * Finishes the anchor-id node
740 private function finishAnchorId () {
741 // Pop the last entry
742 $this->getStackerInstance()->popNamed('current_node');
746 * Finishes the anchor node
750 private function finishAnchor () {
751 // Pop the last entry
752 $this->getStackerInstance()->popNamed('current_node');
756 * Finishes the entry-id node
760 private function finishEntryId () {
761 // Pop the last entry
762 $this->getStackerInstance()->popNamed('current_node');
766 * Finishes the entry node
770 private function finishEntry () {
771 // Pop the last entry
772 $this->getStackerInstance()->popNamed('current_node');
776 * Finishes the block node
780 private function finishBlock () {
781 // Pop the last entry
782 $this->getStackerInstance()->popNamed('current_node');
786 * Finishes the block-list node
790 private function finishBlockList () {
791 // Pop the last entry
792 $this->getStackerInstance()->popNamed('current_node');
796 * Starts the menu text
800 private function startText () {
801 // Do we have a template instance?
802 if (is_null($this->getTemplateInstance())) {
803 // Init template instance for underlaying web templates
804 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
806 // Set it in this template engine
807 $this->setTemplateInstance($templateInstance);
810 // Load the text template for this page
811 $this->getTemplateInstance()->loadCodeTemplate('menu_text_start');
813 // Set the variable group to page
814 $this->setVariableGroup('menu');
816 // Set its content in this template instance
817 $this->assignVariable('text', $this->getTemplateInstance()->getRawTemplateData());
821 * Finishes the menu text
825 private function finishText () {
826 // Load the text template for this page
827 $this->getTemplateInstance()->loadCodeTemplate('menu_text_end');
829 // Set the variable group to page
830 $this->setVariableGroup('menu');
832 // Set its content in this template instance
833 $this->assignVariable('text_end', $this->getTemplateInstance()->getRawTemplateData());
837 * Getter for menu cache file (FQFN)
839 * @return $fqfn Full-qualified file name of the menu cache
841 public function getMenuCacheFqfn () {
842 // Get the FQFN ready
843 $fqfn = sprintf("%s%s%s/%s.%s",
844 $this->getConfigInstance()->getConfigEntry('base_path'),
845 $this->getGenericBasePath(),
848 $this->getMenuInstance()->getMenuName() . ':' .
849 $this->__toString() . ':' .
850 $this->getMenuInstance()->__toString()
852 $this->getMenuInstance()->getMenuType()