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