3 * A Menu template engine class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 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 // Get language and file I/O instances from application
103 $langInstance = $appInstance->getLanguageInstance();
104 $ioInstance = $appInstance->getFileIoInstance();
106 // Determine base path
107 $templateBasePath = $templateInstance->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($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
113 } elseif (!is_string($templateBasePath)) {
115 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
116 } elseif (!is_dir($templateBasePath)) {
118 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
119 } elseif (!is_readable($templateBasePath)) {
121 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
125 $templateInstance->setTemplateBasePath($templateBasePath);
127 // Set the language and IO instances
128 $templateInstance->setLanguageInstance($langInstance);
129 $templateInstance->setFileIoInstance($ioInstance);
131 // Set template extensions
132 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
133 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('menu_template_extension'));
135 // Absolute output path for compiled templates
136 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
138 // Set the menu instance
139 $templateInstance->setMenuInstance($menuInstance);
141 // Init a variable stacker
142 $stackerInstance = ObjectFactory::createObjectByConfiguredName('menu_stacker_class');
145 $templateInstance->setStackerInstance($stackerInstance);
147 // Return the prepared instance
148 return $templateInstance;
152 * Load a specified menu template into the engine
154 * @param $template The menu template we shall load which is
155 * located in 'menu' by default
158 public function loadMenuTemplate ($template) {
160 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
162 // Load the special template
163 $this->loadTemplate($template);
167 * Getter for current main node
169 * @return $currMainNode Current main node
171 public final function getCurrMainNode () {
172 return $this->curr['main_node'];
176 * Setter for current main node
178 * @param $element Element name to set as current main node
179 * @return $currMainNode Current main node
181 private final function setCurrMainNode ($element) {
182 $this->curr['main_node'] = (string) $element;
186 * Getter for main node array
188 * @return $mainNodes Array with valid main node names
190 public final function getMainNodes () {
191 return $this->mainNodes;
195 * Getter for sub node array
197 * @return $subNodes Array with valid sub node names
199 public final function getSubNodes () {
200 return $this->subNodes;
204 * Handles the start element of an XML resource
206 * @param $resource XML parser resource (currently ignored)
207 * @param $element The element we shall handle
208 * @param $attributes All attributes
210 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
212 public function startElement ($resource, $element, array $attributes) {
213 // Initial method name which will never be called...
214 $methodName = 'initMenu';
216 // Make the element name lower-case
217 $element = strtolower($element);
219 // Is the element a main node?
220 //* DEBUG: */ echo "START: >".$element."<<br />\n";
221 if (in_array($element, $this->getMainNodes())) {
222 // Okay, main node found!
223 $methodName = 'start' . $this->convertToClassName($element);
226 $this->setCurrMainNode($element);
227 } elseif (in_array($element, $this->getSubNodes())) {
229 $methodName = 'start' . $this->convertToClassName($element);
230 } elseif ($element != 'menu') {
231 // Invalid node name found
232 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
236 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
237 call_user_func_array(array($this, $methodName), $attributes);
241 * Ends the main or sub node by sending out the gathered data
243 * @param $resource An XML resource pointer (currently ignored)
244 * @param $nodeName Name of the node we want to finish
246 * @throws XmlNodeMismatchException If current main node mismatches the closing one
248 public function endElement ($resource, $nodeName) {
249 // Make all lower-case
250 $nodeName = strtolower($nodeName);
252 // Does this match with current main node?
253 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
254 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
256 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
259 // Construct method name
260 $methodName = 'finish' . $this->convertToClassName($nodeName);
262 // Call the corresponding method
263 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
264 call_user_func_array(array($this, $methodName), array());
270 * @param $resource XML parser resource (currently ignored)
271 * @param $characters Characters to handle
273 * @todo Find something useful with this!
275 public function characterHandler ($resource, $characters) {
276 // Trim all spaces away
277 $characters = trim($characters);
279 // Is this string empty?
280 if (empty($characters)) {
281 // Then skip it silently
285 // Assign the found characters to variable and use the last entry from
287 parent::assignVariable($this->getStackerInstance()->getNamed('current_node'), $characters);
291 * Handles the template dependency for given node
293 * @param $node The node we should load a dependency template
294 * @param $templateDependency A template to load to satisfy dependencies
297 private function handleTemplateDependency ($node, $templateDependency) {
298 // Is the template dependency set?
299 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
300 // Get a temporay menu template instance
301 $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getApplicationInstance(), $this->getMenuInstance()));
304 $templateInstance->loadMenuTemplate($templateDependency);
306 // Get an XmlParser instance
307 $templateInstance->renderXmlContent();
309 // Parse the template's content contents
310 $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
315 * Intializes the menu
317 * @param $templateDependency A template to load to satisfy dependencies
319 * @todo Add cache creation here
321 private function initMenu ($templateDependency = '') {
322 // Get web template engine
323 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('web_template_class', array($this->getApplicationInstance())));
325 // Handle the dependency template
326 $this->handleTemplateDependency('menu', $templateDependency);
328 // Push the node name on the stacker
329 $this->getStackerInstance()->pushNamed('current_node', 'menu');
333 * Starts the menu entries
335 * @param $templateDependency A template to load to satisfy dependencies
338 private function startEntryList () {
339 // Push the node name on the stacker
340 $this->getStackerInstance()->pushNamed('current_node', 'entry-list');
344 * Starts the menu block header
348 private function startBlockHeader () {
349 // Push the node name on the stacker
350 $this->getStackerInstance()->pushNamed('current_node', 'block-header');
354 * Starts the menu block footer
358 private function startBlockFooter () {
359 // Push the node name on the stacker
360 $this->getStackerInstance()->pushNamed('current_node', 'block-footer');
364 * Starts the menu property 'block-list'
368 private function startBlockList () {
369 // Push the node name on the stacker
370 $this->getStackerInstance()->pushNamed('current_node', 'block-list');
374 * Starts the menu property 'block'
378 private function startBlock () {
379 // Push the node name on the stacker
380 $this->getStackerInstance()->pushNamed('current_node', 'block');
384 * Starts the menu property 'title'
388 private function startTitle () {
389 // Push the node name on the stacker
390 $this->getStackerInstance()->pushNamed('current_node', 'title');
394 * Starts the menu property 'title-id'
398 private function startTitleId () {
399 // Push the node name on the stacker
400 $this->getStackerInstance()->pushNamed('current_node', 'title-id');
404 * Starts the menu property 'title-class'
408 private function startTitleClass () {
409 // Push the node name on the stacker
410 $this->getStackerInstance()->pushNamed('current_node', 'title-class');
414 * Starts the menu property 'title-text'
418 private function startTitleText () {
419 // Push the node name on the stacker
420 $this->getStackerInstance()->pushNamed('current_node', 'title-text');
424 * Starts the menu property 'entry'
428 private function startEntry () {
429 // Push the node name on the stacker
430 $this->getStackerInstance()->pushNamed('current_node', 'entry');
434 * Starts the menu property 'entry-id'
438 private function startEntryId () {
439 // Push the node name on the stacker
440 $this->getStackerInstance()->pushNamed('current_node', 'entry-id');
444 * Starts the menu property 'anchor'
448 private function startAnchor () {
449 // Push the node name on the stacker
450 $this->getStackerInstance()->pushNamed('current_node', 'anchor');
454 * Starts the menu property 'anchor-id'
458 private function startAnchorId () {
459 // Push the node name on the stacker
460 $this->getStackerInstance()->pushNamed('current_node', 'anchor-id');
464 * Starts the menu property 'anchor-text'
468 private function startAnchorText () {
469 // Push the node name on the stacker
470 $this->getStackerInstance()->pushNamed('current_node', 'anchor-text');
474 * Starts the menu property 'anchor-title'
478 private function startAnchorTitle () {
479 // Push the node name on the stacker
480 $this->getStackerInstance()->pushNamed('current_node', 'anchor-title');
484 * Starts the menu property 'anchor-href'
488 private function startAnchorHref () {
489 // Push the node name on the stacker
490 $this->getStackerInstance()->pushNamed('current_node', 'anchor-href');
494 * Starts the menu property 'footer-id'
498 private function startFooterId () {
499 // Push the node name on the stacker
500 $this->getStackerInstance()->pushNamed('current_node', 'footer-id');
504 * Starts the menu property 'footer-class'
508 private function startFooterClass () {
509 // Push the node name on the stacker
510 $this->getStackerInstance()->pushNamed('current_node', 'footer-class');
514 * Starts the menu property 'footer-text'
518 private function startFooterText () {
519 // Push the node name on the stacker
520 $this->getStackerInstance()->pushNamed('current_node', 'footer-text');
524 * Finishes the title node by added another template to the menu
528 private function finishTitle () {
529 // Pop the last entry
530 $this->getStackerInstance()->popNamed('current_node');
534 * Finishes the title-id node by
538 private function finishTitleId () {
539 // Pop the last entry
540 $this->getStackerInstance()->popNamed('current_node');
544 * Finishes the title-class node
548 private function finishTitleClass () {
549 // Pop the last entry
550 $this->getStackerInstance()->popNamed('current_node');
554 * Finishes the title-class node
558 private function finishTitleText () {
559 // Pop the last entry
560 $this->getStackerInstance()->popNamed('current_node');
564 * Finishes the footer-text node
568 private function finishFooterText () {
569 // Pop the last entry
570 $this->getStackerInstance()->popNamed('current_node');
574 * Finishes the footer-class node
578 private function finishFooterClass () {
579 // Pop the last entry
580 $this->getStackerInstance()->popNamed('current_node');
584 * Finishes the footer-id node
588 private function finishFooterId () {
589 // Pop the last entry
590 $this->getStackerInstance()->popNamed('current_node');
594 * Finishes the anchor-href node
598 private function finishAnchorHref () {
599 // Pop the last entry
600 $this->getStackerInstance()->popNamed('current_node');
604 * Finishes the anchor-title node
608 private function finishAnchorTitle () {
609 // Pop the last entry
610 $this->getStackerInstance()->popNamed('current_node');
614 * Finishes the anchor-text node
618 private function finishAnchorText () {
619 // Pop the last entry
620 $this->getStackerInstance()->popNamed('current_node');
624 * Finishes the anchor-id node
628 private function finishAnchorId () {
629 // Pop the last entry
630 $this->getStackerInstance()->popNamed('current_node');
634 * Finishes the anchor node
638 private function finishAnchor () {
639 // Pop the last entry
640 $this->getStackerInstance()->popNamed('current_node');
644 * Finishes the entry-id node
648 private function finishEntryId () {
649 // Pop the last entry
650 $this->getStackerInstance()->popNamed('current_node');
654 * Finishes the entry node
658 private function finishEntry () {
659 // Pop the last entry
660 $this->getStackerInstance()->popNamed('current_node');
664 * Finishes the block node
668 private function finishBlock () {
669 // Pop the last entry
670 $this->getStackerInstance()->popNamed('current_node');
674 * Finishes the block-list node
678 private function finishBlockList () {
679 // Pop the last entry
680 $this->getStackerInstance()->popNamed('current_node');
684 * Finishes the menu entries
688 private function finishEntryList () {
689 // Pop the last entry
690 $this->getStackerInstance()->popNamed('current_node');
694 * Finishes the menu block header
698 private function finishBlockHeader () {
699 // Pop the last entry
700 $this->getStackerInstance()->popNamed('current_node');
704 * Finishes the menu block footer
708 private function finishBlockFooter () {
709 // Pop the last entry
710 $this->getStackerInstance()->popNamed('current_node');
718 private function finishMenu () {
719 // Pop the last entry
720 $this->getStackerInstance()->popNamed('current_node');
724 * Getter for menu cache file (FQFN)
726 * @return $fqfn Full-qualified file name of the menu cache
728 public function getMenuCacheFqfn () {
729 // Get the FQFN ready
730 $fqfn = sprintf("%s%s%s/%s.%s",
731 $this->getConfigInstance()->getConfigEntry('base_path'),
732 $this->getGenericBasePath(),
735 $this->getMenuInstance()->getMenuName() . ':' .
736 $this->__toString() . ':' .
737 $this->getMenuInstance()->__toString()
739 $this->getMenuInstance()->getMenuType()