]> git.mxchange.org Git - hub.git/blob - application/hub/class_ApplicationHelper.php
Filters/commands added, missing config entries added
[hub.git] / application / hub / class_ApplicationHelper.php
1 <?php
2 /**
3  * A class holding general data about the application and some methods for
4  * the management including the entry point.
5  *
6  * E.g.:
7  *
8  * index.php?app=my_app
9  *
10  * You need to create a folder in the folder "application" named "my_app"
11  * (without the quotes) and create a include file called
12  * class_ApplicationHelper.php. You have to write the same class for your
13  * application and implement the same interface called ManageableApplication
14  * because this class include file will be searched for.
15  *
16  * It is good when you avoid more GET parameters to keep URLs short and sweet.
17  * But sometimes you need some GET paramerers e.g. for your imprint or info page
18  * or other linked pages which you have to create and state some informations.
19  *
20  * Please remember that this include file is being loaded *before* the class
21  * loader is loading classes from "exceptions", "interfaces" and "main"!
22  *
23  * @author              Roland Haeder <webmaster@ship-simu.org>
24  * @version             0.0
25  * @copyright   Copyright (c) 2007 - 2008 Roland Haeder, 2009 Hub Developer Team
26  * @license             GNU GPL 3.0 or any newer version
27  *
28  * This program is free software: you can redistribute it and/or modify
29  * it under the terms of the GNU General Public License as published by
30  * the Free Software Foundation, either version 3 of the License, or
31  * (at your option) any later version.
32  *
33  * This program is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36  * GNU General Public License for more details.
37  *
38  * You should have received a copy of the GNU General Public License
39  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
40  */
41 class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplication, Registerable {
42         /**
43          * The version number of this application
44          */
45         private $appVersion = "";
46
47         /**
48          * The human-readable name for this application
49          */
50         private $appName = "";
51
52         /**
53          * The short uni*-like name for this application
54          */
55         private $shortName = "";
56
57         /**
58          * An instance of this class
59          */
60         private static $thisInstance = null;
61
62         /**
63          * Private constructor
64          *
65          * @return      void
66          */
67         protected function __construct () {
68                 // Call parent constructor
69                 parent::__construct(__CLASS__);
70
71                 // Tidy up a little
72                 $this->removeSystemArray();
73                 $this->removeNumberFormaters();
74         }
75
76         /**
77          * Getter for an instance of this class
78          *
79          * @return      $thisInstance   An instance of this class
80          */
81         public final static function getInstance () {
82                 // Is the instance there?
83                 if (is_null(self::$thisInstance)) {
84                         self::$thisInstance = new ApplicationHelper();
85                 }
86
87                 // Return the instance
88                 return self::$thisInstance;
89         }
90
91         /**
92          * Getter for the version number
93          *
94          * @return      $appVersion     The application's version number
95          */
96         public final function getAppVersion () {
97                 return $this->appVersion;
98         }
99         /**
100          * Setter for the version number
101          *
102          * @param       $appVersion     The application's version number
103          * @return      void
104          */
105         public final function setAppVersion ($appVersion) {
106                 // Cast and set it
107                 $this->appVersion = (string) $appVersion;
108         }
109
110         /**
111          * Getter for human-readable name
112          *
113          * @return      $appName        The application's human-readable name
114          */
115         public final function getAppName () {
116                 return $this->appName;
117         }
118
119         /**
120          * Setter for human-readable name
121          *
122          * @param       $appName        The application's human-readable name
123          * @return      void
124          */
125         public final function setAppName ($appName) {
126                 // Cast and set it
127                 $this->appName = (string) $appName;;
128         }
129
130         /**
131          * Getter for short uni*-like name
132          *
133          * @return      $shortName      The application's short uni*-like name
134          */
135         public final function getAppShortName () {
136                 return $this->shortName;
137         }
138
139         /**
140          * Setter for short uni*-like name
141          *
142          * @param       $shortName      The application's short uni*-like name
143          * @return      void
144          */
145         public final function setAppShortName ($shortName) {
146                 // Cast and set it
147                 $this->shortName = (string) $shortName;
148         }
149
150         /**
151          * Launches the hub system
152          *
153          * @return      void
154          */
155         public final function entryPoint () {
156                 // Create a new request object
157                 $requestInstance = ObjectFactory::createObjectByName('ConsoleRequest');
158
159                 // Remember request instance here
160                 $this->setRequestInstance($requestInstance);
161
162                 // Default response is console
163                 $response = 'console';
164                 $responseType = 'console';
165
166                 // Do we have another response?
167                 if ($requestInstance->isRequestElementSet('request')) {
168                         // Then use it
169                         $response = strtolower($requestInstance->getRequestElement('request'));
170                         $responseType = $response;
171                 } // END - if
172
173                 // ... and a new response object
174                 $responseClass = sprintf("%sResponse", $this->convertToClassName($response));
175                 $responseInstance = ObjectFactory::createObjectByName($responseClass, array($this));
176
177                 // Remember response instance here
178                 $this->setResponseInstance($responseInstance);
179
180                 // Get the parameter from the request
181                 $commandName = $requestInstance->getRequestElement('page');
182
183                 // If it is null then get default command
184                 if (is_null($commandName)) {
185                         // Get default command
186                         $commandName = $responseInstance->getDefaultCommand();
187
188                         // Set it in request
189                         $requestInstance->setRequestElement('command', $commandName);
190                 } // END - if
191
192                 // Get a resolver
193                 $resolverClass = sprintf("%sControllerResolver", $this->convertToClassName($responseType));
194                 $resolverInstance = ObjectFactory::createObjectByName($resolverClass, array($commandName, $this));
195
196                 // Get a controller instance as well
197                 $this->controllerInstance = $resolverInstance->resolveController();
198
199                 // Handle the request
200                 $this->controllerInstance->handleRequest($requestInstance, $responseInstance);
201                 // ----------------------------- Init phase ---------------------------
202
203                 // The default node-mode is from our configuration
204                 $nodeMode = $this->getConfigInstance()->readConfig('node_mode');
205                 die("Until here!\n");
206
207                 // Prepare a ConsoleRequest class to catch all parameters
208                 $requestInstance = ObjectFactory::createObjectByName('ConsoleRequest');
209
210                 // Is the node 'mode' parameter set?
211                 if ($requestInstance->isRequestElementSet('mode')) {
212                         // Then use this which overrides the config entry temporarily
213                         $nodeMode = $requestInstance->getRequestElement('mode');
214                 } else {
215                         // Set it for easier re-usage
216                         $requestInstance->setRequestElement('mode', $nodeMode);
217                 }
218
219                 // Now convert the node-mode in a class name
220                 $className = 'Hub' . $this->convertToClassName($nodeMode) . 'Node';
221
222                 // And try to instance it
223                 try {
224                         // Get an instance
225                         $nodeInstance = ObjectFactory::createObjectByName($className, array($requestInstance));
226
227                         // Set the app instance
228                         $nodeInstance->setApplicationInstance($this);
229
230                         // Initialize all filters
231                         $nodeInstance->initializeFilters();
232                 } catch (ClassNotFoundException $e) {
233                         // This exception means, the node mode is invalid.
234                         // @TODO Can we rewrite this to app_die() ?
235                         die('Node mode ' . $nodeMode . ' is invalid.' . "\n");
236                 }
237
238                 // ----------------------- Output teaser lines ------------------------
239                 // Output some introducing lines to the console. This should be later
240                 // be switched off if this is a productive release because it would be
241                 // only visible to the logfile.
242                 $nodeInstance->outputConsoleTeaser();
243
244                 // ----------------------- Bootstrapping phase ------------------------
245                 // Try to bootstrap the node and pass the request instance to it for
246                 // extra arguments which mostly override config entries or enable special
247                 // features within the hub (none is ready at this development stage)
248                 $this->debugOutput('BOOTSTRAP: Beginning with bootstrap...');
249                 $nodeInstance->doBootstrapping();
250                 $this->debugOutput('BOOTSTRAP: Bootstrap finished.');
251
252                 // ----------------------- Init all query queues ----------------------
253                 // After the bootstrap is done we need to initialize the queues which
254                 // will help us to communicate between the "tasks" a hub needs to do.
255                 $nodeInstance->initQueues();
256
257                 // -------------------------- Hub activation --------------------------
258                 // Activates the hub by doing some final preparation steps and setting
259                 // the attribute $hubIsActive to true
260                 $nodeInstance->activateHub();
261
262                 // ----------------------------- Main loop ----------------------------
263                 // This is the main loop. Queried calls should come back here very fast
264                 // so the whole application runs on nice speed. This while-loop goes
265                 // until the hub is no longer active.
266                 while ($nodeInstance->isHubActive()) {
267                 } // END - while
268
269                 // -------------------------- Shutdown phase --------------------------
270                 // Shutting down the hub by saying "good bye" to all connected clients
271                 // and other hubs, flushing all queues and caches.
272                 $nodeInstance->doShutdown();
273         }
274
275         /**
276          * Handle the indexed array of fatal messages and puts them out in an
277          * acceptable fasion
278          *
279          * @param       $messageList    An array of fatal messages
280          * @return      void
281          */
282         public function handleFatalMessages (array $messageList) {
283                 // Walk through all messages
284                 foreach ($messageList as $message) {
285                         die("MSG:" . $message);
286                 }
287         }
288
289         /**
290          * Builds the master template's name
291          *
292          * @return      $masterTemplateName             Name of the master template
293          */
294         public function buildMasterTemplateName () {
295                 return 'node_main';
296         }
297 }
298
299 // [EOF]
300 ?>