]> git.mxchange.org Git - hub.git/blob - application/hub/main/template/objects/class_ObjectRegistryTemplateEngine.php
bd8a99c431e12120de15a240cbc2c31cb0045c28
[hub.git] / application / hub / main / template / objects / class_ObjectRegistryTemplateEngine.php
1 <?php
2 /**
3  * An ObjectRegistry 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 - 2011 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 ObjectRegistryTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
26         /**
27          * Instance for the object registry
28          */
29         private $objectRegistryInstance = null;
30
31         /**
32          * Main nodes in the XML tree ('menu' is ignored)
33          */
34         private $mainNodes = array(
35                 'object-registry'
36         );
37
38         /**
39          * Sub nodes in the XML tree
40          */
41         private $subNodes = array(
42                 'object-list',
43                 'object-list-entry',
44                 'object-name',
45                 'object-recipient-limitation',
46                 'object-max-spread',
47                 'object-protocol',
48                 'object-recipient-type'
49         );
50
51         /**
52          * Current main node
53          */
54         private $curr = array();
55
56         /**
57          * Content from dependency
58          */
59         private $dependencyContent = array();
60
61         /**
62          * Protected constructor
63          *
64          * @return      void
65          */
66         protected function __construct () {
67                 // Call parent constructor
68                 parent::__construct(__CLASS__);
69
70                 // Init object type registry instance
71                 $this->objectRegistryInstance = ObjectFactory::createObjectByConfiguredName('object_type_registry_class');
72         }
73
74         /**
75          * Creates an instance of the class TemplateEngine and prepares it for usage
76          *
77          * @param       $appInstance    A manageable application
78          * @return      $templateInstance       An instance of TemplateEngine
79          * @throws      BasePathIsEmptyException                If the provided $templateBasePath is empty
80          * @throws      InvalidBasePathStringException  If $templateBasePath is no string
81          * @throws      BasePathIsNoDirectoryException  If $templateBasePath is no
82          *                                                                                      directory or not found
83          * @throws      BasePathReadProtectedException  If $templateBasePath is
84          *                                                                                      read-protected
85          */
86         public static final function createObjectRegistryTemplateEngine (ManageableApplication $appInstance) {
87                 // Get a new instance
88                 $templateInstance = new ObjectRegistryTemplateEngine();
89
90                 // Get language and file I/O instances from application
91                 $langInstance = $appInstance->getLanguageInstance();
92                 $ioInstance = $appInstance->getFileIoInstance();
93
94                 // Determine base path
95                 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
96
97                 // Is the base path valid?
98                 if (empty($templateBasePath)) {
99                         // Base path is empty
100                         throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
101                 } elseif (!is_string($templateBasePath)) {
102                         // Is not a string
103                         throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
104                 } elseif (!is_dir($templateBasePath)) {
105                         // Is not a path
106                         throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
107                 } elseif (!is_readable($templateBasePath)) {
108                         // Is not readable
109                         throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
110                 }
111
112                 // Set the base path
113                 $templateInstance->setTemplateBasePath($templateBasePath);
114
115                 // Set the language and IO instances
116                 $templateInstance->setLanguageInstance($langInstance);
117                 $templateInstance->setFileIoInstance($ioInstance);
118
119                 // Set template extensions
120                 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
121                 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('object_registry_template_extension'));
122
123                 // Absolute output path for compiled templates
124                 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
125
126                 // Init a variable stacker
127                 $stackerInstance = ObjectFactory::createObjectByConfiguredName('object_registry_stacker_class');
128
129                 // Set it
130                 $templateInstance->setStackerInstance($stackerInstance);
131
132                 // Return the prepared instance
133                 return $templateInstance;
134         }
135
136         /**
137          * Load a specified object_registry template into the engine
138          *
139          * @param       $template       The object_registry template we shall load which is
140          *                                              located in 'object_registry' by default
141          * @return      void
142          */
143         public function loadObjectRegistryTemplate ($template) {
144                 // Set template type
145                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('object_registry_template_type'));
146
147                 // Load the special template
148                 $this->loadTemplate($template);
149         }
150
151         /**
152          * Getter for current main node
153          *
154          * @return      $currMainNode   Current main node
155          */
156         public final function getCurrMainNode () {
157                 return $this->curr['main_node'];
158         }
159
160         /**
161          * Setter for current main node
162          *
163          * @param       $element                Element name to set as current main node
164          * @return      $currMainNode   Current main node
165          */
166         private final function setCurrMainNode ($element) {
167                 $this->curr['main_node'] = (string) $element;
168         }
169
170         /**
171          * Getter for main node array
172          *
173          * @return      $mainNodes      Array with valid main node names
174          */
175         public final function getMainNodes () {
176                 return $this->mainNodes;
177         }
178
179         /**
180          * Getter for sub node array
181          *
182          * @return      $subNodes       Array with valid sub node names
183          */
184         public final function getSubNodes () {
185                 return $this->subNodes;
186         }
187
188         /**
189          * Handles the start element of an XML resource
190          *
191          * @param       $resource               XML parser resource (currently ignored)
192          * @param       $element                The element we shall handle
193          * @param       $attributes             All attributes
194          * @return      void
195          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
196          */
197         public function startElement ($resource, $element, array $attributes) {
198                 // Initial method name which will never be called...
199                 $methodName = 'initObjectRegistry';
200
201                 // Make the element name lower-case
202                 $element = strtolower($element);
203
204                 // Is the element a main node?
205                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
206                 if (in_array($element, $this->getMainNodes())) {
207                         // Okay, main node found!
208                         $methodName = 'start' . $this->convertToClassName($element);
209
210                         // Set it
211                         $this->setCurrMainNode($element);
212                 } elseif (in_array($element, $this->getSubNodes())) {
213                         // Sub node found
214                         $methodName = 'start' . $this->convertToClassName($element);
215                 } else {
216                         // Invalid node name found
217                         throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
218                 }
219
220                 // Call method
221                 call_user_func_array(array($this, $methodName), $attributes);
222         }
223
224         /**
225          * Ends the main or sub node by sending out the gathered data
226          *
227          * @param       $resource       An XML resource pointer (currently ignored)
228          * @param       $nodeName       Name of the node we want to finish
229          * @return      void
230          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
231          */
232         public function endElement ($resource, $nodeName) {
233                 // Make all lower-case
234                 $nodeName = strtolower($nodeName);
235
236                 // Does this match with current main node?
237                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
238                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
239                         // Did not match!
240                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
241                 } // END - if
242
243                 // Construct method name
244                 $methodName = 'finish' . $this->convertToClassName($nodeName);
245
246                 // Call the corresponding method
247                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
248                 call_user_func_array(array($this, $methodName), array());
249         }
250
251         /**
252          * Currently not used
253          *
254          * @param       $resource               XML parser resource (currently ignored)
255          * @param       $characters             Characters to handle
256          * @return      void
257          * @todo        Find something useful with this!
258          */
259         public function characterHandler ($resource, $characters) {
260                 // Trim all spaces away
261                 $characters = trim($characters);
262
263                 // Is this string empty?
264                 if (empty($characters)) {
265                         // Then skip it silently
266                         return false;
267                 } // END - if
268
269                 // Get current XML node name as an array index
270                 $nodeName = $this->getStackerInstance()->getNamed('object_registry');
271
272                 // Is the node name 'object-name'?
273                 if ($nodeName == 'object-name') {
274                         // Output debug message
275                         $this->debugOutput('TAGS: Adding object type ' . $characters . ' to registry.');
276                 } // END - if
277
278                 // Add it to the registry
279                 $this->objectRegistryInstance->addEntry($nodeName, $characters);
280         }
281
282         /**
283          * Handles the template dependency for given node
284          *
285          * @param       $node                                   The node we should load a dependency template
286          * @param       $templateDependency             A template to load to satisfy dependencies
287          * @return      void
288          */
289         private function handleTemplateDependency ($node, $templateDependency) {
290                 // Is the template dependency set?
291                 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
292                         // Get a temporay menu template instance
293                         $templateInstance = ObjectFactory::createObjectByConfiguredName('object_registry_template_class', array($this->getApplicationInstance()));
294
295                         // Then load it
296                         $templateInstance->loadObjectRegistryTemplate($templateDependency);
297
298                         // Get an XmlParser instance
299                         $templateInstance->renderXmlContent();
300
301                         // Parse the template's content contents
302                         $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
303                 } // END - if
304         }
305
306         /**
307          * Getter for cache file (FQFN)
308          *
309          * @return      $fqfn   Full-qualified file name of the menu cache
310          */
311         public function getObjectRegistryCacheFqfn () {
312                 $this->partialStub('Please implement this method.');
313         }
314
315         /**
316          * Starts the object-registry
317          *
318          * @return      void
319          */
320         private function startObjectRegistry () {
321                 // Push the node name on the stacker
322                 $this->getStackerInstance()->pushNamed('object_registry', 'object-registry');
323         }
324
325         /**
326          * Starts the object-list
327          *
328          * @return      void
329          */
330         private function startObjectList () {
331                 // Push the node name on the stacker
332                 $this->getStackerInstance()->pushNamed('object_registry', 'object-list');
333         }
334
335         /**
336          * Starts the object-list-entry
337          *
338          * @return      void
339          */
340         private function startObjectListEntry () {
341                 // Push the node name on the stacker
342                 $this->getStackerInstance()->pushNamed('object_registry', 'object-list');
343         }
344
345         /**
346          * Starts the object-name
347          *
348          * @return      void
349          */
350         private function startObjectName () {
351                 // Push the node name on the stacker
352                 $this->getStackerInstance()->pushNamed('object_registry', 'object-name');
353         }
354
355         /**
356          * Starts the object-recipient-limitation
357          *
358          * @return      void
359          */
360         private function startObjectRecipientLimitation () {
361                 // Push the node name on the stacker
362                 $this->getStackerInstance()->pushNamed('object_registry', 'object-recipient-limitation');
363         }
364
365         /**
366          * Starts the object-max-spread
367          *
368          * @return      void
369          */
370         private function startObjectMaxSpread () {
371                 // Push the node name on the stacker
372                 $this->getStackerInstance()->pushNamed('object_registry', 'object-max-spread');
373         }
374
375         /**
376          * Starts the object-protocol
377          *
378          * @return      void
379          */
380         private function startObjectProtocol () {
381                 // Push the node name on the stacker
382                 $this->getStackerInstance()->pushNamed('object_registry', 'object-protocol');
383         }
384
385         /**
386          * Starts the object-recipient-type
387          *
388          * @return      void
389          */
390         private function startObjectRecipientType () {
391                 // Push the node name on the stacker
392                 $this->getStackerInstance()->pushNamed('object_registry', 'object-recipient-type');
393         }
394
395         /**
396          * Finishes the object-recipient-type
397          *
398          * @return      void
399          */
400         private function finishObjectRecipientType () {
401                 // Pop the last entry
402                 $this->getStackerInstance()->popNamed('object_registry');
403         }
404
405         /**
406          * Finishes the object-protocol
407          *
408          * @return      void
409          */
410         private function finishObjectProtocol () {
411                 // Pop the last entry
412                 $this->getStackerInstance()->popNamed('object_registry');
413         }
414
415         /**
416          * Finishes the object-max-spread
417          *
418          * @return      void
419          */
420         private function finishObjectMaxSpread () {
421                 // Pop the last entry
422                 $this->getStackerInstance()->popNamed('object_registry');
423         }
424
425         /**
426          * Finishes the object-recipient-limitation
427          *
428          * @return      void
429          */
430         private function finishObjectRecipientLimitation () {
431                 // Pop the last entry
432                 $this->getStackerInstance()->popNamed('object_registry');
433         }
434
435         /**
436          * Finishes the object-name
437          *
438          * @return      void
439          */
440         private function finishObjectName () {
441                 // Pop the last entry
442                 $this->getStackerInstance()->popNamed('object_registry');
443         }
444
445         /**
446          * Finishes the object-list-entry
447          *
448          * @return      void
449          */
450         private function finishObjectListEntry () {
451                 // Pop the last entry
452                 $this->getStackerInstance()->popNamed('object_registry');
453         }
454
455         /**
456          * Finishes the object-list
457          *
458          * @return      void
459          */
460         private function finishObjectList () {
461                 // Pop the last entry
462                 $this->getStackerInstance()->popNamed('object_registry');
463         }
464
465         /**
466          * Finishes the object-registry
467          *
468          * @return      void
469          */
470         private function finishObjectRegistry () {
471                 // Pop the last entry
472                 $this->getStackerInstance()->popNamed('object_registry');
473         }
474 }
475
476 // [EOF]
477 ?>