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(
52 private $menuInstance = null;
57 private $curr = array();
60 * Content from depency
62 private $depencyContent = array();
65 * Protected constructor
69 protected function __construct () {
70 // Call parent constructor
71 parent::__construct(__CLASS__);
75 * Creates an instance of the class TemplateEngine and prepares it for usage
77 * @param $appInstance A manageable application
78 * @param $menuInstance A RenderableMenu instance
79 * @return $tplInstance An instance of TemplateEngine
80 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
81 * @throws InvalidBasePathStringException If $templateBasePath is no string
82 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
83 * directory or not found
84 * @throws BasePathReadProtectedException If $templateBasePath is
87 public final static function createMenuTemplateEngine (ManageableApplication $appInstance, RenderableMenu $menuInstance) {
89 $tplInstance = new MenuTemplateEngine();
91 // Get language and file I/O instances from application
92 $langInstance = $appInstance->getLanguageInstance();
93 $ioInstance = $appInstance->getFileIoInstance();
95 // Determine base path
96 $templateBasePath = $tplInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
98 // Is the base path valid?
99 if (empty($templateBasePath)) {
100 // Base path is empty
101 throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
102 } elseif (!is_string($templateBasePath)) {
104 throw new InvalidBasePathStringException(array($tplInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
105 } elseif (!is_dir($templateBasePath)) {
107 throw new BasePathIsNoDirectoryException(array($tplInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
108 } elseif (!is_readable($templateBasePath)) {
110 throw new BasePathReadProtectedException(array($tplInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
113 // Get configuration instance
114 $configInstance = FrameworkConfiguration::getInstance();
117 $tplInstance->setTemplateBasePath($templateBasePath);
119 // Set the language and IO instances
120 $tplInstance->setLanguageInstance($langInstance);
121 $tplInstance->setFileIoInstance($ioInstance);
123 // Set template extensions
124 $tplInstance->setRawTemplateExtension($configInstance->getConfigEntry('raw_template_extension'));
125 $tplInstance->setCodeTemplateExtension($configInstance->getConfigEntry('menu_template_extension'));
127 // Absolute output path for compiled templates
128 $tplInstance->setCompileOutputPath($configInstance->getConfigEntry('base_path') . $configInstance->getConfigEntry('compile_output_path'));
130 // Set the menu instance
131 $tplInstance->setMenuInstance($menuInstance);
133 // Return the prepared instance
138 * Load a specified menu template into the engine
140 * @param $template The menu template we shall load which is
141 * located in 'menu' by default
144 public function loadMenuTemplate ($template) {
146 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
148 // Load the special template
149 $this->loadTemplate($template);
153 * Getter for current main node
155 * @return $currMainNode Current main node
157 public final function getCurrMainNode () {
158 return $this->curr['main_node'];
162 * Setter for current main node
164 * @param $element Element name to set as current main node
165 * @return $currMainNode Current main node
167 private final function setCurrMainNode ($element) {
168 $this->curr['main_node'] = (string) $element;
172 * Getter for main node array
174 * @return $mainNodes Array with valid main node names
176 public final function getMainNodes () {
177 return $this->mainNodes;
181 * Getter for sub node array
183 * @return $subNodes Array with valid sub node names
185 public final function getSubNodes () {
186 return $this->subNodes;
190 * Handles the start element of an XML resource
192 * @param $resource XML parser resource (currently ignored)
193 * @param $element The element we shall handle
194 * @param $attributes All attributes
196 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
198 public function startElement ($resource, $element, array $attributes) {
199 // Initial method name which will never be called...
200 $methodName = 'initMenu';
202 // Make the element name lower-case
203 $element = strtolower($element);
205 // Is the element a main node?
206 //* DEBUG: */ echo "START: >".$element."<<br />\n";
207 if (in_array($element, $this->getMainNodes())) {
208 // Okay, main node found!
209 $methodName = 'start' . $this->convertToClassName($element);
212 $this->setCurrMainNode($element);
213 } elseif (in_array($element, $this->getSubNodes())) {
215 $methodName = 'start' . $this->convertToClassName($element);
216 } elseif ($element != 'menu') {
217 // Invalid node name found
218 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
222 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
223 call_user_func_array(array($this, $methodName), $attributes);
227 * Ends the main or sub node by sending out the gathered data
229 * @param $resource An XML resource pointer (currently ignored)
230 * @param $nodeName Name of the node we want to finish
232 * @throws XmlNodeMismatchException If current main node mismatches the closing one
234 public function endElement ($resource, $nodeName) {
235 // Make all lower-case
236 $nodeName = strtolower($nodeName);
238 // Does this match with current main node?
239 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
240 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
242 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
245 // Construct method name
246 $methodName = 'finish' . $this->convertToClassName($nodeName);
248 // Call the corresponding method
249 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
250 call_user_func_array(array($this, $methodName), array());
256 * @param $resource XML parser resource (currently ignored)
257 * @param $characters Characters to handle
259 * @todo Find something useful with this!
261 public function characterHandler ($resource, $characters) {
262 // Trim all spaces away
263 $characters = trim($characters);
265 // Is this string empty?
266 if (empty($characters)) {
267 // Then skip it silently
272 $this->partialStub('Handling extra characters is not yet supported! length='.strlen($characters));
276 * Handles the template depency for given node
278 * @param $node The node we should load a depency template
279 * @param $templateDepency A template to load to satisfy depencies
282 private function handleTemplateDepency ($node, $templateDepency) {
283 // Is the template depency set?
284 if ((!empty($templateDepency)) && (!isset($this->depencyContent[$node]))) {
285 // Get a temporay menu template instance
286 $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getApplicationInstance(), $this->getMenuInstance()));
289 $templateInstance->loadMenuTemplate($templateDepency);
291 // Get an XmlParser instance
292 $templateInstance->renderXmlContent();
294 // Parse the template's content contents
295 $this->depencyContent[$node] = $templateInstance->getRawTemplateData();
300 * Intializes the menu
302 * @param $templateDepency A template to load to satisfy depencies
304 * @todo Add cache creation here
306 private function initMenu ($templateDepency = '') {
307 // Get web template engine
308 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('web_template_class', array($this->getApplicationInstance())));
310 // Handle the depency template
311 $this->handleTemplateDepency('menu', $templateDepency);
313 // Load the header template for this page
314 $this->getTemplateInstance()->loadCodeTemplate('menu_global_start');
316 // Set the variable group to page
317 $this->setVariableGroup('menu');
319 // Set its content in this template instance
320 $this->assignVariable('menu_start', $this->getTemplateInstance()->getRawTemplateData());
328 private function finishMenu () {
329 // Load the header template for this page
330 $this->getTemplateInstance()->loadCodeTemplate('menu_global_end');
332 // Set the variable group to page
333 $this->setVariableGroup('menu');
335 // Set its content in this template instance
336 $this->assignVariable('menu_end', $this->getTemplateInstance()->getRawTemplateData());
340 * Starts the menu entries by loading a (maybe) provided template depency
342 * @param $templateDepency A template to load to satisfy depencies
345 private function startEntryList ($templateDepency = '') {
346 // Handle the depency template
347 $this->handleTemplateDepency('entries', $templateDepency);
349 // Load the header template for this page
350 $this->getTemplateInstance()->loadCodeTemplate('menu_entries_start');
352 // Set the variable group to page
353 $this->setVariableGroup('menu');
355 // Set its content in this template instance
356 $this->assignVariable('entries_start', $this->getTemplateInstance()->getRawTemplateData());
360 * Finishes the menu entries
364 private function finishEntryList () {
365 // Load the header template for this page
366 $this->getTemplateInstance()->loadCodeTemplate('menu_entries_end');
368 // Set the variable group to page
369 $this->setVariableGroup('menu');
371 // Set its content in this template instance
372 $this->assignVariable('entries_end', $this->getTemplateInstance()->getRawTemplateData());
376 * Starts the menu header
380 private function startHeader () {
381 // Do we have a template instance?
382 if (is_null($this->getTemplateInstance())) {
383 // Init template instance for underlaying web templates
384 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
386 // Set it in this template engine
387 $this->setTemplateInstance($templateInstance);
390 // Load the header template for this page
391 $this->getTemplateInstance()->loadCodeTemplate('menu_header_start');
393 // Set the variable group to page
394 $this->setVariableGroup('menu');
396 // Set its content in this template instance
397 $this->assignVariable('header', $this->getTemplateInstance()->getRawTemplateData());
401 * Finishes the menu header
405 private function finishHeader () {
406 // Load the header template for this page
407 $this->getTemplateInstance()->loadCodeTemplate('menu_header_end');
409 // Set the variable group to page
410 $this->setVariableGroup('menu');
412 // Set its content in this template instance
413 $this->assignVariable('header_end', $this->getTemplateInstance()->getRawTemplateData());
417 * Starts the menu footer
421 private function startFooter () {
422 // Do we have a template instance?
423 if (is_null($this->getTemplateInstance())) {
424 // Init template instance for underlaying web templates
425 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
427 // Set it in this template engine
428 $this->setTemplateInstance($templateInstance);
431 // Load the footer template for this page
432 $this->getTemplateInstance()->loadCodeTemplate('menu_footer_start');
434 // Set the variable group to page
435 $this->setVariableGroup('menu');
437 // Set its content in this template instance
438 $this->assignVariable('footer', $this->getTemplateInstance()->getRawTemplateData());
442 * Finishes the menu footer
446 private function finishFooter () {
447 // Load the footer template for this page
448 $this->getTemplateInstance()->loadCodeTemplate('menu_footer_end');
450 // Set the variable group to page
451 $this->setVariableGroup('menu');
453 // Set its content in this template instance
454 $this->assignVariable('footer_end', $this->getTemplateInstance()->getRawTemplateData());
458 * Starts the menu property 'title'
460 * @param $id Id of the menu
461 * @param $class The title to add to the menu
464 private function startTitle ($id, $class) {
466 $this->curr['id'] = $id;
468 // Load the title template for this page
469 $this->getTemplateInstance()->loadCodeTemplate('menu_title_' . $id . '_start');
471 // Set the variable group to page
472 $this->setVariableGroup('menu');
474 // Set its content in this template instance
475 $this->assignVariable('title_start_' . $this->curr['id'], $this->getTemplateInstance()->getRawTemplateData());
479 * Finishes the title node by added another template to the menu
483 private function finishTitle () {
484 // Load the title template for this page
485 $this->getTemplateInstance()->loadCodeTemplate('menu_title_' . $this->curr['id'] . '_end');
487 // Set the variable group to page
488 $this->setVariableGroup('menu');
490 // Set its content in this template instance
491 $this->assignVariable('title_end_' . $this->curr['id'], $this->getTemplateInstance()->getRawTemplateData());
495 * Starts the menu text
499 private function startText () {
500 // Do we have a template instance?
501 if (is_null($this->getTemplateInstance())) {
502 // Init template instance for underlaying web templates
503 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
505 // Set it in this template engine
506 $this->setTemplateInstance($templateInstance);
509 // Load the text template for this page
510 $this->getTemplateInstance()->loadCodeTemplate('menu_text_start');
512 // Set the variable group to page
513 $this->setVariableGroup('menu');
515 // Set its content in this template instance
516 $this->assignVariable('text', $this->getTemplateInstance()->getRawTemplateData());
520 * Finishes the menu text
524 private function finishText () {
525 // Load the text template for this page
526 $this->getTemplateInstance()->loadCodeTemplate('menu_text_end');
528 // Set the variable group to page
529 $this->setVariableGroup('menu');
531 // Set its content in this template instance
532 $this->assignVariable('text_end', $this->getTemplateInstance()->getRawTemplateData());
536 * Starts the menu property 'entry'
538 * @param $id Id of the menu
541 private function startEntry ($id) {
543 $this->curr['entry_id'] = $id;
545 // Load the entry template for this page
546 $this->getTemplateInstance()->loadCodeTemplate('menu_entry_' . $id . '_start');
548 // Set the variable group to page
549 $this->setVariableGroup('menu');
551 // Set its content in this template instance
552 $this->assignVariable('entry_start_' . $this->curr['id'], $this->getTemplateInstance()->getRawTemplateData());
556 * Finishes the entry node by added another template to the menu
560 private function finishEntry () {
561 // Load the entry template for this page
562 $this->getTemplateInstance()->loadCodeTemplate('menu_entry_' . $this->curr['entry_id'] . '_end');
564 // Set the variable group to page
565 $this->setVariableGroup('menu');
567 // Set its content in this template instance
568 $this->assignVariable('entry_end_' . $this->curr['id'], $this->getTemplateInstance()->getRawTemplateData());
571 * Starts the menu property 'anchor'
573 * @param $id Id of the anchor
574 * @param $link Link text of the anchor
575 * @param $title Link title of the anchor
578 private function startAnchor () {
579 $this->partialStub('Please implement this method.');
583 * Finishes the anchor node by added another template to the menu
587 private function finishAnchor () {
588 $this->partialStub('Please implement this method.');
592 * Getter for menu cache file (FQFN)
594 * @return $fqfn Full-qualified file name of the menu cache
596 public function getMenuCacheFqfn () {
597 // Get the FQFN ready
598 $fqfn = sprintf("%s%s%s/%s.%s",
599 $this->getConfigInstance()->getConfigEntry('base_path'),
600 $this->getGenericBasePath(),
603 $this->getMenuInstance()->getMenuName() . ':' .
604 $this->__toString() . ':' .
605 $this->getMenuInstance()->__toString()
607 $this->getMenuInstance()->getMenuType()