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(
56 private $menuInstance = null;
61 private $curr = array();
64 * Content from depency
66 private $depencyContent = array();
69 * Protected constructor
73 protected function __construct () {
74 // Call parent constructor
75 parent::__construct(__CLASS__);
79 * Creates an instance of the class TemplateEngine and prepares it for usage
81 * @param $appInstance A manageable application
82 * @param $menuInstance A RenderableMenu instance
83 * @return $tplInstance An instance of TemplateEngine
84 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
85 * @throws InvalidBasePathStringException If $templateBasePath is no string
86 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
87 * directory or not found
88 * @throws BasePathReadProtectedException If $templateBasePath is
91 public final static function createMenuTemplateEngine (ManageableApplication $appInstance, RenderableMenu $menuInstance) {
93 $tplInstance = new MenuTemplateEngine();
95 // Get language and file I/O instances from application
96 $langInstance = $appInstance->getLanguageInstance();
97 $ioInstance = $appInstance->getFileIoInstance();
99 // Determine base path
100 $templateBasePath = $tplInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
102 // Is the base path valid?
103 if (empty($templateBasePath)) {
104 // Base path is empty
105 throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
106 } elseif (!is_string($templateBasePath)) {
108 throw new InvalidBasePathStringException(array($tplInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
109 } elseif (!is_dir($templateBasePath)) {
111 throw new BasePathIsNoDirectoryException(array($tplInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
112 } elseif (!is_readable($templateBasePath)) {
114 throw new BasePathReadProtectedException(array($tplInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
117 // Get configuration instance
118 $configInstance = FrameworkConfiguration::getInstance();
121 $tplInstance->setTemplateBasePath($templateBasePath);
123 // Set the language and IO instances
124 $tplInstance->setLanguageInstance($langInstance);
125 $tplInstance->setFileIoInstance($ioInstance);
127 // Set template extensions
128 $tplInstance->setRawTemplateExtension($configInstance->getConfigEntry('raw_template_extension'));
129 $tplInstance->setCodeTemplateExtension($configInstance->getConfigEntry('menu_template_extension'));
131 // Absolute output path for compiled templates
132 $tplInstance->setCompileOutputPath($configInstance->getConfigEntry('base_path') . $configInstance->getConfigEntry('compile_output_path'));
134 // Set the menu instance
135 $tplInstance->setMenuInstance($menuInstance);
137 // Return the prepared instance
142 * Load a specified menu template into the engine
144 * @param $template The menu template we shall load which is
145 * located in 'menu' by default
148 public function loadMenuTemplate ($template) {
150 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
152 // Load the special template
153 $this->loadTemplate($template);
157 * Getter for current main node
159 * @return $currMainNode Current main node
161 public final function getCurrMainNode () {
162 return $this->curr['main_node'];
166 * Setter for current main node
168 * @param $element Element name to set as current main node
169 * @return $currMainNode Current main node
171 private final function setCurrMainNode ($element) {
172 $this->curr['main_node'] = (string) $element;
176 * Getter for main node array
178 * @return $mainNodes Array with valid main node names
180 public final function getMainNodes () {
181 return $this->mainNodes;
185 * Getter for sub node array
187 * @return $subNodes Array with valid sub node names
189 public final function getSubNodes () {
190 return $this->subNodes;
194 * Handles the start element of an XML resource
196 * @param $resource XML parser resource (currently ignored)
197 * @param $element The element we shall handle
198 * @param $attributes All attributes
200 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
202 public function startElement ($resource, $element, array $attributes) {
203 // Initial method name which will never be called...
204 $methodName = 'initMenu';
206 // Make the element name lower-case
207 $element = strtolower($element);
209 // Is the element a main node?
210 //* DEBUG: */ echo "START: >".$element."<<br />\n";
211 if (in_array($element, $this->getMainNodes())) {
212 // Okay, main node found!
213 $methodName = 'start' . $this->convertToClassName($element);
216 $this->setCurrMainNode($element);
217 } elseif (in_array($element, $this->getSubNodes())) {
219 $methodName = 'start' . $this->convertToClassName($element);
220 } elseif ($element != 'menu') {
221 // Invalid node name found
222 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
226 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
227 call_user_func_array(array($this, $methodName), $attributes);
231 * Ends the main or sub node by sending out the gathered data
233 * @param $resource An XML resource pointer (currently ignored)
234 * @param $nodeName Name of the node we want to finish
236 * @throws XmlNodeMismatchException If current main node mismatches the closing one
238 public function endElement ($resource, $nodeName) {
239 // Make all lower-case
240 $nodeName = strtolower($nodeName);
242 // Does this match with current main node?
243 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
244 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
246 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
249 // Construct method name
250 $methodName = 'finish' . $this->convertToClassName($nodeName);
252 // Call the corresponding method
253 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
254 call_user_func_array(array($this, $methodName), array());
260 * @param $resource XML parser resource (currently ignored)
261 * @param $characters Characters to handle
263 * @todo Find something useful with this!
265 public function characterHandler ($resource, $characters) {
266 // Trim all spaces away
267 $characters = trim($characters);
269 // Is this string empty?
270 if (empty($characters)) {
271 // Then skip it silently
276 $this->partialStub('Handling extra characters is not yet supported! length='.strlen($characters));
280 * Handles the template depency for given node
282 * @param $node The node we should load a depency template
283 * @param $templateDepency A template to load to satisfy depencies
286 private function handleTemplateDepency ($node, $templateDepency) {
287 // Is the template depency set?
288 if ((!empty($templateDepency)) && (!isset($this->depencyContent[$node]))) {
289 // Get a temporay menu template instance
290 $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getApplicationInstance(), $this->getMenuInstance()));
293 $templateInstance->loadMenuTemplate($templateDepency);
295 // Get an XmlParser instance
296 $templateInstance->renderXmlContent();
298 // Parse the template's content contents
299 $this->depencyContent[$node] = $templateInstance->getRawTemplateData();
304 * Intializes the menu
306 * @param $templateDepency A template to load to satisfy depencies
308 * @todo Add cache creation here
310 private function initMenu ($templateDepency = '') {
311 // Get web template engine
312 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('web_template_class', array($this->getApplicationInstance())));
314 // Handle the depency template
315 $this->handleTemplateDepency('menu', $templateDepency);
317 // Load the header template for this page
318 $this->getTemplateInstance()->loadCodeTemplate('menu_global_start');
320 // Set the variable group to page
321 $this->setVariableGroup('menu');
323 // Set its content in this template instance
324 $this->assignVariable('menu_start', $this->getTemplateInstance()->getRawTemplateData());
332 private function finishMenu () {
333 // Load the header template for this page
334 $this->getTemplateInstance()->loadCodeTemplate('menu_global_end');
336 // Set the variable group to page
337 $this->setVariableGroup('menu');
339 // Set its content in this template instance
340 $this->assignVariable('menu_end', $this->getTemplateInstance()->getRawTemplateData());
344 * Starts the menu entries by loading a (maybe) provided template depency
346 * @param $templateDepency A template to load to satisfy depencies
349 private function startEntryList ($templateDepency = '') {
350 // Handle the depency template
351 $this->handleTemplateDepency('entries', $templateDepency);
353 // Load the header template for this page
354 $this->getTemplateInstance()->loadCodeTemplate('menu_entries_start');
356 // Set the variable group to page
357 $this->setVariableGroup('menu');
359 // Set its content in this template instance
360 $this->assignVariable('entries_start', $this->getTemplateInstance()->getRawTemplateData());
364 * Finishes the menu entries
368 private function finishEntryList () {
369 // Load the header template for this page
370 $this->getTemplateInstance()->loadCodeTemplate('menu_entries_end');
372 // Set the variable group to page
373 $this->setVariableGroup('menu');
375 // Set its content in this template instance
376 $this->assignVariable('entries_end', $this->getTemplateInstance()->getRawTemplateData());
380 * Starts the menu header
384 private function startHeader () {
385 // Do we have a template instance?
386 if (is_null($this->getTemplateInstance())) {
387 // Init template instance for underlaying web templates
388 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
390 // Set it in this template engine
391 $this->setTemplateInstance($templateInstance);
394 // Load the header template for this page
395 $this->getTemplateInstance()->loadCodeTemplate('menu_header_start');
397 // Set the variable group to page
398 $this->setVariableGroup('menu');
400 // Set its content in this template instance
401 $this->assignVariable('header', $this->getTemplateInstance()->getRawTemplateData());
405 * Finishes the menu header
409 private function finishHeader () {
410 // Load the header template for this page
411 $this->getTemplateInstance()->loadCodeTemplate('menu_header_end');
413 // Set the variable group to page
414 $this->setVariableGroup('menu');
416 // Set its content in this template instance
417 $this->assignVariable('header_end', $this->getTemplateInstance()->getRawTemplateData());
421 * Starts the menu footer
425 private function startFooter () {
426 // Do we have a template instance?
427 if (is_null($this->getTemplateInstance())) {
428 // Init template instance for underlaying web templates
429 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
431 // Set it in this template engine
432 $this->setTemplateInstance($templateInstance);
435 // Load the footer template for this page
436 $this->getTemplateInstance()->loadCodeTemplate('menu_footer_start');
438 // Set the variable group to page
439 $this->setVariableGroup('menu');
441 // Set its content in this template instance
442 $this->assignVariable('footer', $this->getTemplateInstance()->getRawTemplateData());
446 * Finishes the menu footer
450 private function finishFooter () {
451 // Load the footer template for this page
452 $this->getTemplateInstance()->loadCodeTemplate('menu_footer_end');
454 // Set the variable group to page
455 $this->setVariableGroup('menu');
457 // Set its content in this template instance
458 $this->assignVariable('footer_end', $this->getTemplateInstance()->getRawTemplateData());
462 * Starts the menu property 'title'
464 * @param $id Id of the menu
465 * @param $class The title to add to the menu
468 private function startTitle ($id, $class) {
470 $this->curr['id'] = $id;
472 // Load the title template for this page
473 $this->getTemplateInstance()->loadCodeTemplate('menu_title_' . $id . '_start');
475 // Set the variable group to page
476 $this->setVariableGroup('menu');
478 // Set its content in this template instance
479 $this->assignVariable('title_start_' . $this->curr['id'], $this->getTemplateInstance()->getRawTemplateData());
483 * Finishes the title node by added another template to the menu
487 private function finishTitle () {
488 // Load the title template for this page
489 $this->getTemplateInstance()->loadCodeTemplate('menu_title_' . $this->curr['id'] . '_end');
491 // Set the variable group to page
492 $this->setVariableGroup('menu');
494 // Set its content in this template instance
495 $this->assignVariable('title_end_' . $this->curr['id'], $this->getTemplateInstance()->getRawTemplateData());
499 * Starts the menu text
503 private function startText () {
504 // Do we have a template instance?
505 if (is_null($this->getTemplateInstance())) {
506 // Init template instance for underlaying web templates
507 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
509 // Set it in this template engine
510 $this->setTemplateInstance($templateInstance);
513 // Load the text template for this page
514 $this->getTemplateInstance()->loadCodeTemplate('menu_text_start');
516 // Set the variable group to page
517 $this->setVariableGroup('menu');
519 // Set its content in this template instance
520 $this->assignVariable('text', $this->getTemplateInstance()->getRawTemplateData());
524 * Finishes the menu text
528 private function finishText () {
529 // Load the text template for this page
530 $this->getTemplateInstance()->loadCodeTemplate('menu_text_end');
532 // Set the variable group to page
533 $this->setVariableGroup('menu');
535 // Set its content in this template instance
536 $this->assignVariable('text_end', $this->getTemplateInstance()->getRawTemplateData());
540 * Starts the menu property 'entry'
542 * @param $id Id of the menu
545 private function startEntry ($id) {
547 $this->curr['entry_id'] = $id;
549 // Load the entry template for this page
550 $this->getTemplateInstance()->loadCodeTemplate('menu_entry_' . $id . '_start');
552 // Set the variable group to page
553 $this->setVariableGroup('menu');
555 // Set its content in this template instance
556 $this->assignVariable('entry_start_' . $this->curr['id'], $this->getTemplateInstance()->getRawTemplateData());
560 * Finishes the entry node by added another template to the menu
564 private function finishEntry () {
565 // Load the entry template for this page
566 $this->getTemplateInstance()->loadCodeTemplate('menu_entry_' . $this->curr['entry_id'] . '_end');
568 // Set the variable group to page
569 $this->setVariableGroup('menu');
571 // Set its content in this template instance
572 $this->assignVariable('entry_end_' . $this->curr['id'], $this->getTemplateInstance()->getRawTemplateData());
575 * Starts the menu property 'anchor'
577 * @param $id Id of the anchor
578 * @param $link Link text of the anchor
579 * @param $title Link title of the anchor
582 private function startAnchor () {
583 $this->partialStub('Please implement this method.');
587 * Finishes the anchor node by added another template to the menu
591 private function finishAnchor () {
592 $this->partialStub('Please implement this method.');
596 * Getter for menu cache file (FQFN)
598 * @return $fqfn Full-qualified file name of the menu cache
600 public function getMenuCacheFqfn () {
601 // Get the FQFN ready
602 $fqfn = sprintf("%s%s%s/%s.%s",
603 $this->getConfigInstance()->getConfigEntry('base_path'),
604 $this->getGenericBasePath(),
607 $this->getMenuInstance()->getMenuName() . ':' .
608 $this->__toString() . ':' .
609 $this->getMenuInstance()->__toString()
611 $this->getMenuInstance()->getMenuType()