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(
62 private $menuInstance = null;
67 private $curr = array();
70 * Content from depency
72 private $depencyContent = array();
75 * Protected constructor
79 protected function __construct () {
80 // Call parent constructor
81 parent::__construct(__CLASS__);
85 * Creates an instance of the class TemplateEngine and prepares it for usage
87 * @param $appInstance A manageable application
88 * @param $menuInstance A RenderableMenu instance
89 * @return $tplInstance An instance of TemplateEngine
90 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
91 * @throws InvalidBasePathStringException If $templateBasePath is no string
92 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
93 * directory or not found
94 * @throws BasePathReadProtectedException If $templateBasePath is
97 public final static function createMenuTemplateEngine (ManageableApplication $appInstance, RenderableMenu $menuInstance) {
99 $tplInstance = new MenuTemplateEngine();
101 // Get language and file I/O instances from application
102 $langInstance = $appInstance->getLanguageInstance();
103 $ioInstance = $appInstance->getFileIoInstance();
105 // Determine base path
106 $templateBasePath = $tplInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
108 // Is the base path valid?
109 if (empty($templateBasePath)) {
110 // Base path is empty
111 throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
112 } elseif (!is_string($templateBasePath)) {
114 throw new InvalidBasePathStringException(array($tplInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
115 } elseif (!is_dir($templateBasePath)) {
117 throw new BasePathIsNoDirectoryException(array($tplInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
118 } elseif (!is_readable($templateBasePath)) {
120 throw new BasePathReadProtectedException(array($tplInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
123 // Get configuration instance
124 $configInstance = FrameworkConfiguration::getInstance();
127 $tplInstance->setTemplateBasePath($templateBasePath);
129 // Set the language and IO instances
130 $tplInstance->setLanguageInstance($langInstance);
131 $tplInstance->setFileIoInstance($ioInstance);
133 // Set template extensions
134 $tplInstance->setRawTemplateExtension($configInstance->getConfigEntry('raw_template_extension'));
135 $tplInstance->setCodeTemplateExtension($configInstance->getConfigEntry('menu_template_extension'));
137 // Absolute output path for compiled templates
138 $tplInstance->setCompileOutputPath($configInstance->getConfigEntry('base_path') . $configInstance->getConfigEntry('compile_output_path'));
140 // Set the menu instance
141 $tplInstance->setMenuInstance($menuInstance);
143 // Return the prepared instance
148 * Load a specified menu template into the engine
150 * @param $template The menu template we shall load which is
151 * located in 'menu' by default
154 public function loadMenuTemplate ($template) {
156 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
158 // Load the special template
159 $this->loadTemplate($template);
163 * Getter for current main node
165 * @return $currMainNode Current main node
167 public final function getCurrMainNode () {
168 return $this->curr['main_node'];
172 * Setter for current main node
174 * @param $element Element name to set as current main node
175 * @return $currMainNode Current main node
177 private final function setCurrMainNode ($element) {
178 $this->curr['main_node'] = (string) $element;
182 * Getter for main node array
184 * @return $mainNodes Array with valid main node names
186 public final function getMainNodes () {
187 return $this->mainNodes;
191 * Getter for sub node array
193 * @return $subNodes Array with valid sub node names
195 public final function getSubNodes () {
196 return $this->subNodes;
200 * Handles the start element of an XML resource
202 * @param $resource XML parser resource (currently ignored)
203 * @param $element The element we shall handle
204 * @param $attributes All attributes
206 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
208 public function startElement ($resource, $element, array $attributes) {
209 // Initial method name which will never be called...
210 $methodName = 'initMenu';
212 // Make the element name lower-case
213 $element = strtolower($element);
215 // Is the element a main node?
216 //* DEBUG: */ echo "START: >".$element."<<br />\n";
217 if (in_array($element, $this->getMainNodes())) {
218 // Okay, main node found!
219 $methodName = 'start' . $this->convertToClassName($element);
222 $this->setCurrMainNode($element);
223 } elseif (in_array($element, $this->getSubNodes())) {
225 $methodName = 'start' . $this->convertToClassName($element);
226 } elseif ($element != 'menu') {
227 // Invalid node name found
228 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
232 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
233 call_user_func_array(array($this, $methodName), $attributes);
237 * Ends the main or sub node by sending out the gathered data
239 * @param $resource An XML resource pointer (currently ignored)
240 * @param $nodeName Name of the node we want to finish
242 * @throws XmlNodeMismatchException If current main node mismatches the closing one
244 public function endElement ($resource, $nodeName) {
245 // Make all lower-case
246 $nodeName = strtolower($nodeName);
248 // Does this match with current main node?
249 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
250 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
252 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
255 // Construct method name
256 $methodName = 'finish' . $this->convertToClassName($nodeName);
258 // Call the corresponding method
259 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
260 call_user_func_array(array($this, $methodName), array());
266 * @param $resource XML parser resource (currently ignored)
267 * @param $characters Characters to handle
269 * @todo Find something useful with this!
271 public function characterHandler ($resource, $characters) {
272 // Trim all spaces away
273 $characters = trim($characters);
275 // Is this string empty?
276 if (empty($characters)) {
277 // Then skip it silently
282 $this->partialStub('Handling extra characters is not yet supported! length='.strlen($characters));
286 * Handles the template depency for given node
288 * @param $node The node we should load a depency template
289 * @param $templateDepency A template to load to satisfy depencies
292 private function handleTemplateDepency ($node, $templateDepency) {
293 // Is the template depency set?
294 if ((!empty($templateDepency)) && (!isset($this->depencyContent[$node]))) {
295 // Get a temporay menu template instance
296 $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getApplicationInstance(), $this->getMenuInstance()));
299 $templateInstance->loadMenuTemplate($templateDepency);
301 // Get an XmlParser instance
302 $templateInstance->renderXmlContent();
304 // Parse the template's content contents
305 $this->depencyContent[$node] = $templateInstance->getRawTemplateData();
310 * Intializes the menu
312 * @param $templateDepency A template to load to satisfy depencies
314 * @todo Add cache creation here
316 private function initMenu ($templateDepency = '') {
317 // Get web template engine
318 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('web_template_class', array($this->getApplicationInstance())));
320 // Handle the depency template
321 $this->handleTemplateDepency('menu', $templateDepency);
323 // Load the header template for this page
324 $this->getTemplateInstance()->loadCodeTemplate('menu_global_start');
326 // Set the variable group to page
327 $this->setVariableGroup('menu');
329 // Set its content in this template instance
330 $this->assignVariable('menu_start', $this->getTemplateInstance()->getRawTemplateData());
338 private function finishMenu () {
339 // Load the header template for this page
340 $this->getTemplateInstance()->loadCodeTemplate('menu_global_end');
342 // Set the variable group to page
343 $this->setVariableGroup('menu');
345 // Set its content in this template instance
346 $this->assignVariable('menu_end', $this->getTemplateInstance()->getRawTemplateData());
350 * Starts the menu entries by loading a (maybe) provided template depency
352 * @param $templateDepency A template to load to satisfy depencies
355 private function startEntryList ($templateDepency = '') {
356 // Handle the depency template
357 $this->handleTemplateDepency('entries', $templateDepency);
359 // Load the header template for this page
360 $this->getTemplateInstance()->loadCodeTemplate('menu_entries_start');
362 // Set the variable group to page
363 $this->setVariableGroup('menu');
365 // Set its content in this template instance
366 $this->assignVariable('entries_start', $this->getTemplateInstance()->getRawTemplateData());
370 * Finishes the menu entries
374 private function finishEntryList () {
375 // Load the header template for this page
376 $this->getTemplateInstance()->loadCodeTemplate('menu_entries_end');
378 // Set the variable group to page
379 $this->setVariableGroup('menu');
381 // Set its content in this template instance
382 $this->assignVariable('entries_end', $this->getTemplateInstance()->getRawTemplateData());
386 * Starts the menu header
390 private function startHeader () {
391 // Do we have a template instance?
392 if (is_null($this->getTemplateInstance())) {
393 // Init template instance for underlaying web templates
394 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
396 // Set it in this template engine
397 $this->setTemplateInstance($templateInstance);
400 // Load the header template for this page
401 $this->getTemplateInstance()->loadCodeTemplate('menu_header_start');
403 // Set the variable group to page
404 $this->setVariableGroup('menu');
406 // Set its content in this template instance
407 $this->assignVariable('header', $this->getTemplateInstance()->getRawTemplateData());
411 * Finishes the menu header
415 private function finishHeader () {
416 // Load the header template for this page
417 $this->getTemplateInstance()->loadCodeTemplate('menu_header_end');
419 // Set the variable group to page
420 $this->setVariableGroup('menu');
422 // Set its content in this template instance
423 $this->assignVariable('header_end', $this->getTemplateInstance()->getRawTemplateData());
427 * Starts the menu footer
431 private function startFooter () {
432 // Do we have a template instance?
433 if (is_null($this->getTemplateInstance())) {
434 // Init template instance for underlaying web templates
435 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
437 // Set it in this template engine
438 $this->setTemplateInstance($templateInstance);
441 // Load the footer template for this page
442 $this->getTemplateInstance()->loadCodeTemplate('menu_footer_start');
444 // Set the variable group to page
445 $this->setVariableGroup('menu');
447 // Set its content in this template instance
448 $this->assignVariable('footer', $this->getTemplateInstance()->getRawTemplateData());
452 * Finishes the menu footer
456 private function finishFooter () {
457 // Load the footer template for this page
458 $this->getTemplateInstance()->loadCodeTemplate('menu_footer_end');
460 // Set the variable group to page
461 $this->setVariableGroup('menu');
463 // Set its content in this template instance
464 $this->assignVariable('footer_end', $this->getTemplateInstance()->getRawTemplateData());
468 * Starts the menu property 'title'
472 private function startTitle () {
473 $this->partialStub('Cleared due to XML rewrite.');
477 * Finishes the title node by added another template to the menu
481 private function finishTitle () {
482 $this->partialStub('Cleared due to XML rewrite.');
486 * Starts the menu text
490 private function startText () {
491 // Do we have a template instance?
492 if (is_null($this->getTemplateInstance())) {
493 // Init template instance for underlaying web templates
494 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
496 // Set it in this template engine
497 $this->setTemplateInstance($templateInstance);
500 // Load the text template for this page
501 $this->getTemplateInstance()->loadCodeTemplate('menu_text_start');
503 // Set the variable group to page
504 $this->setVariableGroup('menu');
506 // Set its content in this template instance
507 $this->assignVariable('text', $this->getTemplateInstance()->getRawTemplateData());
511 * Finishes the menu text
515 private function finishText () {
516 // Load the text template for this page
517 $this->getTemplateInstance()->loadCodeTemplate('menu_text_end');
519 // Set the variable group to page
520 $this->setVariableGroup('menu');
522 // Set its content in this template instance
523 $this->assignVariable('text_end', $this->getTemplateInstance()->getRawTemplateData());
527 * Starts the menu property 'entry'
529 * @param $id Id of the menu
532 private function startEntry ($id) {
534 $this->curr['entry_id'] = $id;
536 // Load the entry template for this page
537 $this->getTemplateInstance()->loadCodeTemplate('menu_entry_' . $id . '_start');
539 // Set the variable group to page
540 $this->setVariableGroup('menu');
542 // Set its content in this template instance
543 $this->assignVariable('entry_start_' . $this->curr['id'], $this->getTemplateInstance()->getRawTemplateData());
547 * Finishes the entry node by added another template to the menu
551 private function finishEntry () {
552 // Load the entry template for this page
553 $this->getTemplateInstance()->loadCodeTemplate('menu_entry_' . $this->curr['entry_id'] . '_end');
555 // Set the variable group to page
556 $this->setVariableGroup('menu');
558 // Set its content in this template instance
559 $this->assignVariable('entry_end_' . $this->curr['id'], $this->getTemplateInstance()->getRawTemplateData());
562 * Starts the menu property 'anchor'
564 * @param $id Id of the anchor
565 * @param $link Link text of the anchor
566 * @param $title Link title of the anchor
569 private function startAnchor () {
570 $this->partialStub('Please implement this method.');
574 * Finishes the anchor node by added another template to the menu
578 private function finishAnchor () {
579 $this->partialStub('Please implement this method.');
583 * Getter for menu cache file (FQFN)
585 * @return $fqfn Full-qualified file name of the menu cache
587 public function getMenuCacheFqfn () {
588 // Get the FQFN ready
589 $fqfn = sprintf("%s%s%s/%s.%s",
590 $this->getConfigInstance()->getConfigEntry('base_path'),
591 $this->getGenericBasePath(),
594 $this->getMenuInstance()->getMenuName() . ':' .
595 $this->__toString() . ':' .
596 $this->getMenuInstance()->__toString()
598 $this->getMenuInstance()->getMenuType()