3 * A Menu template engine class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 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 $templateInstance 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 static final function createMenuTemplateEngine (ManageableApplication $appInstance, RenderableMenu $menuInstance) {
100 $templateInstance = new MenuTemplateEngine();
102 // Determine base path
103 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
105 // Is the base path valid?
106 if (empty($templateBasePath)) {
107 // Base path is empty
108 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
109 } elseif (!is_string($templateBasePath)) {
111 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
112 } elseif (!is_dir($templateBasePath)) {
114 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
115 } elseif (!is_readable($templateBasePath)) {
117 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
121 $templateInstance->setTemplateBasePath($templateBasePath);
123 // Set template extensions
124 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
125 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('menu_template_extension'));
127 // Absolute output path for compiled templates
128 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
130 // Set the menu instance
131 $templateInstance->setMenuInstance($menuInstance);
133 // Init a variable stacker
134 $stackerInstance = ObjectFactory::createObjectByConfiguredName('menu_stacker_class');
137 $templateInstance->setStackerInstance($stackerInstance);
139 // Return the prepared instance
140 return $templateInstance;
144 * Load a specified menu template into the engine
146 * @param $template The menu template we shall load which is
147 * located in 'menu' by default
150 public function loadMenuTemplate ($template) {
152 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
154 // Load the special template
155 $this->loadTemplate($template);
159 * Getter for current main node
161 * @return $currMainNode Current main node
163 public final function getCurrMainNode () {
164 return $this->curr['main_node'];
168 * Setter for current main node
170 * @param $element Element name to set as current main node
171 * @return $currMainNode Current main node
173 private final function setCurrMainNode ($element) {
174 $this->curr['main_node'] = (string) $element;
178 * Getter for main node array
180 * @return $mainNodes Array with valid main node names
182 public final function getMainNodes () {
183 return $this->mainNodes;
187 * Getter for sub node array
189 * @return $subNodes Array with valid sub node names
191 public final function getSubNodes () {
192 return $this->subNodes;
196 * Handles the start element of an XML resource
198 * @param $resource XML parser resource (currently ignored)
199 * @param $element The element we shall handle
200 * @param $attributes All attributes
202 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
204 public function startElement ($resource, $element, array $attributes) {
205 // Initial method name which will never be called...
206 $methodName = 'initMenu';
208 // Make the element name lower-case
209 $element = strtolower($element);
211 // Is the element a main node?
212 //* DEBUG: */ echo "START: >".$element."<<br />\n";
213 if (in_array($element, $this->getMainNodes())) {
214 // Okay, main node found!
215 $methodName = 'start' . $this->convertToClassName($element);
218 $this->setCurrMainNode($element);
219 } elseif (in_array($element, $this->getSubNodes())) {
221 $methodName = 'start' . $this->convertToClassName($element);
222 } elseif ($element != 'menu') {
223 // Invalid node name found
224 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
228 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
229 call_user_func_array(array($this, $methodName), $attributes);
233 * Ends the main or sub node by sending out the gathered data
235 * @param $resource An XML resource pointer (currently ignored)
236 * @param $nodeName Name of the node we want to finish
238 * @throws XmlNodeMismatchException If current main node mismatches the closing one
240 public function endElement ($resource, $nodeName) {
241 // Make all lower-case
242 $nodeName = strtolower($nodeName);
244 // Does this match with current main node?
245 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
246 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
248 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
251 // Construct method name
252 $methodName = 'finish' . $this->convertToClassName($nodeName);
254 // Call the corresponding method
255 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
256 call_user_func_array(array($this, $methodName), array());
262 * @param $resource XML parser resource (currently ignored)
263 * @param $characters Characters to handle
265 * @todo Find something useful with this!
267 public function characterHandler ($resource, $characters) {
268 // Trim all spaces away
269 $characters = trim($characters);
271 // Is this string empty?
272 if (empty($characters)) {
273 // Then skip it silently
277 // Assign the found characters to variable and use the last entry from
279 parent::assignVariable($this->getStackerInstance()->getNamed('current_node'), $characters);
283 * Handles the template dependency for given node
285 * @param $node The node we should load a dependency template
286 * @param $templateDependency A template to load to satisfy dependencies
289 private function handleTemplateDependency ($node, $templateDependency) {
290 // Is the template dependency set?
291 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
292 // Get a temporay menu template instance
293 $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getApplicationInstance(), $this->getMenuInstance()));
296 $templateInstance->loadMenuTemplate($templateDependency);
298 // Get an XmlParser instance
299 $templateInstance->renderXmlContent();
301 // Parse the template's content contents
302 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
307 * Intializes the menu
309 * @param $templateDependency A template to load to satisfy dependencies
311 * @todo Add cache creation here
313 private function initMenu ($templateDependency = '') {
314 // Get web template engine
315 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('web_template_class', array($this->getApplicationInstance())));
317 // Handle the dependency template
318 $this->handleTemplateDependency('menu', $templateDependency);
320 // Push the node name on the stacker
321 $this->getStackerInstance()->pushNamed('current_node', 'menu');
325 * Starts the menu entries
327 * @param $templateDependency A template to load to satisfy dependencies
330 private function startEntryList () {
331 // Push the node name on the stacker
332 $this->getStackerInstance()->pushNamed('current_node', 'entry-list');
336 * Starts the menu block header
340 private function startBlockHeader () {
341 // Push the node name on the stacker
342 $this->getStackerInstance()->pushNamed('current_node', 'block-header');
346 * Starts the menu block footer
350 private function startBlockFooter () {
351 // Push the node name on the stacker
352 $this->getStackerInstance()->pushNamed('current_node', 'block-footer');
356 * Starts the menu property 'block-list'
360 private function startBlockList () {
361 // Push the node name on the stacker
362 $this->getStackerInstance()->pushNamed('current_node', 'block-list');
366 * Starts the menu property 'block'
370 private function startBlock () {
371 // Push the node name on the stacker
372 $this->getStackerInstance()->pushNamed('current_node', 'block');
376 * Starts the menu property 'title'
380 private function startTitle () {
381 // Push the node name on the stacker
382 $this->getStackerInstance()->pushNamed('current_node', 'title');
386 * Starts the menu property 'title-id'
390 private function startTitleId () {
391 // Push the node name on the stacker
392 $this->getStackerInstance()->pushNamed('current_node', 'title-id');
396 * Starts the menu property 'title-class'
400 private function startTitleClass () {
401 // Push the node name on the stacker
402 $this->getStackerInstance()->pushNamed('current_node', 'title-class');
406 * Starts the menu property 'title-text'
410 private function startTitleText () {
411 // Push the node name on the stacker
412 $this->getStackerInstance()->pushNamed('current_node', 'title-text');
416 * Starts the menu property 'entry'
420 private function startEntry () {
421 // Push the node name on the stacker
422 $this->getStackerInstance()->pushNamed('current_node', 'entry');
426 * Starts the menu property 'entry-id'
430 private function startEntryId () {
431 // Push the node name on the stacker
432 $this->getStackerInstance()->pushNamed('current_node', 'entry-id');
436 * Starts the menu property 'anchor'
440 private function startAnchor () {
441 // Push the node name on the stacker
442 $this->getStackerInstance()->pushNamed('current_node', 'anchor');
446 * Starts the menu property 'anchor-id'
450 private function startAnchorId () {
451 // Push the node name on the stacker
452 $this->getStackerInstance()->pushNamed('current_node', 'anchor-id');
456 * Starts the menu property 'anchor-text'
460 private function startAnchorText () {
461 // Push the node name on the stacker
462 $this->getStackerInstance()->pushNamed('current_node', 'anchor-text');
466 * Starts the menu property 'anchor-title'
470 private function startAnchorTitle () {
471 // Push the node name on the stacker
472 $this->getStackerInstance()->pushNamed('current_node', 'anchor-title');
476 * Starts the menu property 'anchor-href'
480 private function startAnchorHref () {
481 // Push the node name on the stacker
482 $this->getStackerInstance()->pushNamed('current_node', 'anchor-href');
486 * Starts the menu property 'footer-id'
490 private function startFooterId () {
491 // Push the node name on the stacker
492 $this->getStackerInstance()->pushNamed('current_node', 'footer-id');
496 * Starts the menu property 'footer-class'
500 private function startFooterClass () {
501 // Push the node name on the stacker
502 $this->getStackerInstance()->pushNamed('current_node', 'footer-class');
506 * Starts the menu property 'footer-text'
510 private function startFooterText () {
511 // Push the node name on the stacker
512 $this->getStackerInstance()->pushNamed('current_node', 'footer-text');
516 * Finishes the title node by added another template to the menu
520 private function finishTitle () {
521 // Pop the last entry
522 $this->getStackerInstance()->popNamed('current_node');
526 * Finishes the title-id node by
530 private function finishTitleId () {
531 // Pop the last entry
532 $this->getStackerInstance()->popNamed('current_node');
536 * Finishes the title-class node
540 private function finishTitleClass () {
541 // Pop the last entry
542 $this->getStackerInstance()->popNamed('current_node');
546 * Finishes the title-class node
550 private function finishTitleText () {
551 // Pop the last entry
552 $this->getStackerInstance()->popNamed('current_node');
556 * Finishes the footer-text node
560 private function finishFooterText () {
561 // Pop the last entry
562 $this->getStackerInstance()->popNamed('current_node');
566 * Finishes the footer-class node
570 private function finishFooterClass () {
571 // Pop the last entry
572 $this->getStackerInstance()->popNamed('current_node');
576 * Finishes the footer-id node
580 private function finishFooterId () {
581 // Pop the last entry
582 $this->getStackerInstance()->popNamed('current_node');
586 * Finishes the anchor-href node
590 private function finishAnchorHref () {
591 // Pop the last entry
592 $this->getStackerInstance()->popNamed('current_node');
596 * Finishes the anchor-title node
600 private function finishAnchorTitle () {
601 // Pop the last entry
602 $this->getStackerInstance()->popNamed('current_node');
606 * Finishes the anchor-text node
610 private function finishAnchorText () {
611 // Pop the last entry
612 $this->getStackerInstance()->popNamed('current_node');
616 * Finishes the anchor-id node
620 private function finishAnchorId () {
621 // Pop the last entry
622 $this->getStackerInstance()->popNamed('current_node');
626 * Finishes the anchor node
630 private function finishAnchor () {
631 // Pop the last entry
632 $this->getStackerInstance()->popNamed('current_node');
636 * Finishes the entry-id node
640 private function finishEntryId () {
641 // Pop the last entry
642 $this->getStackerInstance()->popNamed('current_node');
646 * Finishes the entry node
650 private function finishEntry () {
651 // Pop the last entry
652 $this->getStackerInstance()->popNamed('current_node');
656 * Finishes the block node
660 private function finishBlock () {
661 // Pop the last entry
662 $this->getStackerInstance()->popNamed('current_node');
666 * Finishes the block-list node
670 private function finishBlockList () {
671 // Pop the last entry
672 $this->getStackerInstance()->popNamed('current_node');
676 * Finishes the menu entries
680 private function finishEntryList () {
681 // Pop the last entry
682 $this->getStackerInstance()->popNamed('current_node');
686 * Finishes the menu block header
690 private function finishBlockHeader () {
691 // Pop the last entry
692 $this->getStackerInstance()->popNamed('current_node');
696 * Finishes the menu block footer
700 private function finishBlockFooter () {
701 // Pop the last entry
702 $this->getStackerInstance()->popNamed('current_node');
710 private function finishMenu () {
711 // Pop the last entry
712 $this->getStackerInstance()->popNamed('current_node');
716 * Getter for menu cache file (FQFN)
718 * @return $fqfn Full-qualified file name of the menu cache
720 public function getMenuCacheFqfn () {
721 // Get the FQFN ready
722 $fqfn = sprintf("%s%s%s/%s.%s",
723 $this->getConfigInstance()->getConfigEntry('base_path'),
724 $this->getGenericBasePath(),
727 $this->getMenuInstance()->getMenuName() . ':' .
728 $this->__toString() . ':' .
729 $this->getMenuInstance()->__toString()
731 $this->getMenuInstance()->getMenuType()