]> git.mxchange.org Git - hub.git/blob - application/hub/main/template/announcement/class_AnnouncementTemplateEngine.php
State pattern added, hub continued (sorry, I let it lay around uncommitted for long...
[hub.git] / application / hub / main / template / announcement / class_AnnouncementTemplateEngine.php
1 <?php
2 /**
3  * A Announcement template engine class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
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
10  * @todo                This template engine does not make use of setTemplateType()
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 class AnnouncementTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
26         /**
27          * Main nodes in the XML tree ('menu' is ignored)
28          */
29         private $mainNodes = array(
30                 'announcement',
31         );
32
33         /**
34          * Sub nodes in the XML tree
35          */
36         private $subNodes = array(
37                 'announcement-data',
38                 'listener',
39                 'hub-listener',
40                 'client-listener',
41                 'node-id',
42                 'node-status',
43                 'tcp-port',
44                 'udp-port',
45                 'session-id',
46                 'object-type-list',
47         );
48
49         /**
50          * Current main node
51          */
52         private $curr = array();
53
54         /**
55          * Content from dependency
56          */
57         private $dependencyContent = array();
58
59         /**
60          * Protected constructor
61          *
62          * @return      void
63          */
64         protected function __construct () {
65                 // Call parent constructor
66                 parent::__construct(__CLASS__);
67         }
68
69         /**
70          * Creates an instance of the class TemplateEngine and prepares it for usage
71          *
72          * @param       $appInstance    A manageable application
73          * @return      $templateInstance       An instance of TemplateEngine
74          * @throws      BasePathIsEmptyException                If the provided $templateBasePath is empty
75          * @throws      InvalidBasePathStringException  If $templateBasePath is no string
76          * @throws      BasePathIsNoDirectoryException  If $templateBasePath is no
77          *                                                                                      directory or not found
78          * @throws      BasePathReadProtectedException  If $templateBasePath is
79          *                                                                                      read-protected
80          */
81         public final static function createAnnouncementTemplateEngine (ManageableApplication $appInstance) {
82                 // Get a new instance
83                 $templateInstance = new AnnouncementTemplateEngine();
84
85                 // Get language and file I/O instances from application
86                 $langInstance = $appInstance->getLanguageInstance();
87                 $ioInstance = $appInstance->getFileIoInstance();
88
89                 // Determine base path
90                 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
91
92                 // Is the base path valid?
93                 if (empty($templateBasePath)) {
94                         // Base path is empty
95                         throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
96                 } elseif (!is_string($templateBasePath)) {
97                         // Is not a string
98                         throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
99                 } elseif (!is_dir($templateBasePath)) {
100                         // Is not a path
101                         throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
102                 } elseif (!is_readable($templateBasePath)) {
103                         // Is not readable
104                         throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
105                 }
106
107                 // Get configuration instance
108                 $configInstance = FrameworkConfiguration::getInstance();
109
110                 // Set the base path
111                 $templateInstance->setTemplateBasePath($templateBasePath);
112
113                 // Set the language and IO instances
114                 $templateInstance->setLanguageInstance($langInstance);
115                 $templateInstance->setFileIoInstance($ioInstance);
116
117                 // Set template extensions
118                 $templateInstance->setRawTemplateExtension($configInstance->getConfigEntry('raw_template_extension'));
119                 $templateInstance->setCodeTemplateExtension($configInstance->getConfigEntry('announcement_template_extension'));
120
121                 // Absolute output path for compiled templates
122                 $templateInstance->setCompileOutputPath($configInstance->getConfigEntry('base_path') . $configInstance->getConfigEntry('compile_output_path'));
123
124                 // Init a variable stacker
125                 $stackerInstance = ObjectFactory::createObjectByConfiguredName('announcement_stacker_class');
126
127                 // Set it
128                 $templateInstance->setStackerInstance($stackerInstance);
129
130                 // Return the prepared instance
131                 return $templateInstance;
132         }
133
134         /**
135          * Load a specified announcement template into the engine
136          *
137          * @param       $template       The announcement template we shall load which is
138          *                                              located in 'announcement' by default
139          * @return      void
140          */
141         public function loadAnnouncementTemplate ($template) {
142                 // Set template type
143                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('announcement_template_type'));
144
145                 // Load the special template
146                 $this->loadTemplate($template);
147         }
148
149         /**
150          * Getter for current main node
151          *
152          * @return      $currMainNode   Current main node
153          */
154         public final function getCurrMainNode () {
155                 return $this->curr['main_node'];
156         }
157
158         /**
159          * Setter for current main node
160          *
161          * @param       $element                Element name to set as current main node
162          * @return      $currMainNode   Current main node
163          */
164         private final function setCurrMainNode ($element) {
165                 $this->curr['main_node'] = (string) $element;
166         }
167
168         /**
169          * Getter for main node array
170          *
171          * @return      $mainNodes      Array with valid main node names
172          */
173         public final function getMainNodes () {
174                 return $this->mainNodes;
175         }
176
177         /**
178          * Getter for sub node array
179          *
180          * @return      $subNodes       Array with valid sub node names
181          */
182         public final function getSubNodes () {
183                 return $this->subNodes;
184         }
185
186         /**
187          * Handles the start element of an XML resource
188          *
189          * @param       $resource               XML parser resource (currently ignored)
190          * @param       $element                The element we shall handle
191          * @param       $attributes             All attributes
192          * @return      void
193          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
194          */
195         public function startElement ($resource, $element, array $attributes) {
196                 // Initial method name which will never be called...
197                 $methodName = 'initMenu';
198
199                 // Make the element name lower-case
200                 $element = strtolower($element);
201
202                 // Is the element a main node?
203                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
204                 if (in_array($element, $this->getMainNodes())) {
205                         // Okay, main node found!
206                         $methodName = 'start' . $this->convertToClassName($element);
207
208                         // Set it
209                         $this->setCurrMainNode($element);
210                 } elseif (in_array($element, $this->getSubNodes())) {
211                         // Sub node found
212                         $methodName = 'start' . $this->convertToClassName($element);
213                 } elseif ($element != 'menu') {
214                         // Invalid node name found
215                         throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
216                 }
217
218                 // Call method
219                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
220                 call_user_func_array(array($this, $methodName), $attributes);
221         }
222
223         /**
224          * Ends the main or sub node by sending out the gathered data
225          *
226          * @param       $resource       An XML resource pointer (currently ignored)
227          * @param       $nodeName       Name of the node we want to finish
228          * @return      void
229          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
230          */
231         public function endElement ($resource, $nodeName) {
232                 // Make all lower-case
233                 $nodeName = strtolower($nodeName);
234
235                 // Does this match with current main node?
236                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
237                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
238                         // Did not match!
239                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
240                 } // END - if
241
242                 // Construct method name
243                 $methodName = 'finish' . $this->convertToClassName($nodeName);
244
245                 // Call the corresponding method
246                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
247                 call_user_func_array(array($this, $methodName), array());
248         }
249
250         /**
251          * Currently not used
252          *
253          * @param       $resource               XML parser resource (currently ignored)
254          * @param       $characters             Characters to handle
255          * @return      void
256          * @todo        Find something useful with this!
257          */
258         public function characterHandler ($resource, $characters) {
259                 // Trim all spaces away
260                 $characters = trim($characters);
261
262                 // Is this string empty?
263                 if (empty($characters)) {
264                         // Then skip it silently
265                         return false;
266                 } // END - if
267
268                 // Assign the found characters to variable and use the last entry from
269                 // stack as the name
270                 parent::assignVariable($this->getStackerInstance()->getNamed('announcement'), $characters);
271         }
272
273         /**
274          * Handles the template dependency for given node
275          *
276          * @param       $node                                   The node we should load a dependency template
277          * @param       $templateDependency             A template to load to satisfy dependencies
278          * @return      void
279          */
280         private function handleTemplateDependency ($node, $templateDependency) {
281                 // Is the template dependency set?
282                 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
283                         // Get a temporay menu template instance
284                         $templateInstance = ObjectFactory::createObjectByConfiguredName('announcement_template_class', array($this->getApplicationInstance()));
285
286                         // Then load it
287                         $templateInstance->loadAnnouncementTemplate($templateDependency);
288
289                         // Get an XmlParser instance
290                         $templateInstance->renderXmlContent();
291
292                         // Parse the template's content contents
293                         $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
294                 } // END - if
295         }
296
297         /**
298          * Getter for cache file (FQFN)
299          *
300          * @return      $fqfn   Full-qualified file name of the menu cache
301          */
302         public function getMenuCacheFqfn () {
303                 // Get the FQFN ready
304                 $fqfn = sprintf("%s%s%s/%s.%s",
305                         $this->getConfigInstance()->getConfigEntry('base_path'),
306                         $this->getGenericBasePath(),
307                         'menus/_cache',
308                         md5(
309                                 $this->getMenuInstance()->getMenuName() . ':' .
310                                 $this->__toString() . ':' .
311                                 $this->getMenuInstance()->__toString()
312                         ),
313                         $this->getMenuInstance()->getMenuType()
314                 );
315
316                 // Return it
317                 return $fqfn;
318         }
319
320         /**
321          * Starts the announcement
322          *
323          * @return      void
324          */
325         private function startAnnouncement () {
326                 // Push the node name on the stacker
327                 $this->getStackerInstance()->pushNamed('announcement', 'announcement');
328         }
329
330         /**
331          * Starts the announcement data
332          *
333          * @return      void
334          */
335         private function startAnnouncementData () {
336                 // Push the node name on the stacker
337                 $this->getStackerInstance()->pushNamed('announcement', 'announcement-data');
338         }
339
340         /**
341          * Starts the node id
342          *
343          * @return      void
344          */
345         private function startNodeId () {
346                 // Push the node name on the stacker
347                 $this->getStackerInstance()->pushNamed('announcement', 'node-id');
348         }
349
350         /**
351          * Starts the node status
352          *
353          * @return      void
354          */
355         private function startNodeStatus () {
356                 // Push the node name on the stacker
357                 $this->getStackerInstance()->pushNamed('announcement', 'node-status');
358         }
359
360         /**
361          * Starts the listener
362          *
363          * @return      void
364          */
365         private function startListener () {
366                 // Push the node name on the stacker
367                 $this->getStackerInstance()->pushNamed('announcement', 'listener');
368         }
369
370         /**
371          * Starts the client listener
372          *
373          * @return      void
374          */
375         private function startClientListener () {
376                 // Push the node name on the stacker
377                 $this->getStackerInstance()->pushNamed('announcement', 'client-listener');
378         }
379
380         /**
381          * Starts the hub listener
382          *
383          * @return      void
384          */
385         private function startHubListener () {
386                 // Push the node name on the stacker
387                 $this->getStackerInstance()->pushNamed('announcement', 'hub-listener');
388         }
389
390         /**
391          * Starts the TCP port
392          *
393          * @return      void
394          */
395         private function startTcpPort () {
396                 // Push the node name on the stacker
397                 $this->getStackerInstance()->pushNamed('announcement', 'tcp-port');
398         }
399
400         /**
401          * Starts the UDP port
402          *
403          * @return      void
404          */
405         private function startUdpPort () {
406                 // Push the node name on the stacker
407                 $this->getStackerInstance()->pushNamed('announcement', 'udp-port');
408         }
409
410         /**
411          * Starts the session id
412          *
413          * @return      void
414          */
415         private function startSessionId () {
416                 // Push the node name on the stacker
417                 $this->getStackerInstance()->pushNamed('announcement', 'session-id');
418         }
419
420         /**
421          * Starts the object type list
422          *
423          * @return      void
424          */
425         private function startObjectTypeList () {
426                 // Push the node name on the stacker
427                 $this->getStackerInstance()->pushNamed('announcement', 'object-type-list');
428         }
429
430         /**
431          * Starts the object type
432          *
433          * @return      void
434          */
435         private function startObjectType () {
436                 // Push the node name on the stacker
437                 $this->getStackerInstance()->pushNamed('announcement', 'object-type');
438         }
439
440         /**
441          * Finishes the object type
442          *
443          * @return      void
444          */
445         private function finishObjectType () {
446                 // Pop the last entry
447                 $this->getStackerInstance()->popNamed('announcement');
448         }
449
450         /**
451          * Finishes the object type list
452          *
453          * @return      void
454          */
455         private function finishObjectTypeList () {
456                 // Pop the last entry
457                 $this->getStackerInstance()->popNamed('announcement');
458         }
459
460         /**
461          * Finishes the session id
462          *
463          * @return      void
464          */
465         private function finishSessionId () {
466                 // Pop the last entry
467                 $this->getStackerInstance()->popNamed('announcement');
468         }
469
470         /**
471          * Finishes the UDP port
472          *
473          * @return      void
474          */
475         private function finishUdpPort () {
476                 // Pop the last entry
477                 $this->getStackerInstance()->popNamed('announcement');
478         }
479
480         /**
481          * Finishes the TCP port
482          *
483          * @return      void
484          */
485         private function finishTcpPort () {
486                 // Pop the last entry
487                 $this->getStackerInstance()->popNamed('announcement');
488         }
489
490         /**
491          * Finishes the hub listener
492          *
493          * @return      void
494          */
495         private function finishHubListener () {
496                 // Pop the last entry
497                 $this->getStackerInstance()->popNamed('announcement');
498         }
499
500         /**
501          * Finishes the client listener
502          *
503          * @return      void
504          */
505         private function finishClientListener () {
506                 // Pop the last entry
507                 $this->getStackerInstance()->popNamed('announcement');
508         }
509
510         /**
511          * Finishes the listener
512          *
513          * @return      void
514          */
515         private function finishListener () {
516                 // Pop the last entry
517                 $this->getStackerInstance()->popNamed('announcement');
518         }
519
520         /**
521          * Finishes the node status
522          *
523          * @return      void
524          */
525         private function finishNodeStatus () {
526                 // Pop the last entry
527                 $this->getStackerInstance()->popNamed('announcement');
528         }
529
530         /**
531          * Finishes the node id
532          *
533          * @return      void
534          */
535         private function finishNodeId () {
536                 // Pop the last entry
537                 $this->getStackerInstance()->popNamed('announcement');
538         }
539
540         /**
541          * Finishes the announcement data
542          *
543          * @return      void
544          */
545         private function finishAnnouncementData () {
546                 // Pop the last entry
547                 $this->getStackerInstance()->popNamed('announcement');
548         }
549
550         /**
551          * Finishes the announcement
552          *
553          * @return      void
554          */
555         private function finishAnnouncement () {
556                 // Pop the last entry
557                 $this->getStackerInstance()->popNamed('announcement');
558         }
559 }
560
561 // [EOF]
562 ?>