A lot naming conventions applied:
[shipsimu.git] / application / ship-simu / 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.0
25  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
26  * @license             GNU GPL 3.0 or any newer version
27  * @link                http://www.ship-simu.org
28  *
29  * This program is free software: you can redistribute it and/or modify
30  * it under the terms of the GNU General Public License as published by
31  * the Free Software Foundation, either version 3 of the License, or
32  * (at your option) any later version.
33  *
34  * This program is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37  * GNU General Public License for more details.
38  *
39  * You should have received a copy of the GNU General Public License
40  * along with this program. If not, see <http://www.gnu.org/licenses/>.
41  */
42 class ApplicationHelper extends BaseApplication implements ManageableApplication, Registerable {
43         /**
44          * The version number of this application
45          */
46         private $appVersion = "";
47
48         /**
49          * The human-readable name for this application
50          */
51         private $appName = "";
52
53         /**
54          * The short uni*-like name for this application
55          */
56         private $shortName = "";
57
58         /**
59          * The name of the master template
60          */
61         private $masterTemplate = "shipsimu_main";
62
63         /**
64          * An instance of a controller
65          */
66         private $controllerInstance = null;
67
68         /**
69          * An instance of this class
70          */
71         private static $thisInstance = null;
72
73         /**
74          * Protected constructor
75          *
76          * @return      void
77          */
78         protected function __construct () {
79                 // Call parent constructor
80                 parent::__construct(__CLASS__);
81         }
82
83         /**
84          * Getter for an instance of this class
85          *
86          * @return      $thisInstance           An instance of this class
87          */
88         public final static function getInstance () {
89                 // Is the instance there?
90                 if (is_null(self::$thisInstance)) {
91                         self::$thisInstance = new ApplicationHelper();
92                 }
93
94                 // Return the instance
95                 return self::$thisInstance;
96         }
97
98         /**
99          * Getter for the version number
100          *
101          * @return      $appVersion             The application's version number
102          */
103         public final function getAppVersion () {
104                 return $this->appVersion;
105         }
106
107         /**
108          * Setter for the version number
109          *
110          * @param       $appVersion             The application's version number
111          * @return      void
112          */
113         public final function setAppVersion ($appVersion) {
114                 // Cast and set it
115                 $appVersion = (string) $appVersion;
116                 $this->appVersion = $appVersion;
117         }
118
119         /**
120          * Getter for human-readable name
121          *
122          * @return      $appName        The application's human-readable name
123          */
124         public final function getAppName () {
125                 return $this->appName;
126         }
127
128         /**
129          * Setter for human-readable name
130          *
131          * @param       $appName        The application's human-readable name
132          * @return      void
133          */
134         public final function setAppName ($appName) {
135                 // Cast and set it
136                 $appName = (string) $appName;
137                 $this->appName = $appName;
138         }
139
140         /**
141          * Getter for short uni*-like name
142          *
143          * @return      $shortName      The application's short uni*-like name
144          */
145         public final function getAppShortName () {
146                 return $this->shortName;
147         }
148
149         /**
150          * Setter for short uni*-like name
151          *
152          * @param       $shortName      The application's short uni*-like name
153          * @return      void
154          */
155         public final function setAppShortName ($shortName) {
156                 // Cast and set it
157                 $shortName = (string) $shortName;
158                 $this->shortName = $shortName;
159         }
160
161         /**
162          * Getter for master template name
163          *
164          * @return      $masterTemplate         Name of the master template
165          */
166         public final function getMasterTemplate () {
167                 return $this->masterTemplate;
168         }
169
170         /**
171          * Launches the ship-simulator game
172          *
173          * @return      void
174          */
175         public final function entryPoint () {
176                 // Create a new request object
177                 $requestInstance = ObjectFactory::createObjectByName('HttpRequest');
178
179                 // Default response is HTTP (HTML page) and type is "Web"
180                 $response = "http";
181                 $responseType = "web";
182
183                 // Do we have another response?
184                 if ($requestInstance->isRequestElementSet('request')) {
185                         // Then use it
186                         $response = strtolower($requestInstance->getRequestElement('request'));
187                         $responseType = $response;
188                 } // END - if
189
190                 // ... and a new response object
191                 $responseInstance = ObjectFactory::createObjectByName(ucfirst($response)."Response", array($this));
192
193                 // Remember both in this application
194                 $this->setRequestInstance($requestInstance);
195                 $this->setResponseInstance($responseInstance);
196
197                 // Get the parameter from the request
198                 $commandName = $requestInstance->getRequestElement('page');
199
200                 // If it is null then get default command
201                 if (is_null($commandName)) {
202                         $commandName = $responseInstance->getDefaultCommand();
203                 } // END - if
204
205                 // Get a resolver
206                 $resolverInstance = ObjectFactory::createObjectByName(ucfirst($responseType)."ControllerResolver", array($commandName, $this));
207
208                 // Get a controller instance as well
209                 $this->controllerInstance = $resolverInstance->resolveController();
210
211                 // Handle the request
212                 $this->controllerInstance->handleRequest($requestInstance, $responseInstance);
213         }
214
215         /**
216          * Handle the indexed array of fatal messages and puts them out in an
217          * acceptable fasion
218          *
219          * @param       $messageList    An array of fatal messages
220          * @return      void
221          */
222         public function handleFatalMessages (array $messageList) {
223                 // Walk through all messages
224                 foreach ($messageList as $message) {
225                         print("MSG:".$message."<br />\n");
226                 } // END - if
227         }
228
229         /**
230          * Assigns application-depending data
231          *
232          * @param       $templateInstance       An instance of a template engine
233          * @return      void
234          */
235         public function assignExtraTemplateData (CompileableTemplate $templateInstance) {
236                 // Assign charset
237                 $templateInstance->assignConfigVariable('header_charset');
238         }
239 }
240
241 // [EOF]
242 ?>