update copyright as changes will happen this year
[core.git] / application / tests / 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@shipsimu.org>
24  * @version             0.0
25  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core 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 $selfInstance = 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
72         /**
73          * Getter for an instance of this class
74          *
75          * @return      $selfInstance   An instance of this class
76          */
77         public static final function getSelfInstance () {
78                 // Is the instance there?
79                 if (is_null(self::$selfInstance)) {
80                         self::$selfInstance = new ApplicationHelper();
81                 } // END - if
82
83                 // Return the instance
84                 return self::$selfInstance;
85         }
86
87         /**
88          * Getter for the version number
89          *
90          * @return      $appVersion     The application's version number
91          */
92         public final function getAppVersion () {
93                 return $this->appVersion;
94         }
95         /**
96          * Setter for the version number
97          *
98          * @param       $appVersion     The application's version number
99          * @return      void
100          */
101         public final function setAppVersion ($appVersion) {
102                 // Cast and set it
103                 $this->appVersion = (string) $appVersion;
104         }
105
106         /**
107          * Getter for human-readable name
108          *
109          * @return      $appName        The application's human-readable name
110          */
111         public final function getAppName () {
112                 return $this->appName;
113         }
114
115         /**
116          * Setter for human-readable name
117          *
118          * @param       $appName        The application's human-readable name
119          * @return      void
120          */
121         public final function setAppName ($appName) {
122                 // Cast and set it
123                 $this->appName = (string) $appName;;
124         }
125
126         /**
127          * Getter for short uni*-like name
128          *
129          * @return      $shortName      The application's short uni*-like name
130          */
131         public final function getAppShortName () {
132                 return $this->shortName;
133         }
134
135         /**
136          * Setter for short uni*-like name
137          *
138          * @param       $shortName      The application's short uni*-like name
139          * @return      void
140          */
141         public final function setAppShortName ($shortName) {
142                 // Cast and set it
143                 $this->shortName = (string) $shortName;
144         }
145
146         /**
147          * Launches the test suite
148          *
149          * @return      void
150          */
151         public final function entryPoint () {
152                 // Set this application in registry
153                 Registry::getRegistry()->addInstance('app', $this);
154
155                 // Default response is console
156                 $response = self::getResponseTypeFromSystem();
157                 $responseType = self::getResponseTypeFromSystem();
158
159                 // Create a new request object
160                 $requestInstance = ObjectFactory::createObjectByName(self::convertToClassName($response) . 'Request');
161
162                 // Remember request instance here
163                 $this->setRequestInstance($requestInstance);
164
165                 // Do we have another response?
166                 if ($requestInstance->isRequestElementSet('request')) {
167                         // Then use it
168                         $response = strtolower($requestInstance->getRequestElement('request'));
169                         $responseType = $response;
170                 } // END - if
171
172                 // ... and a new response object
173                 $responseClass = sprintf('%sResponse', self::convertToClassName($response));
174                 $responseInstance = ObjectFactory::createObjectByName($responseClass, array($this));
175
176                 // Remember response instance here
177                 $this->setResponseInstance($responseInstance);
178
179                 // Get the parameter from the request
180                 $commandName = $requestInstance->getRequestElement('command');
181
182                 // If it is null then get default command
183                 if (is_null($commandName)) {
184                         // Get default command
185                         $commandName = $responseInstance->determineDefaultCommand();
186
187                         // Set it in request
188                         $requestInstance->setRequestElement('command', $commandName);
189                 } // END - if
190
191                 // Get a controller resolver
192                 $resolverClass = self::convertToClassName($this->getAppShortName() . '_' . $responseType . '_controller_resolver');
193                 $resolverInstance = ObjectFactory::createObjectByName($resolverClass, array($commandName, $this));
194
195                 // Get a controller instance as well
196                 $this->setControllerInstance($resolverInstance->resolveController());
197
198                 // Launch the test suite here
199                 $this->getControllerInstance()->handleRequest($requestInstance, $responseInstance);
200
201                 // -------------------------- Shutdown phase --------------------------
202                 // Shutting down the hub by saying "good bye" to all connected peers
203                 // and other hubs, flushing all queues and caches.
204                 self::createDebugInstance(__CLASS__)->debugOutput('MAIN: Shutdown in progress, main loop exited.');
205                 $this->getControllerInstance()->executeShutdownFilters($requestInstance, $responseInstance);
206                 self::createDebugInstance(__CLASS__)->debugOutput('MAIN: Shutdown completed. (This is the last line.)');
207         }
208
209         /**
210          * Handle the indexed array of fatal messages and puts them out in an
211          * acceptable fasion
212          *
213          * @param       $messageList    An array of fatal messages
214          * @return      void
215          */
216         public function handleFatalMessages (array $messageList) {
217                 // Walk through all messages
218                 foreach ($messageList as $message) {
219                         exit(__METHOD__ . ':MSG:' . $message);
220                 } // END - foreach
221         }
222
223         /**
224          * Builds the master template's name
225          *
226          * @return      $masterTemplateName             Name of the master template
227          */
228         public function buildMasterTemplateName () {
229                 return 'node_main';
230         }
231
232         /**
233          * Assigns extra application-depending data
234          *
235          * @param       $templateInstance       An instance of a CompileableTemplate
236          * @return      void
237          * @todo        Nothing to add?
238          */
239         public function assignExtraTemplateData (CompileableTemplate $templateInstance) {
240                 $this->partialStub('Unfinished method. templateInstance=' . $templateInstance->__toString());
241         }
242 }
243
244 // [EOF]
245 ?>