]> git.mxchange.org Git - hub.git/blob - application/hub/main/template/connect/class_SelfConnectTemplateEngine.php
Self-connect continued (still broken), some code moved to generic classes to encapsul...
[hub.git] / application / hub / main / template / connect / class_SelfConnectTemplateEngine.php
1 <?php
2 /**
3  * An SelfConnect 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 SelfConnectTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
26         /**
27          * Main nodes in the XML tree ('menu' is ignored)
28          */
29         private $mainNodes = array(
30                 'self-connect',
31         );
32
33         /**
34          * Sub nodes in the XML tree
35          */
36         private $subNodes = array(
37                 'self-connect-data',
38                 'node-id',
39                 'session-id'
40         );
41
42         /**
43          * Current main node
44          */
45         private $curr = array();
46
47         /**
48          * Content from dependency
49          */
50         private $dependencyContent = array();
51
52         /**
53          * Protected constructor
54          *
55          * @return      void
56          */
57         protected function __construct () {
58                 // Call parent constructor
59                 parent::__construct(__CLASS__);
60         }
61
62         /**
63          * Creates an instance of the class TemplateEngine and prepares it for usage
64          *
65          * @param       $appInstance    A manageable application
66          * @return      $templateInstance       An instance of TemplateEngine
67          * @throws      BasePathIsEmptyException                If the provided $templateBasePath is empty
68          * @throws      InvalidBasePathStringException  If $templateBasePath is no string
69          * @throws      BasePathIsNoDirectoryException  If $templateBasePath is no
70          *                                                                                      directory or not found
71          * @throws      BasePathReadProtectedException  If $templateBasePath is
72          *                                                                                      read-protected
73          */
74         public final static function createSelfConnectTemplateEngine (ManageableApplication $appInstance) {
75                 // Get a new instance
76                 $templateInstance = new SelfConnectTemplateEngine();
77
78                 // Get language and file I/O instances from application
79                 $langInstance = $appInstance->getLanguageInstance();
80                 $ioInstance = $appInstance->getFileIoInstance();
81
82                 // Determine base path
83                 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
84
85                 // Is the base path valid?
86                 if (empty($templateBasePath)) {
87                         // Base path is empty
88                         throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
89                 } elseif (!is_string($templateBasePath)) {
90                         // Is not a string
91                         throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
92                 } elseif (!is_dir($templateBasePath)) {
93                         // Is not a path
94                         throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
95                 } elseif (!is_readable($templateBasePath)) {
96                         // Is not readable
97                         throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
98                 }
99
100                 // Set the base path
101                 $templateInstance->setTemplateBasePath($templateBasePath);
102
103                 // Set the language and IO instances
104                 $templateInstance->setLanguageInstance($langInstance);
105                 $templateInstance->setFileIoInstance($ioInstance);
106
107                 // Set template extensions
108                 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
109                 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('self_connect_template_extension'));
110
111                 // Absolute output path for compiled templates
112                 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
113
114                 // Init a variable stacker
115                 $stackerInstance = ObjectFactory::createObjectByConfiguredName('self_connect_stacker_class');
116
117                 // Set it
118                 $templateInstance->setStackerInstance($stackerInstance);
119
120                 // Return the prepared instance
121                 return $templateInstance;
122         }
123
124         /**
125          * Load a specified self-connect template into the engine
126          *
127          * @param       $template       The self-connect template we shall load which is
128          *                                              located in 'self_connect' by default
129          * @return      void
130          */
131         public function loadSelfConnectTemplate ($template = 'self_connect') {
132                 // Set template type
133                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('self_connect_template_type'));
134
135                 // Load the special template
136                 $this->loadTemplate($template);
137         }
138
139         /**
140          * Getter for current main node
141          *
142          * @return      $currMainNode   Current main node
143          */
144         public final function getCurrMainNode () {
145                 return $this->curr['main_node'];
146         }
147
148         /**
149          * Setter for current main node
150          *
151          * @param       $element                Element name to set as current main node
152          * @return      $currMainNode   Current main node
153          */
154         private final function setCurrMainNode ($element) {
155                 $this->curr['main_node'] = (string) $element;
156         }
157
158         /**
159          * Getter for main node array
160          *
161          * @return      $mainNodes      Array with valid main node names
162          */
163         public final function getMainNodes () {
164                 return $this->mainNodes;
165         }
166
167         /**
168          * Getter for sub node array
169          *
170          * @return      $subNodes       Array with valid sub node names
171          */
172         public final function getSubNodes () {
173                 return $this->subNodes;
174         }
175
176         /**
177          * Handles the start element of an XML resource
178          *
179          * @param       $resource               XML parser resource (currently ignored)
180          * @param       $element                The element we shall handle
181          * @param       $attributes             All attributes
182          * @return      void
183          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
184          */
185         public function startElement ($resource, $element, array $attributes) {
186                 // Initial method name which will never be called...
187                 $methodName = 'initSelfConnect';
188
189                 // Make the element name lower-case
190                 $element = strtolower($element);
191
192                 // Is the element a main node?
193                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
194                 if (in_array($element, $this->getMainNodes())) {
195                         // Okay, main node found!
196                         $methodName = 'start' . $this->convertToClassName($element);
197
198                         // Set it
199                         $this->setCurrMainNode($element);
200                 } elseif (in_array($element, $this->getSubNodes())) {
201                         // Sub node found
202                         $methodName = 'start' . $this->convertToClassName($element);
203                 } else {
204                         // Invalid node name found
205                         throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
206                 }
207
208                 // Call method
209                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
210                 call_user_func_array(array($this, $methodName), $attributes);
211         }
212
213         /**
214          * Ends the main or sub node by sending out the gathered data
215          *
216          * @param       $resource       An XML resource pointer (currently ignored)
217          * @param       $nodeName       Name of the node we want to finish
218          * @return      void
219          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
220          */
221         public function endElement ($resource, $nodeName) {
222                 // Make all lower-case
223                 $nodeName = strtolower($nodeName);
224
225                 // Does this match with current main node?
226                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
227                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
228                         // Did not match!
229                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
230                 } // END - if
231
232                 // Construct method name
233                 $methodName = 'finish' . $this->convertToClassName($nodeName);
234
235                 // Call the corresponding method
236                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
237                 call_user_func_array(array($this, $methodName), array());
238         }
239
240         /**
241          * Currently not used
242          *
243          * @param       $resource               XML parser resource (currently ignored)
244          * @param       $characters             Characters to handle
245          * @return      void
246          * @todo        Find something useful with this!
247          */
248         public function characterHandler ($resource, $characters) {
249                 // Trim all spaces away
250                 $characters = trim($characters);
251
252                 // Is this string empty?
253                 if (empty($characters)) {
254                         // Then skip it silently
255                         return false;
256                 } // END - if
257
258                 // Assign the found characters to variable and use the last entry from
259                 // stack as the name
260                 parent::assignVariable($this->getStackerInstance()->getNamed('self_connect'), $characters);
261         }
262
263         /**
264          * Handles the template dependency for given node
265          *
266          * @param       $node                                   The node we should load a dependency template
267          * @param       $templateDependency             A template to load to satisfy dependencies
268          * @return      void
269          */
270         private function handleTemplateDependency ($node, $templateDependency) {
271                 // Is the template dependency set?
272                 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
273                         // Get a temporay menu template instance
274                         $templateInstance = ObjectFactory::createObjectByConfiguredName('self_connect_template_class', array($this->getApplicationInstance()));
275
276                         // Then load it
277                         $templateInstance->loadSelfConnectTemplate($templateDependency);
278
279                         // Get an XmlParser instance
280                         $templateInstance->renderXmlContent();
281
282                         // Parse the template's content contents
283                         $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
284                 } // END - if
285         }
286
287         /**
288          * Getter for cache file (FQFN)
289          *
290          * @return      $fqfn   Full-qualified file name of the menu cache
291          */
292         public function getMenuCacheFqfn () {
293                 $this->partialStub('Please implement this method.');
294         }
295
296         /**
297          * Starts the self_connect
298          *
299          * @return      void
300          */
301         private function startSelfConnect () {
302                 // Push the node name on the stacker
303                 $this->getStackerInstance()->pushNamed('self_connect', 'self-connect');
304         }
305
306         /**
307          * Starts the self_connect data
308          *
309          * @return      void
310          */
311         private function startSelfConnectData () {
312                 // Push the node name on the stacker
313                 $this->getStackerInstance()->pushNamed('self_connect', 'self-connect-data');
314         }
315
316         /**
317          * Starts the node id
318          *
319          * @return      void
320          */
321         private function startNodeId () {
322                 // Push the node name on the stacker
323                 $this->getStackerInstance()->pushNamed('self_connect', 'node-id');
324         }
325
326         /**
327          * Starts the session id
328          *
329          * @return      void
330          */
331         private function startSessionId () {
332                 // Push the node name on the stacker
333                 $this->getStackerInstance()->pushNamed('self_connect', 'session-id');
334         }
335
336         /**
337          * Finishes the session id
338          *
339          * @return      void
340          */
341         private function finishSessionId () {
342                 // Pop the last entry
343                 $this->getStackerInstance()->popNamed('self_connect');
344         }
345
346         /**
347          * Finishes the node id
348          *
349          * @return      void
350          */
351         private function finishNodeId () {
352                 // Pop the last entry
353                 $this->getStackerInstance()->popNamed('self_connect');
354         }
355
356         /**
357          * Finishes the self_connect data
358          *
359          * @return      void
360          */
361         private function finishSelfConnectData () {
362                 // Pop the last entry
363                 $this->getStackerInstance()->popNamed('self_connect');
364         }
365
366         /**
367          * Finishes the self_connect
368          *
369          * @return      void
370          */
371         private function finishSelfConnect () {
372                 // Pop the last entry
373                 $this->getStackerInstance()->popNamed('self_connect');
374         }
375 }
376
377 // [EOF]
378 ?>