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(
35 * Sub nodes in the XML tree
37 private $subNodes = array(
50 private $menuInstance = null;
55 private $curr = array();
58 * Content from depency
60 private $depencyContent = array();
63 * Protected constructor
67 protected function __construct () {
68 // Call parent constructor
69 parent::__construct(__CLASS__);
73 * Creates an instance of the class TemplateEngine and prepares it for usage
75 * @param $appInstance A manageable application
76 * @param $menuInstance A RenderableMenu instance
77 * @return $tplInstance An instance of TemplateEngine
78 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
79 * @throws InvalidBasePathStringException If $templateBasePath is no string
80 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
81 * directory or not found
82 * @throws BasePathReadProtectedException If $templateBasePath is
85 public final static function createMenuTemplateEngine (ManageableApplication $appInstance, RenderableMenu $menuInstance) {
87 $tplInstance = new MenuTemplateEngine();
89 // Get language and file I/O instances from application
90 $langInstance = $appInstance->getLanguageInstance();
91 $ioInstance = $appInstance->getFileIoInstance();
93 // Determine base path
94 $templateBasePath = $tplInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
96 // Is the base path valid?
97 if (empty($templateBasePath)) {
99 throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
100 } elseif (!is_string($templateBasePath)) {
102 throw new InvalidBasePathStringException(array($tplInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
103 } elseif (!is_dir($templateBasePath)) {
105 throw new BasePathIsNoDirectoryException(array($tplInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
106 } elseif (!is_readable($templateBasePath)) {
108 throw new BasePathReadProtectedException(array($tplInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
111 // Get configuration instance
112 $configInstance = FrameworkConfiguration::getInstance();
115 $tplInstance->setTemplateBasePath($templateBasePath);
117 // Set the language and IO instances
118 $tplInstance->setLanguageInstance($langInstance);
119 $tplInstance->setFileIoInstance($ioInstance);
121 // Set template extensions
122 $tplInstance->setRawTemplateExtension($configInstance->getConfigEntry('raw_template_extension'));
123 $tplInstance->setCodeTemplateExtension($configInstance->getConfigEntry('menu_template_extension'));
125 // Absolute output path for compiled templates
126 $tplInstance->setCompileOutputPath($configInstance->getConfigEntry('base_path') . $configInstance->getConfigEntry('compile_output_path'));
128 // Set the menu instance
129 $tplInstance->setMenuInstance($menuInstance);
131 // Return the prepared instance
136 * Load a specified menu template into the engine
138 * @param $template The menu template we shall load which is
139 * located in 'menu' by default
142 public function loadMenuTemplate ($template) {
144 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
146 // Load the special template
147 $this->loadTemplate($template);
151 * Getter for current main node
153 * @return $currMainNode Current main node
155 public final function getCurrMainNode () {
156 return $this->curr['main_node'];
160 * Setter for current main node
162 * @param $element Element name to set as current main node
163 * @return $currMainNode Current main node
165 private final function setCurrMainNode ($element) {
166 $this->curr['main_node'] = (string) $element;
170 * Getter for main node array
172 * @return $mainNodes Array with valid main node names
174 public final function getMainNodes () {
175 return $this->mainNodes;
179 * Getter for sub node array
181 * @return $subNodes Array with valid sub node names
183 public final function getSubNodes () {
184 return $this->subNodes;
188 * Handles the start element of an XML resource
190 * @param $resource XML parser resource (currently ignored)
191 * @param $element The element we shall handle
192 * @param $attributes All attributes
194 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
196 public function startElement ($resource, $element, array $attributes) {
197 // Initial method name which will never be called...
198 $methodName = 'initMenu';
200 // Make the element name lower-case
201 $element = strtolower($element);
203 // Is the element a main node?
204 //* DEBUG: */ echo "START: >".$element."<<br />\n";
205 if (in_array($element, $this->getMainNodes())) {
206 // Okay, main node found!
207 $methodName = 'startMenu' . $this->convertToClassName($element);
210 $this->setCurrMainNode($element);
211 } elseif (in_array($element, $this->getSubNodes())) {
213 $methodName = 'startMenu' . $this->convertToClassName($element);
214 } elseif ($element != 'menu') {
215 // Invalid node name found
216 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
220 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
221 call_user_func_array(array($this, $methodName), $attributes);
225 * Ends the main or sub node by sending out the gathered data
227 * @param $resource An XML resource pointer (currently ignored)
228 * @param $nodeName Name of the node we want to finish
230 * @throws XmlNodeMismatchException If current main node mismatches the closing one
232 public function endElement ($resource, $nodeName) {
233 // Make all lower-case
234 $nodeName = strtolower($nodeName);
236 // Does this match with current main node?
237 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
238 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
240 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
243 // Construct method name
244 $methodName = 'finish' . $this->convertToClassName($nodeName);
246 // Call the corresponding method
247 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
248 call_user_func_array(array($this, $methodName), array());
254 * @param $resource XML parser resource (currently ignored)
255 * @param $characters Characters to handle
257 * @todo Find something useful with this!
259 public function characterHandler ($resource, $characters) {
260 // Trim all spaces away
261 $characters = trim($characters);
263 // Is this string empty?
264 if (empty($characters)) {
265 // Then skip it silently
270 $this->partialStub('Handling extra characters is not yet supported! length='.strlen($characters));
274 * Handles the template depency for given node
276 * @param $node The node we should load a depency template
277 * @param $templateDepency A template to load to satisfy depencies
280 private function handleTemplateDepency ($node, $templateDepency) {
281 // Is the template depency set?
282 if ((!empty($templateDepency)) && (!isset($this->depencyContent[$node]))) {
283 // Get a temporay menu template instance
284 $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getApplicationInstance(), $this->getMenuInstance()));
287 $templateInstance->loadMenuTemplate($templateDepency);
289 // Get an XmlParser instance
290 $templateInstance->renderXmlContent();
292 // Parse the template's content contents
293 $this->depencyContent[$node] = $templateInstance->getRawTemplateData();
298 * Intializes the menu
300 * @param $templateDepency A template to load to satisfy depencies
302 * @todo Add cache creation here
304 private function initMenu ($templateDepency = '') {
305 // Get web template engine
306 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('web_template_class', array($this->getApplicationInstance())));
308 // Handle the depency template
309 $this->handleTemplateDepency('menu', $templateDepency);
311 // Load the header template for this page
312 $this->getTemplateInstance()->loadCodeTemplate('menu_global_start');
314 // Set the variable group to page
315 $this->setVariableGroup('menu');
317 // Set its content in this template instance
318 $this->assignVariable('menu_start', $this->getTemplateInstance()->getRawTemplateData());
326 private function finishMenu () {
327 // Load the header template for this page
328 $this->getTemplateInstance()->loadCodeTemplate('menu_global_end');
330 // Set the variable group to page
331 $this->setVariableGroup('menu');
333 // Set its content in this template instance
334 $this->assignVariable('menu_end', $this->getTemplateInstance()->getRawTemplateData());
338 * Starts the menu entries by loading a (maybe) provided template depency
340 * @param $templateDepency A template to load to satisfy depencies
343 private function startMenuEntries ($templateDepency = '') {
344 // Handle the depency template
345 $this->handleTemplateDepency('entries', $templateDepency);
347 // Load the header template for this page
348 $this->getTemplateInstance()->loadCodeTemplate('menu_entries_start');
350 // Set the variable group to page
351 $this->setVariableGroup('menu');
353 // Set its content in this template instance
354 $this->assignVariable('entries_start', $this->getTemplateInstance()->getRawTemplateData());
358 * Finishes the menu entries
362 private function finishEntries () {
363 // Load the header template for this page
364 $this->getTemplateInstance()->loadCodeTemplate('menu_entries_end');
366 // Set the variable group to page
367 $this->setVariableGroup('menu');
369 // Set its content in this template instance
370 $this->assignVariable('entries_end', $this->getTemplateInstance()->getRawTemplateData());
374 * Starts the menu header
378 private function startMenuHeader () {
379 // Do we have a template instance?
380 if (is_null($this->getTemplateInstance())) {
381 // Init template instance for underlaying web templates
382 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
384 // Set it in this template engine
385 $this->setTemplateInstance($templateInstance);
388 // Load the header template for this page
389 $this->getTemplateInstance()->loadCodeTemplate('menu_header_start');
391 // Set the variable group to page
392 $this->setVariableGroup('menu');
394 // Set its content in this template instance
395 $this->assignVariable('header', $this->getTemplateInstance()->getRawTemplateData());
399 * Finishes the menu header
403 private function finishHeader () {
404 // Load the header template for this page
405 $this->getTemplateInstance()->loadCodeTemplate('menu_header_end');
407 // Set the variable group to page
408 $this->setVariableGroup('menu');
410 // Set its content in this template instance
411 $this->assignVariable('header_end', $this->getTemplateInstance()->getRawTemplateData());
415 * Starts the menu footer
419 private function startMenuFooter () {
420 // Do we have a template instance?
421 if (is_null($this->getTemplateInstance())) {
422 // Init template instance for underlaying web templates
423 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
425 // Set it in this template engine
426 $this->setTemplateInstance($templateInstance);
429 // Load the footer template for this page
430 $this->getTemplateInstance()->loadCodeTemplate('menu_footer_start');
432 // Set the variable group to page
433 $this->setVariableGroup('menu');
435 // Set its content in this template instance
436 $this->assignVariable('footer', $this->getTemplateInstance()->getRawTemplateData());
440 * Finishes the menu footer
444 private function finishFooter () {
445 // Load the footer template for this page
446 $this->getTemplateInstance()->loadCodeTemplate('menu_footer_end');
448 // Set the variable group to page
449 $this->setVariableGroup('menu');
451 // Set its content in this template instance
452 $this->assignVariable('footer_end', $this->getTemplateInstance()->getRawTemplateData());
456 * Starts the menu property 'title'
458 * @param $id Id of the menu
459 * @param $class The title to add to the menu
462 private function startMenuTitle ($id, $class) {
464 $this->curr['id'] = $id;
466 // Load the title template for this page
467 $this->getTemplateInstance()->loadCodeTemplate('menu_title_' . $id . '_start');
469 // Set the variable group to page
470 $this->setVariableGroup('menu');
472 // Set its content in this template instance
473 $this->assignVariable('title_start_' . $this->curr['id'], $this->getTemplateInstance()->getRawTemplateData());
477 * Finishes the title node by added another template to the menu
481 private function finishTitle () {
482 // Load the title template for this page
483 $this->getTemplateInstance()->loadCodeTemplate('menu_title_' . $this->curr['id'] . '_end');
485 // Set the variable group to page
486 $this->setVariableGroup('menu');
488 // Set its content in this template instance
489 $this->assignVariable('title_end_' . $this->curr['id'], $this->getTemplateInstance()->getRawTemplateData());
493 * Starts the menu text
497 private function startMenuText () {
498 // Do we have a template instance?
499 if (is_null($this->getTemplateInstance())) {
500 // Init template instance for underlaying web templates
501 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
503 // Set it in this template engine
504 $this->setTemplateInstance($templateInstance);
507 // Load the text template for this page
508 $this->getTemplateInstance()->loadCodeTemplate('menu_text_start');
510 // Set the variable group to page
511 $this->setVariableGroup('menu');
513 // Set its content in this template instance
514 $this->assignVariable('text', $this->getTemplateInstance()->getRawTemplateData());
518 * Finishes the menu text
522 private function finishText () {
523 // Load the text template for this page
524 $this->getTemplateInstance()->loadCodeTemplate('menu_text_end');
526 // Set the variable group to page
527 $this->setVariableGroup('menu');
529 // Set its content in this template instance
530 $this->assignVariable('text_end', $this->getTemplateInstance()->getRawTemplateData());
534 * Starts the menu property 'entry'
536 * @param $id Id of the menu
539 private function startMenuEntry ($id) {
541 $this->curr['entry_id'] = $id;
543 // Load the entry template for this page
544 $this->getTemplateInstance()->loadCodeTemplate('menu_entry_' . $id . '_start');
546 // Set the variable group to page
547 $this->setVariableGroup('menu');
549 // Set its content in this template instance
550 $this->assignVariable('entry_start_' . $this->curr['id'], $this->getTemplateInstance()->getRawTemplateData());
554 * Finishes the entry node by added another template to the menu
558 private function finishEntry () {
559 // Load the entry template for this page
560 $this->getTemplateInstance()->loadCodeTemplate('menu_entry_' . $this->curr['entry_id'] . '_end');
562 // Set the variable group to page
563 $this->setVariableGroup('menu');
565 // Set its content in this template instance
566 $this->assignVariable('entry_end_' . $this->curr['id'], $this->getTemplateInstance()->getRawTemplateData());
569 * Starts the menu property 'anchor'
571 * @param $id Id of the anchor
572 * @param $link Link text of the anchor
573 * @param $title Link title of the anchor
576 private function startMenuAnchor ($id, $link, $title) {
577 $this->partialStub('id='.$id.',link='.$link.',title='.$title);
581 * Finishes the anchor node by added another template to the menu
585 private function finishAnchor () {
586 $this->partialStub('Please implement this method.');
590 * Getter for menu cache file (FQFN)
592 * @return $fqfn Full-qualified file name of the menu cache
594 public function getMenuCacheFqfn () {
595 // Get the FQFN ready
596 $fqfn = sprintf("%s%s%s/%s.%s",
597 $this->getConfigInstance()->getConfigEntry('base_path'),
598 $this->getGenericBasePath(),
601 $this->getMenuInstance()->getMenuName() . ':' .
602 $this->__toString() . ':' .
603 $this->getMenuInstance()->__toString()
605 $this->getMenuInstance()->getMenuType()